1 /* 2 * Copyright (c) 2001 The Regents of the University of Michigan. 3 * All rights reserved. 4 * 5 * Kendrick Smith <kmsmith@umich.edu> 6 * Andy Adamson <kandros@umich.edu> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35 #include <linux/file.h> 36 #include <linux/fs.h> 37 #include <linux/slab.h> 38 #include <linux/namei.h> 39 #include <linux/swap.h> 40 #include <linux/pagemap.h> 41 #include <linux/ratelimit.h> 42 #include <linux/sunrpc/svcauth_gss.h> 43 #include <linux/sunrpc/addr.h> 44 #include <linux/jhash.h> 45 #include <linux/string_helpers.h> 46 #include <linux/fsnotify.h> 47 #include <linux/rhashtable.h> 48 #include <linux/nfs_ssc.h> 49 50 #include "xdr4.h" 51 #include "xdr4cb.h" 52 #include "vfs.h" 53 #include "current_stateid.h" 54 55 #include "netns.h" 56 #include "pnfs.h" 57 #include "filecache.h" 58 #include "trace.h" 59 60 #define NFSDDBG_FACILITY NFSDDBG_PROC 61 62 #define all_ones {{ ~0, ~0}, ~0} 63 static const stateid_t one_stateid = { 64 .si_generation = ~0, 65 .si_opaque = all_ones, 66 }; 67 static const stateid_t zero_stateid = { 68 /* all fields zero */ 69 }; 70 static const stateid_t currentstateid = { 71 .si_generation = 1, 72 }; 73 static const stateid_t close_stateid = { 74 .si_generation = 0xffffffffU, 75 }; 76 77 static u64 current_sessionid = 1; 78 79 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) 80 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) 81 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) 82 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) 83 84 /* forward declarations */ 85 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); 86 static void nfs4_free_ol_stateid(struct nfs4_stid *stid); 87 void nfsd4_end_grace(struct nfsd_net *nn); 88 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps); 89 static void nfsd4_file_hash_remove(struct nfs4_file *fi); 90 static void deleg_reaper(struct nfsd_net *nn); 91 92 /* Locking: */ 93 94 /* 95 * Currently used for the del_recall_lru and file hash table. In an 96 * effort to decrease the scope of the client_mutex, this spinlock may 97 * eventually cover more: 98 */ 99 static DEFINE_SPINLOCK(state_lock); 100 101 enum nfsd4_st_mutex_lock_subclass { 102 OPEN_STATEID_MUTEX = 0, 103 LOCK_STATEID_MUTEX = 1, 104 }; 105 106 /* 107 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for 108 * the refcount on the open stateid to drop. 109 */ 110 static DECLARE_WAIT_QUEUE_HEAD(close_wq); 111 112 /* 113 * A waitqueue where a writer to clients/#/ctl destroying a client can 114 * wait for cl_rpc_users to drop to 0 and then for the client to be 115 * unhashed. 116 */ 117 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq); 118 119 static struct kmem_cache *client_slab; 120 static struct kmem_cache *openowner_slab; 121 static struct kmem_cache *lockowner_slab; 122 static struct kmem_cache *file_slab; 123 static struct kmem_cache *stateid_slab; 124 static struct kmem_cache *deleg_slab; 125 static struct kmem_cache *odstate_slab; 126 127 static void free_session(struct nfsd4_session *); 128 129 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; 130 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; 131 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops; 132 133 static struct workqueue_struct *laundry_wq; 134 135 int nfsd4_create_laundry_wq(void) 136 { 137 int rc = 0; 138 139 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); 140 if (laundry_wq == NULL) 141 rc = -ENOMEM; 142 return rc; 143 } 144 145 void nfsd4_destroy_laundry_wq(void) 146 { 147 destroy_workqueue(laundry_wq); 148 } 149 150 static bool is_session_dead(struct nfsd4_session *ses) 151 { 152 return ses->se_dead; 153 } 154 155 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) 156 { 157 if (atomic_read(&ses->se_ref) > ref_held_by_me) 158 return nfserr_jukebox; 159 ses->se_dead = true; 160 return nfs_ok; 161 } 162 163 static bool is_client_expired(struct nfs4_client *clp) 164 { 165 return clp->cl_time == 0; 166 } 167 168 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn, 169 struct nfs4_client *clp) 170 { 171 if (clp->cl_state != NFSD4_ACTIVE) 172 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0); 173 } 174 175 static __be32 get_client_locked(struct nfs4_client *clp) 176 { 177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 178 179 lockdep_assert_held(&nn->client_lock); 180 181 if (is_client_expired(clp)) 182 return nfserr_expired; 183 atomic_inc(&clp->cl_rpc_users); 184 nfsd4_dec_courtesy_client_count(nn, clp); 185 clp->cl_state = NFSD4_ACTIVE; 186 return nfs_ok; 187 } 188 189 /* must be called under the client_lock */ 190 static inline void 191 renew_client_locked(struct nfs4_client *clp) 192 { 193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 194 195 if (is_client_expired(clp)) { 196 WARN_ON(1); 197 printk("%s: client (clientid %08x/%08x) already expired\n", 198 __func__, 199 clp->cl_clientid.cl_boot, 200 clp->cl_clientid.cl_id); 201 return; 202 } 203 204 list_move_tail(&clp->cl_lru, &nn->client_lru); 205 clp->cl_time = ktime_get_boottime_seconds(); 206 nfsd4_dec_courtesy_client_count(nn, clp); 207 clp->cl_state = NFSD4_ACTIVE; 208 } 209 210 static void put_client_renew_locked(struct nfs4_client *clp) 211 { 212 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 213 214 lockdep_assert_held(&nn->client_lock); 215 216 if (!atomic_dec_and_test(&clp->cl_rpc_users)) 217 return; 218 if (!is_client_expired(clp)) 219 renew_client_locked(clp); 220 else 221 wake_up_all(&expiry_wq); 222 } 223 224 static void put_client_renew(struct nfs4_client *clp) 225 { 226 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 227 228 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) 229 return; 230 if (!is_client_expired(clp)) 231 renew_client_locked(clp); 232 else 233 wake_up_all(&expiry_wq); 234 spin_unlock(&nn->client_lock); 235 } 236 237 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) 238 { 239 __be32 status; 240 241 if (is_session_dead(ses)) 242 return nfserr_badsession; 243 status = get_client_locked(ses->se_client); 244 if (status) 245 return status; 246 atomic_inc(&ses->se_ref); 247 return nfs_ok; 248 } 249 250 static void nfsd4_put_session_locked(struct nfsd4_session *ses) 251 { 252 struct nfs4_client *clp = ses->se_client; 253 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 254 255 lockdep_assert_held(&nn->client_lock); 256 257 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) 258 free_session(ses); 259 put_client_renew_locked(clp); 260 } 261 262 static void nfsd4_put_session(struct nfsd4_session *ses) 263 { 264 struct nfs4_client *clp = ses->se_client; 265 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 266 267 spin_lock(&nn->client_lock); 268 nfsd4_put_session_locked(ses); 269 spin_unlock(&nn->client_lock); 270 } 271 272 static struct nfsd4_blocked_lock * 273 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, 274 struct nfsd_net *nn) 275 { 276 struct nfsd4_blocked_lock *cur, *found = NULL; 277 278 spin_lock(&nn->blocked_locks_lock); 279 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { 280 if (fh_match(fh, &cur->nbl_fh)) { 281 list_del_init(&cur->nbl_list); 282 WARN_ON(list_empty(&cur->nbl_lru)); 283 list_del_init(&cur->nbl_lru); 284 found = cur; 285 break; 286 } 287 } 288 spin_unlock(&nn->blocked_locks_lock); 289 if (found) 290 locks_delete_block(&found->nbl_lock); 291 return found; 292 } 293 294 static struct nfsd4_blocked_lock * 295 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, 296 struct nfsd_net *nn) 297 { 298 struct nfsd4_blocked_lock *nbl; 299 300 nbl = find_blocked_lock(lo, fh, nn); 301 if (!nbl) { 302 nbl = kmalloc(sizeof(*nbl), GFP_KERNEL); 303 if (nbl) { 304 INIT_LIST_HEAD(&nbl->nbl_list); 305 INIT_LIST_HEAD(&nbl->nbl_lru); 306 fh_copy_shallow(&nbl->nbl_fh, fh); 307 locks_init_lock(&nbl->nbl_lock); 308 kref_init(&nbl->nbl_kref); 309 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, 310 &nfsd4_cb_notify_lock_ops, 311 NFSPROC4_CLNT_CB_NOTIFY_LOCK); 312 } 313 } 314 return nbl; 315 } 316 317 static void 318 free_nbl(struct kref *kref) 319 { 320 struct nfsd4_blocked_lock *nbl; 321 322 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref); 323 locks_release_private(&nbl->nbl_lock); 324 kfree(nbl); 325 } 326 327 static void 328 free_blocked_lock(struct nfsd4_blocked_lock *nbl) 329 { 330 locks_delete_block(&nbl->nbl_lock); 331 kref_put(&nbl->nbl_kref, free_nbl); 332 } 333 334 static void 335 remove_blocked_locks(struct nfs4_lockowner *lo) 336 { 337 struct nfs4_client *clp = lo->lo_owner.so_client; 338 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 339 struct nfsd4_blocked_lock *nbl; 340 LIST_HEAD(reaplist); 341 342 /* Dequeue all blocked locks */ 343 spin_lock(&nn->blocked_locks_lock); 344 while (!list_empty(&lo->lo_blocked)) { 345 nbl = list_first_entry(&lo->lo_blocked, 346 struct nfsd4_blocked_lock, 347 nbl_list); 348 list_del_init(&nbl->nbl_list); 349 WARN_ON(list_empty(&nbl->nbl_lru)); 350 list_move(&nbl->nbl_lru, &reaplist); 351 } 352 spin_unlock(&nn->blocked_locks_lock); 353 354 /* Now free them */ 355 while (!list_empty(&reaplist)) { 356 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, 357 nbl_lru); 358 list_del_init(&nbl->nbl_lru); 359 free_blocked_lock(nbl); 360 } 361 } 362 363 static void 364 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) 365 { 366 struct nfsd4_blocked_lock *nbl = container_of(cb, 367 struct nfsd4_blocked_lock, nbl_cb); 368 locks_delete_block(&nbl->nbl_lock); 369 } 370 371 static int 372 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) 373 { 374 trace_nfsd_cb_notify_lock_done(&zero_stateid, task); 375 376 /* 377 * Since this is just an optimization, we don't try very hard if it 378 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and 379 * just quit trying on anything else. 380 */ 381 switch (task->tk_status) { 382 case -NFS4ERR_DELAY: 383 rpc_delay(task, 1 * HZ); 384 return 0; 385 default: 386 return 1; 387 } 388 } 389 390 static void 391 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) 392 { 393 struct nfsd4_blocked_lock *nbl = container_of(cb, 394 struct nfsd4_blocked_lock, nbl_cb); 395 396 free_blocked_lock(nbl); 397 } 398 399 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { 400 .prepare = nfsd4_cb_notify_lock_prepare, 401 .done = nfsd4_cb_notify_lock_done, 402 .release = nfsd4_cb_notify_lock_release, 403 .opcode = OP_CB_NOTIFY_LOCK, 404 }; 405 406 /* 407 * We store the NONE, READ, WRITE, and BOTH bits separately in the 408 * st_{access,deny}_bmap field of the stateid, in order to track not 409 * only what share bits are currently in force, but also what 410 * combinations of share bits previous opens have used. This allows us 411 * to enforce the recommendation in 412 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that 413 * the server return an error if the client attempt to downgrade to a 414 * combination of share bits not explicable by closing some of its 415 * previous opens. 416 * 417 * This enforcement is arguably incomplete, since we don't keep 418 * track of access/deny bit combinations; so, e.g., we allow: 419 * 420 * OPEN allow read, deny write 421 * OPEN allow both, deny none 422 * DOWNGRADE allow read, deny none 423 * 424 * which we should reject. 425 * 426 * But you could also argue that our current code is already overkill, 427 * since it only exists to return NFS4ERR_INVAL on incorrect client 428 * behavior. 429 */ 430 static unsigned int 431 bmap_to_share_mode(unsigned long bmap) 432 { 433 int i; 434 unsigned int access = 0; 435 436 for (i = 1; i < 4; i++) { 437 if (test_bit(i, &bmap)) 438 access |= i; 439 } 440 return access; 441 } 442 443 /* set share access for a given stateid */ 444 static inline void 445 set_access(u32 access, struct nfs4_ol_stateid *stp) 446 { 447 unsigned char mask = 1 << access; 448 449 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); 450 stp->st_access_bmap |= mask; 451 } 452 453 /* clear share access for a given stateid */ 454 static inline void 455 clear_access(u32 access, struct nfs4_ol_stateid *stp) 456 { 457 unsigned char mask = 1 << access; 458 459 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); 460 stp->st_access_bmap &= ~mask; 461 } 462 463 /* test whether a given stateid has access */ 464 static inline bool 465 test_access(u32 access, struct nfs4_ol_stateid *stp) 466 { 467 unsigned char mask = 1 << access; 468 469 return (bool)(stp->st_access_bmap & mask); 470 } 471 472 /* set share deny for a given stateid */ 473 static inline void 474 set_deny(u32 deny, struct nfs4_ol_stateid *stp) 475 { 476 unsigned char mask = 1 << deny; 477 478 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); 479 stp->st_deny_bmap |= mask; 480 } 481 482 /* clear share deny for a given stateid */ 483 static inline void 484 clear_deny(u32 deny, struct nfs4_ol_stateid *stp) 485 { 486 unsigned char mask = 1 << deny; 487 488 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); 489 stp->st_deny_bmap &= ~mask; 490 } 491 492 /* test whether a given stateid is denying specific access */ 493 static inline bool 494 test_deny(u32 deny, struct nfs4_ol_stateid *stp) 495 { 496 unsigned char mask = 1 << deny; 497 498 return (bool)(stp->st_deny_bmap & mask); 499 } 500 501 static int nfs4_access_to_omode(u32 access) 502 { 503 switch (access & NFS4_SHARE_ACCESS_BOTH) { 504 case NFS4_SHARE_ACCESS_READ: 505 return O_RDONLY; 506 case NFS4_SHARE_ACCESS_WRITE: 507 return O_WRONLY; 508 case NFS4_SHARE_ACCESS_BOTH: 509 return O_RDWR; 510 } 511 WARN_ON_ONCE(1); 512 return O_RDONLY; 513 } 514 515 static inline int 516 access_permit_read(struct nfs4_ol_stateid *stp) 517 { 518 return test_access(NFS4_SHARE_ACCESS_READ, stp) || 519 test_access(NFS4_SHARE_ACCESS_BOTH, stp) || 520 test_access(NFS4_SHARE_ACCESS_WRITE, stp); 521 } 522 523 static inline int 524 access_permit_write(struct nfs4_ol_stateid *stp) 525 { 526 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || 527 test_access(NFS4_SHARE_ACCESS_BOTH, stp); 528 } 529 530 static inline struct nfs4_stateowner * 531 nfs4_get_stateowner(struct nfs4_stateowner *sop) 532 { 533 atomic_inc(&sop->so_count); 534 return sop; 535 } 536 537 static int 538 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) 539 { 540 return (sop->so_owner.len == owner->len) && 541 0 == memcmp(sop->so_owner.data, owner->data, owner->len); 542 } 543 544 static struct nfs4_openowner * 545 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, 546 struct nfs4_client *clp) 547 { 548 struct nfs4_stateowner *so; 549 550 lockdep_assert_held(&clp->cl_lock); 551 552 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], 553 so_strhash) { 554 if (!so->so_is_open_owner) 555 continue; 556 if (same_owner_str(so, &open->op_owner)) 557 return openowner(nfs4_get_stateowner(so)); 558 } 559 return NULL; 560 } 561 562 static inline u32 563 opaque_hashval(const void *ptr, int nbytes) 564 { 565 unsigned char *cptr = (unsigned char *) ptr; 566 567 u32 x = 0; 568 while (nbytes--) { 569 x *= 37; 570 x += *cptr++; 571 } 572 return x; 573 } 574 575 void 576 put_nfs4_file(struct nfs4_file *fi) 577 { 578 if (refcount_dec_and_test(&fi->fi_ref)) { 579 nfsd4_file_hash_remove(fi); 580 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); 581 WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); 582 kfree_rcu(fi, fi_rcu); 583 } 584 } 585 586 static struct nfsd_file * 587 find_writeable_file_locked(struct nfs4_file *f) 588 { 589 struct nfsd_file *ret; 590 591 lockdep_assert_held(&f->fi_lock); 592 593 ret = nfsd_file_get(f->fi_fds[O_WRONLY]); 594 if (!ret) 595 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 596 return ret; 597 } 598 599 static struct nfsd_file * 600 find_writeable_file(struct nfs4_file *f) 601 { 602 struct nfsd_file *ret; 603 604 spin_lock(&f->fi_lock); 605 ret = find_writeable_file_locked(f); 606 spin_unlock(&f->fi_lock); 607 608 return ret; 609 } 610 611 static struct nfsd_file * 612 find_readable_file_locked(struct nfs4_file *f) 613 { 614 struct nfsd_file *ret; 615 616 lockdep_assert_held(&f->fi_lock); 617 618 ret = nfsd_file_get(f->fi_fds[O_RDONLY]); 619 if (!ret) 620 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 621 return ret; 622 } 623 624 static struct nfsd_file * 625 find_readable_file(struct nfs4_file *f) 626 { 627 struct nfsd_file *ret; 628 629 spin_lock(&f->fi_lock); 630 ret = find_readable_file_locked(f); 631 spin_unlock(&f->fi_lock); 632 633 return ret; 634 } 635 636 struct nfsd_file * 637 find_any_file(struct nfs4_file *f) 638 { 639 struct nfsd_file *ret; 640 641 if (!f) 642 return NULL; 643 spin_lock(&f->fi_lock); 644 ret = nfsd_file_get(f->fi_fds[O_RDWR]); 645 if (!ret) { 646 ret = nfsd_file_get(f->fi_fds[O_WRONLY]); 647 if (!ret) 648 ret = nfsd_file_get(f->fi_fds[O_RDONLY]); 649 } 650 spin_unlock(&f->fi_lock); 651 return ret; 652 } 653 654 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f) 655 { 656 lockdep_assert_held(&f->fi_lock); 657 658 if (f->fi_fds[O_RDWR]) 659 return f->fi_fds[O_RDWR]; 660 if (f->fi_fds[O_WRONLY]) 661 return f->fi_fds[O_WRONLY]; 662 if (f->fi_fds[O_RDONLY]) 663 return f->fi_fds[O_RDONLY]; 664 return NULL; 665 } 666 667 static atomic_long_t num_delegations; 668 unsigned long max_delegations; 669 670 /* 671 * Open owner state (share locks) 672 */ 673 674 /* hash tables for lock and open owners */ 675 #define OWNER_HASH_BITS 8 676 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) 677 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) 678 679 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) 680 { 681 unsigned int ret; 682 683 ret = opaque_hashval(ownername->data, ownername->len); 684 return ret & OWNER_HASH_MASK; 685 } 686 687 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp; 688 689 static const struct rhashtable_params nfs4_file_rhash_params = { 690 .key_len = sizeof_field(struct nfs4_file, fi_inode), 691 .key_offset = offsetof(struct nfs4_file, fi_inode), 692 .head_offset = offsetof(struct nfs4_file, fi_rlist), 693 694 /* 695 * Start with a single page hash table to reduce resizing churn 696 * on light workloads. 697 */ 698 .min_size = 256, 699 .automatic_shrinking = true, 700 }; 701 702 /* 703 * Check if courtesy clients have conflicting access and resolve it if possible 704 * 705 * access: is op_share_access if share_access is true. 706 * Check if access mode, op_share_access, would conflict with 707 * the current deny mode of the file 'fp'. 708 * access: is op_share_deny if share_access is false. 709 * Check if the deny mode, op_share_deny, would conflict with 710 * current access of the file 'fp'. 711 * stp: skip checking this entry. 712 * new_stp: normal open, not open upgrade. 713 * 714 * Function returns: 715 * false - access/deny mode conflict with normal client. 716 * true - no conflict or conflict with courtesy client(s) is resolved. 717 */ 718 static bool 719 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp, 720 struct nfs4_ol_stateid *stp, u32 access, bool share_access) 721 { 722 struct nfs4_ol_stateid *st; 723 bool resolvable = true; 724 unsigned char bmap; 725 struct nfsd_net *nn; 726 struct nfs4_client *clp; 727 728 lockdep_assert_held(&fp->fi_lock); 729 list_for_each_entry(st, &fp->fi_stateids, st_perfile) { 730 /* ignore lock stateid */ 731 if (st->st_openstp) 732 continue; 733 if (st == stp && new_stp) 734 continue; 735 /* check file access against deny mode or vice versa */ 736 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap; 737 if (!(access & bmap_to_share_mode(bmap))) 738 continue; 739 clp = st->st_stid.sc_client; 740 if (try_to_expire_client(clp)) 741 continue; 742 resolvable = false; 743 break; 744 } 745 if (resolvable) { 746 clp = stp->st_stid.sc_client; 747 nn = net_generic(clp->net, nfsd_net_id); 748 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 749 } 750 return resolvable; 751 } 752 753 static void 754 __nfs4_file_get_access(struct nfs4_file *fp, u32 access) 755 { 756 lockdep_assert_held(&fp->fi_lock); 757 758 if (access & NFS4_SHARE_ACCESS_WRITE) 759 atomic_inc(&fp->fi_access[O_WRONLY]); 760 if (access & NFS4_SHARE_ACCESS_READ) 761 atomic_inc(&fp->fi_access[O_RDONLY]); 762 } 763 764 static __be32 765 nfs4_file_get_access(struct nfs4_file *fp, u32 access) 766 { 767 lockdep_assert_held(&fp->fi_lock); 768 769 /* Does this access mode make sense? */ 770 if (access & ~NFS4_SHARE_ACCESS_BOTH) 771 return nfserr_inval; 772 773 /* Does it conflict with a deny mode already set? */ 774 if ((access & fp->fi_share_deny) != 0) 775 return nfserr_share_denied; 776 777 __nfs4_file_get_access(fp, access); 778 return nfs_ok; 779 } 780 781 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) 782 { 783 /* Common case is that there is no deny mode. */ 784 if (deny) { 785 /* Does this deny mode make sense? */ 786 if (deny & ~NFS4_SHARE_DENY_BOTH) 787 return nfserr_inval; 788 789 if ((deny & NFS4_SHARE_DENY_READ) && 790 atomic_read(&fp->fi_access[O_RDONLY])) 791 return nfserr_share_denied; 792 793 if ((deny & NFS4_SHARE_DENY_WRITE) && 794 atomic_read(&fp->fi_access[O_WRONLY])) 795 return nfserr_share_denied; 796 } 797 return nfs_ok; 798 } 799 800 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) 801 { 802 might_lock(&fp->fi_lock); 803 804 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { 805 struct nfsd_file *f1 = NULL; 806 struct nfsd_file *f2 = NULL; 807 808 swap(f1, fp->fi_fds[oflag]); 809 if (atomic_read(&fp->fi_access[1 - oflag]) == 0) 810 swap(f2, fp->fi_fds[O_RDWR]); 811 spin_unlock(&fp->fi_lock); 812 if (f1) 813 nfsd_file_put(f1); 814 if (f2) 815 nfsd_file_put(f2); 816 } 817 } 818 819 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) 820 { 821 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); 822 823 if (access & NFS4_SHARE_ACCESS_WRITE) 824 __nfs4_file_put_access(fp, O_WRONLY); 825 if (access & NFS4_SHARE_ACCESS_READ) 826 __nfs4_file_put_access(fp, O_RDONLY); 827 } 828 829 /* 830 * Allocate a new open/delegation state counter. This is needed for 831 * pNFS for proper return on close semantics. 832 * 833 * Note that we only allocate it for pNFS-enabled exports, otherwise 834 * all pointers to struct nfs4_clnt_odstate are always NULL. 835 */ 836 static struct nfs4_clnt_odstate * 837 alloc_clnt_odstate(struct nfs4_client *clp) 838 { 839 struct nfs4_clnt_odstate *co; 840 841 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); 842 if (co) { 843 co->co_client = clp; 844 refcount_set(&co->co_odcount, 1); 845 } 846 return co; 847 } 848 849 static void 850 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) 851 { 852 struct nfs4_file *fp = co->co_file; 853 854 lockdep_assert_held(&fp->fi_lock); 855 list_add(&co->co_perfile, &fp->fi_clnt_odstate); 856 } 857 858 static inline void 859 get_clnt_odstate(struct nfs4_clnt_odstate *co) 860 { 861 if (co) 862 refcount_inc(&co->co_odcount); 863 } 864 865 static void 866 put_clnt_odstate(struct nfs4_clnt_odstate *co) 867 { 868 struct nfs4_file *fp; 869 870 if (!co) 871 return; 872 873 fp = co->co_file; 874 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { 875 list_del(&co->co_perfile); 876 spin_unlock(&fp->fi_lock); 877 878 nfsd4_return_all_file_layouts(co->co_client, fp); 879 kmem_cache_free(odstate_slab, co); 880 } 881 } 882 883 static struct nfs4_clnt_odstate * 884 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) 885 { 886 struct nfs4_clnt_odstate *co; 887 struct nfs4_client *cl; 888 889 if (!new) 890 return NULL; 891 892 cl = new->co_client; 893 894 spin_lock(&fp->fi_lock); 895 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { 896 if (co->co_client == cl) { 897 get_clnt_odstate(co); 898 goto out; 899 } 900 } 901 co = new; 902 co->co_file = fp; 903 hash_clnt_odstate_locked(new); 904 out: 905 spin_unlock(&fp->fi_lock); 906 return co; 907 } 908 909 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, 910 void (*sc_free)(struct nfs4_stid *)) 911 { 912 struct nfs4_stid *stid; 913 int new_id; 914 915 stid = kmem_cache_zalloc(slab, GFP_KERNEL); 916 if (!stid) 917 return NULL; 918 919 idr_preload(GFP_KERNEL); 920 spin_lock(&cl->cl_lock); 921 /* Reserving 0 for start of file in nfsdfs "states" file: */ 922 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT); 923 spin_unlock(&cl->cl_lock); 924 idr_preload_end(); 925 if (new_id < 0) 926 goto out_free; 927 928 stid->sc_free = sc_free; 929 stid->sc_client = cl; 930 stid->sc_stateid.si_opaque.so_id = new_id; 931 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; 932 /* Will be incremented before return to client: */ 933 refcount_set(&stid->sc_count, 1); 934 spin_lock_init(&stid->sc_lock); 935 INIT_LIST_HEAD(&stid->sc_cp_list); 936 937 return stid; 938 out_free: 939 kmem_cache_free(slab, stid); 940 return NULL; 941 } 942 943 /* 944 * Create a unique stateid_t to represent each COPY. 945 */ 946 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, 947 unsigned char cs_type) 948 { 949 int new_id; 950 951 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; 952 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; 953 954 idr_preload(GFP_KERNEL); 955 spin_lock(&nn->s2s_cp_lock); 956 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); 957 stid->cs_stid.si_opaque.so_id = new_id; 958 stid->cs_stid.si_generation = 1; 959 spin_unlock(&nn->s2s_cp_lock); 960 idr_preload_end(); 961 if (new_id < 0) 962 return 0; 963 stid->cs_type = cs_type; 964 return 1; 965 } 966 967 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) 968 { 969 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); 970 } 971 972 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, 973 struct nfs4_stid *p_stid) 974 { 975 struct nfs4_cpntf_state *cps; 976 977 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); 978 if (!cps) 979 return NULL; 980 cps->cpntf_time = ktime_get_boottime_seconds(); 981 refcount_set(&cps->cp_stateid.cs_count, 1); 982 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) 983 goto out_free; 984 spin_lock(&nn->s2s_cp_lock); 985 list_add(&cps->cp_list, &p_stid->sc_cp_list); 986 spin_unlock(&nn->s2s_cp_lock); 987 return cps; 988 out_free: 989 kfree(cps); 990 return NULL; 991 } 992 993 void nfs4_free_copy_state(struct nfsd4_copy *copy) 994 { 995 struct nfsd_net *nn; 996 997 if (copy->cp_stateid.cs_type != NFS4_COPY_STID) 998 return; 999 nn = net_generic(copy->cp_clp->net, nfsd_net_id); 1000 spin_lock(&nn->s2s_cp_lock); 1001 idr_remove(&nn->s2s_cp_stateids, 1002 copy->cp_stateid.cs_stid.si_opaque.so_id); 1003 spin_unlock(&nn->s2s_cp_lock); 1004 } 1005 1006 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) 1007 { 1008 struct nfs4_cpntf_state *cps; 1009 struct nfsd_net *nn; 1010 1011 nn = net_generic(net, nfsd_net_id); 1012 spin_lock(&nn->s2s_cp_lock); 1013 while (!list_empty(&stid->sc_cp_list)) { 1014 cps = list_first_entry(&stid->sc_cp_list, 1015 struct nfs4_cpntf_state, cp_list); 1016 _free_cpntf_state_locked(nn, cps); 1017 } 1018 spin_unlock(&nn->s2s_cp_lock); 1019 } 1020 1021 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) 1022 { 1023 struct nfs4_stid *stid; 1024 1025 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); 1026 if (!stid) 1027 return NULL; 1028 1029 return openlockstateid(stid); 1030 } 1031 1032 /* 1033 * As the sc_free callback of deleg, this may be called by nfs4_put_stid 1034 * in nfsd_break_one_deleg. 1035 * Considering nfsd_break_one_deleg is called with the flc->flc_lock held, 1036 * this function mustn't ever sleep. 1037 */ 1038 static void nfs4_free_deleg(struct nfs4_stid *stid) 1039 { 1040 struct nfs4_delegation *dp = delegstateid(stid); 1041 1042 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list)); 1043 WARN_ON_ONCE(!list_empty(&dp->dl_perfile)); 1044 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt)); 1045 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru)); 1046 kmem_cache_free(deleg_slab, stid); 1047 atomic_long_dec(&num_delegations); 1048 } 1049 1050 /* 1051 * When we recall a delegation, we should be careful not to hand it 1052 * out again straight away. 1053 * To ensure this we keep a pair of bloom filters ('new' and 'old') 1054 * in which the filehandles of recalled delegations are "stored". 1055 * If a filehandle appear in either filter, a delegation is blocked. 1056 * When a delegation is recalled, the filehandle is stored in the "new" 1057 * filter. 1058 * Every 30 seconds we swap the filters and clear the "new" one, 1059 * unless both are empty of course. This results in delegations for a 1060 * given filehandle being blocked for between 30 and 60 seconds. 1061 * 1062 * Each filter is 256 bits. We hash the filehandle to 32bit and use the 1063 * low 3 bytes as hash-table indices. 1064 * 1065 * 'blocked_delegations_lock', which is always taken in block_delegations(), 1066 * is used to manage concurrent access. Testing does not need the lock 1067 * except when swapping the two filters. 1068 */ 1069 static DEFINE_SPINLOCK(blocked_delegations_lock); 1070 static struct bloom_pair { 1071 int entries, old_entries; 1072 time64_t swap_time; 1073 int new; /* index into 'set' */ 1074 DECLARE_BITMAP(set[2], 256); 1075 } blocked_delegations; 1076 1077 static int delegation_blocked(struct knfsd_fh *fh) 1078 { 1079 u32 hash; 1080 struct bloom_pair *bd = &blocked_delegations; 1081 1082 if (bd->entries == 0) 1083 return 0; 1084 if (ktime_get_seconds() - bd->swap_time > 30) { 1085 spin_lock(&blocked_delegations_lock); 1086 if (ktime_get_seconds() - bd->swap_time > 30) { 1087 bd->entries -= bd->old_entries; 1088 bd->old_entries = bd->entries; 1089 bd->new = 1-bd->new; 1090 memset(bd->set[bd->new], 0, 1091 sizeof(bd->set[0])); 1092 bd->swap_time = ktime_get_seconds(); 1093 } 1094 spin_unlock(&blocked_delegations_lock); 1095 } 1096 hash = jhash(&fh->fh_raw, fh->fh_size, 0); 1097 if (test_bit(hash&255, bd->set[0]) && 1098 test_bit((hash>>8)&255, bd->set[0]) && 1099 test_bit((hash>>16)&255, bd->set[0])) 1100 return 1; 1101 1102 if (test_bit(hash&255, bd->set[1]) && 1103 test_bit((hash>>8)&255, bd->set[1]) && 1104 test_bit((hash>>16)&255, bd->set[1])) 1105 return 1; 1106 1107 return 0; 1108 } 1109 1110 static void block_delegations(struct knfsd_fh *fh) 1111 { 1112 u32 hash; 1113 struct bloom_pair *bd = &blocked_delegations; 1114 1115 hash = jhash(&fh->fh_raw, fh->fh_size, 0); 1116 1117 spin_lock(&blocked_delegations_lock); 1118 __set_bit(hash&255, bd->set[bd->new]); 1119 __set_bit((hash>>8)&255, bd->set[bd->new]); 1120 __set_bit((hash>>16)&255, bd->set[bd->new]); 1121 if (bd->entries == 0) 1122 bd->swap_time = ktime_get_seconds(); 1123 bd->entries += 1; 1124 spin_unlock(&blocked_delegations_lock); 1125 } 1126 1127 static struct nfs4_delegation * 1128 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, 1129 struct nfs4_clnt_odstate *odstate, u32 dl_type) 1130 { 1131 struct nfs4_delegation *dp; 1132 struct nfs4_stid *stid; 1133 long n; 1134 1135 dprintk("NFSD alloc_init_deleg\n"); 1136 n = atomic_long_inc_return(&num_delegations); 1137 if (n < 0 || n > max_delegations) 1138 goto out_dec; 1139 if (delegation_blocked(&fp->fi_fhandle)) 1140 goto out_dec; 1141 stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg); 1142 if (stid == NULL) 1143 goto out_dec; 1144 dp = delegstateid(stid); 1145 1146 /* 1147 * delegation seqid's are never incremented. The 4.1 special 1148 * meaning of seqid 0 isn't meaningful, really, but let's avoid 1149 * 0 anyway just for consistency and use 1: 1150 */ 1151 dp->dl_stid.sc_stateid.si_generation = 1; 1152 INIT_LIST_HEAD(&dp->dl_perfile); 1153 INIT_LIST_HEAD(&dp->dl_perclnt); 1154 INIT_LIST_HEAD(&dp->dl_recall_lru); 1155 dp->dl_clnt_odstate = odstate; 1156 get_clnt_odstate(odstate); 1157 dp->dl_type = dl_type; 1158 dp->dl_retries = 1; 1159 dp->dl_recalled = false; 1160 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, 1161 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); 1162 nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client, 1163 &nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR); 1164 dp->dl_cb_fattr.ncf_file_modified = false; 1165 get_nfs4_file(fp); 1166 dp->dl_stid.sc_file = fp; 1167 return dp; 1168 out_dec: 1169 atomic_long_dec(&num_delegations); 1170 return NULL; 1171 } 1172 1173 void 1174 nfs4_put_stid(struct nfs4_stid *s) 1175 { 1176 struct nfs4_file *fp = s->sc_file; 1177 struct nfs4_client *clp = s->sc_client; 1178 1179 might_lock(&clp->cl_lock); 1180 1181 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { 1182 wake_up_all(&close_wq); 1183 return; 1184 } 1185 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); 1186 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 1187 atomic_dec(&s->sc_client->cl_admin_revoked); 1188 nfs4_free_cpntf_statelist(clp->net, s); 1189 spin_unlock(&clp->cl_lock); 1190 s->sc_free(s); 1191 if (fp) 1192 put_nfs4_file(fp); 1193 } 1194 1195 void 1196 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) 1197 { 1198 stateid_t *src = &stid->sc_stateid; 1199 1200 spin_lock(&stid->sc_lock); 1201 if (unlikely(++src->si_generation == 0)) 1202 src->si_generation = 1; 1203 memcpy(dst, src, sizeof(*dst)); 1204 spin_unlock(&stid->sc_lock); 1205 } 1206 1207 static void put_deleg_file(struct nfs4_file *fp) 1208 { 1209 struct nfsd_file *rnf = NULL; 1210 struct nfsd_file *nf = NULL; 1211 1212 spin_lock(&fp->fi_lock); 1213 if (--fp->fi_delegees == 0) { 1214 swap(nf, fp->fi_deleg_file); 1215 swap(rnf, fp->fi_rdeleg_file); 1216 } 1217 spin_unlock(&fp->fi_lock); 1218 1219 if (nf) 1220 nfsd_file_put(nf); 1221 if (rnf) 1222 nfs4_file_put_access(fp, NFS4_SHARE_ACCESS_READ); 1223 } 1224 1225 static void nfsd4_finalize_deleg_timestamps(struct nfs4_delegation *dp, struct file *f) 1226 { 1227 struct iattr ia = { .ia_valid = ATTR_ATIME | ATTR_CTIME | ATTR_MTIME }; 1228 struct inode *inode = file_inode(f); 1229 int ret; 1230 1231 /* don't do anything if FMODE_NOCMTIME isn't set */ 1232 if ((READ_ONCE(f->f_mode) & FMODE_NOCMTIME) == 0) 1233 return; 1234 1235 spin_lock(&f->f_lock); 1236 f->f_mode &= ~FMODE_NOCMTIME; 1237 spin_unlock(&f->f_lock); 1238 1239 /* was it never written? */ 1240 if (!dp->dl_written) 1241 return; 1242 1243 /* did it get a setattr for the timestamps at some point? */ 1244 if (dp->dl_setattr) 1245 return; 1246 1247 /* Stamp everything to "now" */ 1248 inode_lock(inode); 1249 ret = notify_change(&nop_mnt_idmap, f->f_path.dentry, &ia, NULL); 1250 inode_unlock(inode); 1251 if (ret) { 1252 struct inode *inode = file_inode(f); 1253 1254 pr_notice_ratelimited("Unable to update timestamps on inode %02x:%02x:%lu: %d\n", 1255 MAJOR(inode->i_sb->s_dev), 1256 MINOR(inode->i_sb->s_dev), 1257 inode->i_ino, ret); 1258 } 1259 } 1260 1261 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) 1262 { 1263 struct nfs4_file *fp = dp->dl_stid.sc_file; 1264 struct nfsd_file *nf = fp->fi_deleg_file; 1265 1266 WARN_ON_ONCE(!fp->fi_delegees); 1267 1268 nfsd4_finalize_deleg_timestamps(dp, nf->nf_file); 1269 kernel_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); 1270 put_deleg_file(fp); 1271 } 1272 1273 static void destroy_unhashed_deleg(struct nfs4_delegation *dp) 1274 { 1275 put_clnt_odstate(dp->dl_clnt_odstate); 1276 nfs4_unlock_deleg_lease(dp); 1277 nfs4_put_stid(&dp->dl_stid); 1278 } 1279 1280 /** 1281 * nfs4_delegation_exists - Discover if this delegation already exists 1282 * @clp: a pointer to the nfs4_client we're granting a delegation to 1283 * @fp: a pointer to the nfs4_file we're granting a delegation on 1284 * 1285 * Return: 1286 * On success: true iff an existing delegation is found 1287 */ 1288 1289 static bool 1290 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) 1291 { 1292 struct nfs4_delegation *searchdp = NULL; 1293 struct nfs4_client *searchclp = NULL; 1294 1295 lockdep_assert_held(&state_lock); 1296 lockdep_assert_held(&fp->fi_lock); 1297 1298 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { 1299 searchclp = searchdp->dl_stid.sc_client; 1300 if (clp == searchclp) { 1301 return true; 1302 } 1303 } 1304 return false; 1305 } 1306 1307 /** 1308 * hash_delegation_locked - Add a delegation to the appropriate lists 1309 * @dp: a pointer to the nfs4_delegation we are adding. 1310 * @fp: a pointer to the nfs4_file we're granting a delegation on 1311 * 1312 * Return: 1313 * On success: NULL if the delegation was successfully hashed. 1314 * 1315 * On error: -EAGAIN if one was previously granted to this 1316 * nfs4_client for this nfs4_file. Delegation is not hashed. 1317 * 1318 */ 1319 1320 static int 1321 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) 1322 { 1323 struct nfs4_client *clp = dp->dl_stid.sc_client; 1324 1325 lockdep_assert_held(&state_lock); 1326 lockdep_assert_held(&fp->fi_lock); 1327 lockdep_assert_held(&clp->cl_lock); 1328 1329 if (nfs4_delegation_exists(clp, fp)) 1330 return -EAGAIN; 1331 refcount_inc(&dp->dl_stid.sc_count); 1332 dp->dl_stid.sc_type = SC_TYPE_DELEG; 1333 list_add(&dp->dl_perfile, &fp->fi_delegations); 1334 list_add(&dp->dl_perclnt, &clp->cl_delegations); 1335 return 0; 1336 } 1337 1338 static bool delegation_hashed(struct nfs4_delegation *dp) 1339 { 1340 return !(list_empty(&dp->dl_perfile)); 1341 } 1342 1343 static bool 1344 unhash_delegation_locked(struct nfs4_delegation *dp, unsigned short statusmask) 1345 { 1346 struct nfs4_file *fp = dp->dl_stid.sc_file; 1347 1348 lockdep_assert_held(&state_lock); 1349 1350 if (!delegation_hashed(dp)) 1351 return false; 1352 1353 if (statusmask == SC_STATUS_REVOKED && 1354 dp->dl_stid.sc_client->cl_minorversion == 0) 1355 statusmask = SC_STATUS_CLOSED; 1356 dp->dl_stid.sc_status |= statusmask; 1357 if (statusmask & SC_STATUS_ADMIN_REVOKED) 1358 atomic_inc(&dp->dl_stid.sc_client->cl_admin_revoked); 1359 1360 /* Ensure that deleg break won't try to requeue it */ 1361 ++dp->dl_time; 1362 spin_lock(&fp->fi_lock); 1363 list_del_init(&dp->dl_perclnt); 1364 list_del_init(&dp->dl_recall_lru); 1365 list_del_init(&dp->dl_perfile); 1366 spin_unlock(&fp->fi_lock); 1367 return true; 1368 } 1369 1370 static void destroy_delegation(struct nfs4_delegation *dp) 1371 { 1372 bool unhashed; 1373 1374 spin_lock(&state_lock); 1375 unhashed = unhash_delegation_locked(dp, SC_STATUS_CLOSED); 1376 spin_unlock(&state_lock); 1377 if (unhashed) 1378 destroy_unhashed_deleg(dp); 1379 } 1380 1381 /** 1382 * revoke_delegation - perform nfs4 delegation structure cleanup 1383 * @dp: pointer to the delegation 1384 * 1385 * This function assumes that it's called either from the administrative 1386 * interface (nfsd4_revoke_states()) that's revoking a specific delegation 1387 * stateid or it's called from a laundromat thread (nfsd4_landromat()) that 1388 * determined that this specific state has expired and needs to be revoked 1389 * (both mark state with the appropriate stid sc_status mode). It is also 1390 * assumed that a reference was taken on the @dp state. 1391 * 1392 * If this function finds that the @dp state is SC_STATUS_FREED it means 1393 * that a FREE_STATEID operation for this stateid has been processed and 1394 * we can proceed to removing it from recalled list. However, if @dp state 1395 * isn't marked SC_STATUS_FREED, it means we need place it on the cl_revoked 1396 * list and wait for the FREE_STATEID to arrive from the client. At the same 1397 * time, we need to mark it as SC_STATUS_FREEABLE to indicate to the 1398 * nfsd4_free_stateid() function that this stateid has already been added 1399 * to the cl_revoked list and that nfsd4_free_stateid() is now responsible 1400 * for removing it from the list. Inspection of where the delegation state 1401 * in the revocation process is protected by the clp->cl_lock. 1402 */ 1403 static void revoke_delegation(struct nfs4_delegation *dp) 1404 { 1405 struct nfs4_client *clp = dp->dl_stid.sc_client; 1406 1407 WARN_ON(!list_empty(&dp->dl_recall_lru)); 1408 WARN_ON_ONCE(dp->dl_stid.sc_client->cl_minorversion > 0 && 1409 !(dp->dl_stid.sc_status & 1410 (SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED))); 1411 1412 trace_nfsd_stid_revoke(&dp->dl_stid); 1413 1414 spin_lock(&clp->cl_lock); 1415 if (dp->dl_stid.sc_status & SC_STATUS_FREED) { 1416 list_del_init(&dp->dl_recall_lru); 1417 goto out; 1418 } 1419 list_add(&dp->dl_recall_lru, &clp->cl_revoked); 1420 dp->dl_stid.sc_status |= SC_STATUS_FREEABLE; 1421 out: 1422 spin_unlock(&clp->cl_lock); 1423 destroy_unhashed_deleg(dp); 1424 } 1425 1426 /* 1427 * SETCLIENTID state 1428 */ 1429 1430 static unsigned int clientid_hashval(u32 id) 1431 { 1432 return id & CLIENT_HASH_MASK; 1433 } 1434 1435 static unsigned int clientstr_hashval(struct xdr_netobj name) 1436 { 1437 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; 1438 } 1439 1440 /* 1441 * A stateid that had a deny mode associated with it is being released 1442 * or downgraded. Recalculate the deny mode on the file. 1443 */ 1444 static void 1445 recalculate_deny_mode(struct nfs4_file *fp) 1446 { 1447 struct nfs4_ol_stateid *stp; 1448 u32 old_deny; 1449 1450 spin_lock(&fp->fi_lock); 1451 old_deny = fp->fi_share_deny; 1452 fp->fi_share_deny = 0; 1453 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { 1454 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); 1455 if (fp->fi_share_deny == old_deny) 1456 break; 1457 } 1458 spin_unlock(&fp->fi_lock); 1459 } 1460 1461 static void 1462 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) 1463 { 1464 int i; 1465 bool change = false; 1466 1467 for (i = 1; i < 4; i++) { 1468 if ((i & deny) != i) { 1469 change = true; 1470 clear_deny(i, stp); 1471 } 1472 } 1473 1474 /* Recalculate per-file deny mode if there was a change */ 1475 if (change) 1476 recalculate_deny_mode(stp->st_stid.sc_file); 1477 } 1478 1479 /* release all access and file references for a given stateid */ 1480 static void 1481 release_all_access(struct nfs4_ol_stateid *stp) 1482 { 1483 int i; 1484 struct nfs4_file *fp = stp->st_stid.sc_file; 1485 1486 if (fp && stp->st_deny_bmap != 0) 1487 recalculate_deny_mode(fp); 1488 1489 for (i = 1; i < 4; i++) { 1490 if (test_access(i, stp)) 1491 nfs4_file_put_access(stp->st_stid.sc_file, i); 1492 clear_access(i, stp); 1493 } 1494 } 1495 1496 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) 1497 { 1498 kfree(sop->so_owner.data); 1499 sop->so_ops->so_free(sop); 1500 } 1501 1502 static void nfs4_put_stateowner(struct nfs4_stateowner *sop) 1503 { 1504 struct nfs4_client *clp = sop->so_client; 1505 1506 might_lock(&clp->cl_lock); 1507 1508 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) 1509 return; 1510 sop->so_ops->so_unhash(sop); 1511 spin_unlock(&clp->cl_lock); 1512 nfs4_free_stateowner(sop); 1513 } 1514 1515 static bool 1516 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) 1517 { 1518 return list_empty(&stp->st_perfile); 1519 } 1520 1521 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) 1522 { 1523 struct nfs4_file *fp = stp->st_stid.sc_file; 1524 1525 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); 1526 1527 if (list_empty(&stp->st_perfile)) 1528 return false; 1529 1530 spin_lock(&fp->fi_lock); 1531 list_del_init(&stp->st_perfile); 1532 spin_unlock(&fp->fi_lock); 1533 list_del(&stp->st_perstateowner); 1534 return true; 1535 } 1536 1537 static void nfs4_free_ol_stateid(struct nfs4_stid *stid) 1538 { 1539 struct nfs4_ol_stateid *stp = openlockstateid(stid); 1540 1541 put_clnt_odstate(stp->st_clnt_odstate); 1542 release_all_access(stp); 1543 if (stp->st_stateowner) 1544 nfs4_put_stateowner(stp->st_stateowner); 1545 WARN_ON(!list_empty(&stid->sc_cp_list)); 1546 kmem_cache_free(stateid_slab, stid); 1547 } 1548 1549 static void nfs4_free_lock_stateid(struct nfs4_stid *stid) 1550 { 1551 struct nfs4_ol_stateid *stp = openlockstateid(stid); 1552 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); 1553 struct nfsd_file *nf; 1554 1555 nf = find_any_file(stp->st_stid.sc_file); 1556 if (nf) { 1557 get_file(nf->nf_file); 1558 filp_close(nf->nf_file, (fl_owner_t)lo); 1559 nfsd_file_put(nf); 1560 } 1561 nfs4_free_ol_stateid(stid); 1562 } 1563 1564 /* 1565 * Put the persistent reference to an already unhashed generic stateid, while 1566 * holding the cl_lock. If it's the last reference, then put it onto the 1567 * reaplist for later destruction. 1568 */ 1569 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, 1570 struct list_head *reaplist) 1571 { 1572 struct nfs4_stid *s = &stp->st_stid; 1573 struct nfs4_client *clp = s->sc_client; 1574 1575 lockdep_assert_held(&clp->cl_lock); 1576 1577 WARN_ON_ONCE(!list_empty(&stp->st_locks)); 1578 1579 if (!refcount_dec_and_test(&s->sc_count)) { 1580 wake_up_all(&close_wq); 1581 return; 1582 } 1583 1584 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); 1585 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 1586 atomic_dec(&s->sc_client->cl_admin_revoked); 1587 list_add(&stp->st_locks, reaplist); 1588 } 1589 1590 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) 1591 { 1592 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); 1593 1594 if (!unhash_ol_stateid(stp)) 1595 return false; 1596 list_del_init(&stp->st_locks); 1597 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 1598 return true; 1599 } 1600 1601 static void release_lock_stateid(struct nfs4_ol_stateid *stp) 1602 { 1603 struct nfs4_client *clp = stp->st_stid.sc_client; 1604 bool unhashed; 1605 1606 spin_lock(&clp->cl_lock); 1607 unhashed = unhash_lock_stateid(stp); 1608 spin_unlock(&clp->cl_lock); 1609 if (unhashed) 1610 nfs4_put_stid(&stp->st_stid); 1611 } 1612 1613 static void unhash_lockowner_locked(struct nfs4_lockowner *lo) 1614 { 1615 struct nfs4_client *clp = lo->lo_owner.so_client; 1616 1617 lockdep_assert_held(&clp->cl_lock); 1618 1619 list_del_init(&lo->lo_owner.so_strhash); 1620 } 1621 1622 /* 1623 * Free a list of generic stateids that were collected earlier after being 1624 * fully unhashed. 1625 */ 1626 static void 1627 free_ol_stateid_reaplist(struct list_head *reaplist) 1628 { 1629 struct nfs4_ol_stateid *stp; 1630 struct nfs4_file *fp; 1631 1632 might_sleep(); 1633 1634 while (!list_empty(reaplist)) { 1635 stp = list_first_entry(reaplist, struct nfs4_ol_stateid, 1636 st_locks); 1637 list_del(&stp->st_locks); 1638 fp = stp->st_stid.sc_file; 1639 stp->st_stid.sc_free(&stp->st_stid); 1640 if (fp) 1641 put_nfs4_file(fp); 1642 } 1643 } 1644 1645 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, 1646 struct list_head *reaplist) 1647 { 1648 struct nfs4_ol_stateid *stp; 1649 1650 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); 1651 1652 while (!list_empty(&open_stp->st_locks)) { 1653 stp = list_entry(open_stp->st_locks.next, 1654 struct nfs4_ol_stateid, st_locks); 1655 unhash_lock_stateid(stp); 1656 put_ol_stateid_locked(stp, reaplist); 1657 } 1658 } 1659 1660 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, 1661 struct list_head *reaplist) 1662 { 1663 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); 1664 1665 if (!unhash_ol_stateid(stp)) 1666 return false; 1667 release_open_stateid_locks(stp, reaplist); 1668 return true; 1669 } 1670 1671 static void release_open_stateid(struct nfs4_ol_stateid *stp) 1672 { 1673 LIST_HEAD(reaplist); 1674 1675 spin_lock(&stp->st_stid.sc_client->cl_lock); 1676 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 1677 if (unhash_open_stateid(stp, &reaplist)) 1678 put_ol_stateid_locked(stp, &reaplist); 1679 spin_unlock(&stp->st_stid.sc_client->cl_lock); 1680 free_ol_stateid_reaplist(&reaplist); 1681 } 1682 1683 static bool nfs4_openowner_unhashed(struct nfs4_openowner *oo) 1684 { 1685 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock); 1686 1687 return list_empty(&oo->oo_owner.so_strhash) && 1688 list_empty(&oo->oo_perclient); 1689 } 1690 1691 static void unhash_openowner_locked(struct nfs4_openowner *oo) 1692 { 1693 struct nfs4_client *clp = oo->oo_owner.so_client; 1694 1695 lockdep_assert_held(&clp->cl_lock); 1696 1697 list_del_init(&oo->oo_owner.so_strhash); 1698 list_del_init(&oo->oo_perclient); 1699 } 1700 1701 static void release_last_closed_stateid(struct nfs4_openowner *oo) 1702 { 1703 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, 1704 nfsd_net_id); 1705 struct nfs4_ol_stateid *s; 1706 1707 spin_lock(&nn->client_lock); 1708 s = oo->oo_last_closed_stid; 1709 if (s) { 1710 list_del_init(&oo->oo_close_lru); 1711 oo->oo_last_closed_stid = NULL; 1712 } 1713 spin_unlock(&nn->client_lock); 1714 if (s) 1715 nfs4_put_stid(&s->st_stid); 1716 } 1717 1718 static void release_openowner(struct nfs4_openowner *oo) 1719 { 1720 struct nfs4_ol_stateid *stp; 1721 struct nfs4_client *clp = oo->oo_owner.so_client; 1722 LIST_HEAD(reaplist); 1723 1724 spin_lock(&clp->cl_lock); 1725 unhash_openowner_locked(oo); 1726 while (!list_empty(&oo->oo_owner.so_stateids)) { 1727 stp = list_first_entry(&oo->oo_owner.so_stateids, 1728 struct nfs4_ol_stateid, st_perstateowner); 1729 if (unhash_open_stateid(stp, &reaplist)) 1730 put_ol_stateid_locked(stp, &reaplist); 1731 } 1732 spin_unlock(&clp->cl_lock); 1733 free_ol_stateid_reaplist(&reaplist); 1734 release_last_closed_stateid(oo); 1735 nfs4_put_stateowner(&oo->oo_owner); 1736 } 1737 1738 static struct nfs4_stid *find_one_sb_stid(struct nfs4_client *clp, 1739 struct super_block *sb, 1740 unsigned int sc_types) 1741 { 1742 unsigned long id, tmp; 1743 struct nfs4_stid *stid; 1744 1745 spin_lock(&clp->cl_lock); 1746 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id) 1747 if ((stid->sc_type & sc_types) && 1748 stid->sc_status == 0 && 1749 stid->sc_file->fi_inode->i_sb == sb) { 1750 refcount_inc(&stid->sc_count); 1751 break; 1752 } 1753 spin_unlock(&clp->cl_lock); 1754 return stid; 1755 } 1756 1757 /** 1758 * nfsd4_revoke_states - revoke all nfsv4 states associated with given filesystem 1759 * @net: used to identify instance of nfsd (there is one per net namespace) 1760 * @sb: super_block used to identify target filesystem 1761 * 1762 * All nfs4 states (open, lock, delegation, layout) held by the server instance 1763 * and associated with a file on the given filesystem will be revoked resulting 1764 * in any files being closed and so all references from nfsd to the filesystem 1765 * being released. Thus nfsd will no longer prevent the filesystem from being 1766 * unmounted. 1767 * 1768 * The clients which own the states will subsequently being notified that the 1769 * states have been "admin-revoked". 1770 */ 1771 void nfsd4_revoke_states(struct net *net, struct super_block *sb) 1772 { 1773 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1774 unsigned int idhashval; 1775 unsigned int sc_types; 1776 1777 sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT; 1778 1779 spin_lock(&nn->client_lock); 1780 for (idhashval = 0; idhashval < CLIENT_HASH_MASK; idhashval++) { 1781 struct list_head *head = &nn->conf_id_hashtbl[idhashval]; 1782 struct nfs4_client *clp; 1783 retry: 1784 list_for_each_entry(clp, head, cl_idhash) { 1785 struct nfs4_stid *stid = find_one_sb_stid(clp, sb, 1786 sc_types); 1787 if (stid) { 1788 struct nfs4_ol_stateid *stp; 1789 struct nfs4_delegation *dp; 1790 struct nfs4_layout_stateid *ls; 1791 1792 spin_unlock(&nn->client_lock); 1793 switch (stid->sc_type) { 1794 case SC_TYPE_OPEN: 1795 stp = openlockstateid(stid); 1796 mutex_lock_nested(&stp->st_mutex, 1797 OPEN_STATEID_MUTEX); 1798 1799 spin_lock(&clp->cl_lock); 1800 if (stid->sc_status == 0) { 1801 stid->sc_status |= 1802 SC_STATUS_ADMIN_REVOKED; 1803 atomic_inc(&clp->cl_admin_revoked); 1804 spin_unlock(&clp->cl_lock); 1805 release_all_access(stp); 1806 } else 1807 spin_unlock(&clp->cl_lock); 1808 mutex_unlock(&stp->st_mutex); 1809 break; 1810 case SC_TYPE_LOCK: 1811 stp = openlockstateid(stid); 1812 mutex_lock_nested(&stp->st_mutex, 1813 LOCK_STATEID_MUTEX); 1814 spin_lock(&clp->cl_lock); 1815 if (stid->sc_status == 0) { 1816 struct nfs4_lockowner *lo = 1817 lockowner(stp->st_stateowner); 1818 struct nfsd_file *nf; 1819 1820 stid->sc_status |= 1821 SC_STATUS_ADMIN_REVOKED; 1822 atomic_inc(&clp->cl_admin_revoked); 1823 spin_unlock(&clp->cl_lock); 1824 nf = find_any_file(stp->st_stid.sc_file); 1825 if (nf) { 1826 get_file(nf->nf_file); 1827 filp_close(nf->nf_file, 1828 (fl_owner_t)lo); 1829 nfsd_file_put(nf); 1830 } 1831 release_all_access(stp); 1832 } else 1833 spin_unlock(&clp->cl_lock); 1834 mutex_unlock(&stp->st_mutex); 1835 break; 1836 case SC_TYPE_DELEG: 1837 refcount_inc(&stid->sc_count); 1838 dp = delegstateid(stid); 1839 spin_lock(&state_lock); 1840 if (!unhash_delegation_locked( 1841 dp, SC_STATUS_ADMIN_REVOKED)) 1842 dp = NULL; 1843 spin_unlock(&state_lock); 1844 if (dp) 1845 revoke_delegation(dp); 1846 break; 1847 case SC_TYPE_LAYOUT: 1848 ls = layoutstateid(stid); 1849 nfsd4_close_layout(ls); 1850 break; 1851 } 1852 nfs4_put_stid(stid); 1853 spin_lock(&nn->client_lock); 1854 if (clp->cl_minorversion == 0) 1855 /* Allow cleanup after a lease period. 1856 * store_release ensures cleanup will 1857 * see any newly revoked states if it 1858 * sees the time updated. 1859 */ 1860 nn->nfs40_last_revoke = 1861 ktime_get_boottime_seconds(); 1862 goto retry; 1863 } 1864 } 1865 } 1866 spin_unlock(&nn->client_lock); 1867 } 1868 1869 static inline int 1870 hash_sessionid(struct nfs4_sessionid *sessionid) 1871 { 1872 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; 1873 1874 return sid->sequence % SESSION_HASH_SIZE; 1875 } 1876 1877 #ifdef CONFIG_SUNRPC_DEBUG 1878 static inline void 1879 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) 1880 { 1881 u32 *ptr = (u32 *)(&sessionid->data[0]); 1882 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); 1883 } 1884 #else 1885 static inline void 1886 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) 1887 { 1888 } 1889 #endif 1890 1891 /* 1892 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it 1893 * won't be used for replay. 1894 */ 1895 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) 1896 { 1897 struct nfs4_stateowner *so = cstate->replay_owner; 1898 1899 if (nfserr == nfserr_replay_me) 1900 return; 1901 1902 if (!seqid_mutating_err(ntohl(nfserr))) { 1903 nfsd4_cstate_clear_replay(cstate); 1904 return; 1905 } 1906 if (!so) 1907 return; 1908 if (so->so_is_open_owner) 1909 release_last_closed_stateid(openowner(so)); 1910 so->so_seqid++; 1911 return; 1912 } 1913 1914 static void 1915 gen_sessionid(struct nfsd4_session *ses) 1916 { 1917 struct nfs4_client *clp = ses->se_client; 1918 struct nfsd4_sessionid *sid; 1919 1920 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; 1921 sid->clientid = clp->cl_clientid; 1922 sid->sequence = current_sessionid++; 1923 sid->reserved = 0; 1924 } 1925 1926 /* 1927 * The protocol defines ca_maxresponssize_cached to include the size of 1928 * the rpc header, but all we need to cache is the data starting after 1929 * the end of the initial SEQUENCE operation--the rest we regenerate 1930 * each time. Therefore we can advertise a ca_maxresponssize_cached 1931 * value that is the number of bytes in our cache plus a few additional 1932 * bytes. In order to stay on the safe side, and not promise more than 1933 * we can cache, those additional bytes must be the minimum possible: 24 1934 * bytes of rpc header (xid through accept state, with AUTH_NULL 1935 * verifier), 12 for the compound header (with zero-length tag), and 44 1936 * for the SEQUENCE op response: 1937 */ 1938 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) 1939 1940 static struct shrinker *nfsd_slot_shrinker; 1941 static DEFINE_SPINLOCK(nfsd_session_list_lock); 1942 static LIST_HEAD(nfsd_session_list); 1943 /* The sum of "target_slots-1" on every session. The shrinker can push this 1944 * down, though it can take a little while for the memory to actually 1945 * be freed. The "-1" is because we can never free slot 0 while the 1946 * session is active. 1947 */ 1948 static atomic_t nfsd_total_target_slots = ATOMIC_INIT(0); 1949 1950 static void 1951 free_session_slots(struct nfsd4_session *ses, int from) 1952 { 1953 int i; 1954 1955 if (from >= ses->se_fchannel.maxreqs) 1956 return; 1957 1958 for (i = from; i < ses->se_fchannel.maxreqs; i++) { 1959 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i); 1960 1961 /* 1962 * Save the seqid in case we reactivate this slot. 1963 * This will never require a memory allocation so GFP 1964 * flag is irrelevant 1965 */ 1966 xa_store(&ses->se_slots, i, xa_mk_value(slot->sl_seqid), 0); 1967 free_svc_cred(&slot->sl_cred); 1968 kfree(slot); 1969 } 1970 ses->se_fchannel.maxreqs = from; 1971 if (ses->se_target_maxslots > from) { 1972 int new_target = from ?: 1; 1973 atomic_sub(ses->se_target_maxslots - new_target, &nfsd_total_target_slots); 1974 ses->se_target_maxslots = new_target; 1975 } 1976 } 1977 1978 /** 1979 * reduce_session_slots - reduce the target max-slots of a session if possible 1980 * @ses: The session to affect 1981 * @dec: how much to decrease the target by 1982 * 1983 * This interface can be used by a shrinker to reduce the target max-slots 1984 * for a session so that some slots can eventually be freed. 1985 * It uses spin_trylock() as it may be called in a context where another 1986 * spinlock is held that has a dependency on client_lock. As shrinkers are 1987 * best-effort, skiping a session is client_lock is already held has no 1988 * great coast 1989 * 1990 * Return value: 1991 * The number of slots that the target was reduced by. 1992 */ 1993 static int 1994 reduce_session_slots(struct nfsd4_session *ses, int dec) 1995 { 1996 struct nfsd_net *nn = net_generic(ses->se_client->net, 1997 nfsd_net_id); 1998 int ret = 0; 1999 2000 if (ses->se_target_maxslots <= 1) 2001 return ret; 2002 if (!spin_trylock(&nn->client_lock)) 2003 return ret; 2004 ret = min(dec, ses->se_target_maxslots-1); 2005 ses->se_target_maxslots -= ret; 2006 atomic_sub(ret, &nfsd_total_target_slots); 2007 ses->se_slot_gen += 1; 2008 if (ses->se_slot_gen == 0) { 2009 int i; 2010 ses->se_slot_gen = 1; 2011 for (i = 0; i < ses->se_fchannel.maxreqs; i++) { 2012 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i); 2013 slot->sl_generation = 0; 2014 } 2015 } 2016 spin_unlock(&nn->client_lock); 2017 return ret; 2018 } 2019 2020 static struct nfsd4_slot *nfsd4_alloc_slot(struct nfsd4_channel_attrs *fattrs, 2021 int index, gfp_t gfp) 2022 { 2023 struct nfsd4_slot *slot; 2024 size_t size; 2025 2026 /* 2027 * The RPC and NFS session headers are never saved in 2028 * the slot reply cache buffer. 2029 */ 2030 size = fattrs->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ ? 2031 0 : fattrs->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; 2032 2033 slot = kzalloc(struct_size(slot, sl_data, size), gfp); 2034 if (!slot) 2035 return NULL; 2036 slot->sl_index = index; 2037 return slot; 2038 } 2039 2040 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, 2041 struct nfsd4_channel_attrs *battrs) 2042 { 2043 int numslots = fattrs->maxreqs; 2044 struct nfsd4_session *new; 2045 struct nfsd4_slot *slot; 2046 int i; 2047 2048 new = kzalloc(sizeof(*new), GFP_KERNEL); 2049 if (!new) 2050 return NULL; 2051 xa_init(&new->se_slots); 2052 2053 slot = nfsd4_alloc_slot(fattrs, 0, GFP_KERNEL); 2054 if (!slot || xa_is_err(xa_store(&new->se_slots, 0, slot, GFP_KERNEL))) 2055 goto out_free; 2056 2057 for (i = 1; i < numslots; i++) { 2058 const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN; 2059 slot = nfsd4_alloc_slot(fattrs, i, gfp); 2060 if (!slot) 2061 break; 2062 if (xa_is_err(xa_store(&new->se_slots, i, slot, gfp))) { 2063 kfree(slot); 2064 break; 2065 } 2066 } 2067 fattrs->maxreqs = i; 2068 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); 2069 new->se_target_maxslots = i; 2070 atomic_add(i - 1, &nfsd_total_target_slots); 2071 new->se_cb_slot_avail = ~0U; 2072 new->se_cb_highest_slot = min(battrs->maxreqs - 1, 2073 NFSD_BC_SLOT_TABLE_SIZE - 1); 2074 spin_lock_init(&new->se_lock); 2075 return new; 2076 out_free: 2077 kfree(slot); 2078 xa_destroy(&new->se_slots); 2079 kfree(new); 2080 return NULL; 2081 } 2082 2083 static void free_conn(struct nfsd4_conn *c) 2084 { 2085 svc_xprt_put(c->cn_xprt); 2086 kfree(c); 2087 } 2088 2089 static void nfsd4_conn_lost(struct svc_xpt_user *u) 2090 { 2091 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); 2092 struct nfs4_client *clp = c->cn_session->se_client; 2093 2094 trace_nfsd_cb_lost(clp); 2095 2096 spin_lock(&clp->cl_lock); 2097 if (!list_empty(&c->cn_persession)) { 2098 list_del(&c->cn_persession); 2099 free_conn(c); 2100 } 2101 nfsd4_probe_callback(clp); 2102 spin_unlock(&clp->cl_lock); 2103 } 2104 2105 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) 2106 { 2107 struct nfsd4_conn *conn; 2108 2109 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); 2110 if (!conn) 2111 return NULL; 2112 svc_xprt_get(rqstp->rq_xprt); 2113 conn->cn_xprt = rqstp->rq_xprt; 2114 conn->cn_flags = flags; 2115 INIT_LIST_HEAD(&conn->cn_xpt_user.list); 2116 return conn; 2117 } 2118 2119 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) 2120 { 2121 conn->cn_session = ses; 2122 list_add(&conn->cn_persession, &ses->se_conns); 2123 } 2124 2125 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) 2126 { 2127 struct nfs4_client *clp = ses->se_client; 2128 2129 spin_lock(&clp->cl_lock); 2130 __nfsd4_hash_conn(conn, ses); 2131 spin_unlock(&clp->cl_lock); 2132 } 2133 2134 static int nfsd4_register_conn(struct nfsd4_conn *conn) 2135 { 2136 conn->cn_xpt_user.callback = nfsd4_conn_lost; 2137 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); 2138 } 2139 2140 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) 2141 { 2142 int ret; 2143 2144 nfsd4_hash_conn(conn, ses); 2145 ret = nfsd4_register_conn(conn); 2146 if (ret) 2147 /* oops; xprt is already down: */ 2148 nfsd4_conn_lost(&conn->cn_xpt_user); 2149 /* We may have gained or lost a callback channel: */ 2150 nfsd4_probe_callback_sync(ses->se_client); 2151 } 2152 2153 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) 2154 { 2155 u32 dir = NFS4_CDFC4_FORE; 2156 2157 if (cses->flags & SESSION4_BACK_CHAN) 2158 dir |= NFS4_CDFC4_BACK; 2159 return alloc_conn(rqstp, dir); 2160 } 2161 2162 /* must be called under client_lock */ 2163 static void nfsd4_del_conns(struct nfsd4_session *s) 2164 { 2165 struct nfs4_client *clp = s->se_client; 2166 struct nfsd4_conn *c; 2167 2168 spin_lock(&clp->cl_lock); 2169 while (!list_empty(&s->se_conns)) { 2170 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); 2171 list_del_init(&c->cn_persession); 2172 spin_unlock(&clp->cl_lock); 2173 2174 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); 2175 free_conn(c); 2176 2177 spin_lock(&clp->cl_lock); 2178 } 2179 spin_unlock(&clp->cl_lock); 2180 } 2181 2182 static void __free_session(struct nfsd4_session *ses) 2183 { 2184 free_session_slots(ses, 0); 2185 xa_destroy(&ses->se_slots); 2186 kfree(ses); 2187 } 2188 2189 static void free_session(struct nfsd4_session *ses) 2190 { 2191 nfsd4_del_conns(ses); 2192 __free_session(ses); 2193 } 2194 2195 static unsigned long 2196 nfsd_slot_count(struct shrinker *s, struct shrink_control *sc) 2197 { 2198 unsigned long cnt = atomic_read(&nfsd_total_target_slots); 2199 2200 return cnt ? cnt : SHRINK_EMPTY; 2201 } 2202 2203 static unsigned long 2204 nfsd_slot_scan(struct shrinker *s, struct shrink_control *sc) 2205 { 2206 struct nfsd4_session *ses; 2207 unsigned long scanned = 0; 2208 unsigned long freed = 0; 2209 2210 spin_lock(&nfsd_session_list_lock); 2211 list_for_each_entry(ses, &nfsd_session_list, se_all_sessions) { 2212 freed += reduce_session_slots(ses, 1); 2213 scanned += 1; 2214 if (scanned >= sc->nr_to_scan) { 2215 /* Move starting point for next scan */ 2216 list_move(&nfsd_session_list, &ses->se_all_sessions); 2217 break; 2218 } 2219 } 2220 spin_unlock(&nfsd_session_list_lock); 2221 sc->nr_scanned = scanned; 2222 return freed; 2223 } 2224 2225 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) 2226 { 2227 int idx; 2228 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 2229 2230 new->se_client = clp; 2231 gen_sessionid(new); 2232 2233 INIT_LIST_HEAD(&new->se_conns); 2234 2235 atomic_set(&new->se_ref, 0); 2236 new->se_dead = false; 2237 new->se_cb_prog = cses->callback_prog; 2238 new->se_cb_sec = cses->cb_sec; 2239 2240 for (idx = 0; idx < NFSD_BC_SLOT_TABLE_SIZE; ++idx) 2241 new->se_cb_seq_nr[idx] = 1; 2242 2243 idx = hash_sessionid(&new->se_sessionid); 2244 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); 2245 spin_lock(&clp->cl_lock); 2246 list_add(&new->se_perclnt, &clp->cl_sessions); 2247 spin_unlock(&clp->cl_lock); 2248 2249 spin_lock(&nfsd_session_list_lock); 2250 list_add_tail(&new->se_all_sessions, &nfsd_session_list); 2251 spin_unlock(&nfsd_session_list_lock); 2252 2253 { 2254 struct sockaddr *sa = svc_addr(rqstp); 2255 /* 2256 * This is a little silly; with sessions there's no real 2257 * use for the callback address. Use the peer address 2258 * as a reasonable default for now, but consider fixing 2259 * the rpc client not to require an address in the 2260 * future: 2261 */ 2262 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); 2263 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); 2264 } 2265 } 2266 2267 /* caller must hold client_lock */ 2268 static struct nfsd4_session * 2269 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) 2270 { 2271 struct nfsd4_session *elem; 2272 int idx; 2273 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2274 2275 lockdep_assert_held(&nn->client_lock); 2276 2277 dump_sessionid(__func__, sessionid); 2278 idx = hash_sessionid(sessionid); 2279 /* Search in the appropriate list */ 2280 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { 2281 if (!memcmp(elem->se_sessionid.data, sessionid->data, 2282 NFS4_MAX_SESSIONID_LEN)) { 2283 return elem; 2284 } 2285 } 2286 2287 dprintk("%s: session not found\n", __func__); 2288 return NULL; 2289 } 2290 2291 static struct nfsd4_session * 2292 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, 2293 __be32 *ret) 2294 { 2295 struct nfsd4_session *session; 2296 __be32 status = nfserr_badsession; 2297 2298 session = __find_in_sessionid_hashtbl(sessionid, net); 2299 if (!session) 2300 goto out; 2301 status = nfsd4_get_session_locked(session); 2302 if (status) 2303 session = NULL; 2304 out: 2305 *ret = status; 2306 return session; 2307 } 2308 2309 /* caller must hold client_lock */ 2310 static void 2311 unhash_session(struct nfsd4_session *ses) 2312 { 2313 struct nfs4_client *clp = ses->se_client; 2314 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2315 2316 lockdep_assert_held(&nn->client_lock); 2317 2318 list_del(&ses->se_hash); 2319 spin_lock(&ses->se_client->cl_lock); 2320 list_del(&ses->se_perclnt); 2321 spin_unlock(&ses->se_client->cl_lock); 2322 spin_lock(&nfsd_session_list_lock); 2323 list_del(&ses->se_all_sessions); 2324 spin_unlock(&nfsd_session_list_lock); 2325 } 2326 2327 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ 2328 static int 2329 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) 2330 { 2331 /* 2332 * We're assuming the clid was not given out from a boot 2333 * precisely 2^32 (about 136 years) before this one. That seems 2334 * a safe assumption: 2335 */ 2336 if (clid->cl_boot == (u32)nn->boot_time) 2337 return 0; 2338 trace_nfsd_clid_stale(clid); 2339 return 1; 2340 } 2341 2342 static struct nfs4_client *alloc_client(struct xdr_netobj name, 2343 struct nfsd_net *nn) 2344 { 2345 struct nfs4_client *clp; 2346 int i; 2347 2348 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients && 2349 atomic_read(&nn->nfsd_courtesy_clients) > 0) 2350 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 2351 2352 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); 2353 if (clp == NULL) 2354 return NULL; 2355 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); 2356 if (clp->cl_name.data == NULL) 2357 goto err_no_name; 2358 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, 2359 sizeof(struct list_head), 2360 GFP_KERNEL); 2361 if (!clp->cl_ownerstr_hashtbl) 2362 goto err_no_hashtbl; 2363 clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0); 2364 if (!clp->cl_callback_wq) 2365 goto err_no_callback_wq; 2366 2367 for (i = 0; i < OWNER_HASH_SIZE; i++) 2368 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); 2369 INIT_LIST_HEAD(&clp->cl_sessions); 2370 idr_init(&clp->cl_stateids); 2371 atomic_set(&clp->cl_rpc_users, 0); 2372 clp->cl_cb_state = NFSD4_CB_UNKNOWN; 2373 clp->cl_state = NFSD4_ACTIVE; 2374 atomic_inc(&nn->nfs4_client_count); 2375 atomic_set(&clp->cl_delegs_in_recall, 0); 2376 INIT_LIST_HEAD(&clp->cl_idhash); 2377 INIT_LIST_HEAD(&clp->cl_openowners); 2378 INIT_LIST_HEAD(&clp->cl_delegations); 2379 INIT_LIST_HEAD(&clp->cl_lru); 2380 INIT_LIST_HEAD(&clp->cl_revoked); 2381 #ifdef CONFIG_NFSD_PNFS 2382 INIT_LIST_HEAD(&clp->cl_lo_states); 2383 #endif 2384 INIT_LIST_HEAD(&clp->async_copies); 2385 spin_lock_init(&clp->async_lock); 2386 spin_lock_init(&clp->cl_lock); 2387 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); 2388 return clp; 2389 err_no_callback_wq: 2390 kfree(clp->cl_ownerstr_hashtbl); 2391 err_no_hashtbl: 2392 kfree(clp->cl_name.data); 2393 err_no_name: 2394 kmem_cache_free(client_slab, clp); 2395 return NULL; 2396 } 2397 2398 static void __free_client(struct kref *k) 2399 { 2400 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); 2401 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs); 2402 2403 free_svc_cred(&clp->cl_cred); 2404 destroy_workqueue(clp->cl_callback_wq); 2405 kfree(clp->cl_ownerstr_hashtbl); 2406 kfree(clp->cl_name.data); 2407 kfree(clp->cl_nii_domain.data); 2408 kfree(clp->cl_nii_name.data); 2409 idr_destroy(&clp->cl_stateids); 2410 kfree(clp->cl_ra); 2411 kmem_cache_free(client_slab, clp); 2412 } 2413 2414 static void drop_client(struct nfs4_client *clp) 2415 { 2416 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); 2417 } 2418 2419 static void 2420 free_client(struct nfs4_client *clp) 2421 { 2422 while (!list_empty(&clp->cl_sessions)) { 2423 struct nfsd4_session *ses; 2424 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, 2425 se_perclnt); 2426 list_del(&ses->se_perclnt); 2427 WARN_ON_ONCE(atomic_read(&ses->se_ref)); 2428 free_session(ses); 2429 } 2430 rpc_destroy_wait_queue(&clp->cl_cb_waitq); 2431 if (clp->cl_nfsd_dentry) { 2432 nfsd_client_rmdir(clp->cl_nfsd_dentry); 2433 clp->cl_nfsd_dentry = NULL; 2434 wake_up_all(&expiry_wq); 2435 } 2436 drop_client(clp); 2437 } 2438 2439 /* must be called under the client_lock */ 2440 static void 2441 unhash_client_locked(struct nfs4_client *clp) 2442 { 2443 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2444 struct nfsd4_session *ses; 2445 2446 lockdep_assert_held(&nn->client_lock); 2447 2448 /* Mark the client as expired! */ 2449 clp->cl_time = 0; 2450 /* Make it invisible */ 2451 if (!list_empty(&clp->cl_idhash)) { 2452 list_del_init(&clp->cl_idhash); 2453 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) 2454 rb_erase(&clp->cl_namenode, &nn->conf_name_tree); 2455 else 2456 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); 2457 } 2458 list_del_init(&clp->cl_lru); 2459 spin_lock(&clp->cl_lock); 2460 spin_lock(&nfsd_session_list_lock); 2461 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) { 2462 list_del_init(&ses->se_hash); 2463 list_del_init(&ses->se_all_sessions); 2464 } 2465 spin_unlock(&nfsd_session_list_lock); 2466 spin_unlock(&clp->cl_lock); 2467 } 2468 2469 static void 2470 unhash_client(struct nfs4_client *clp) 2471 { 2472 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2473 2474 spin_lock(&nn->client_lock); 2475 unhash_client_locked(clp); 2476 spin_unlock(&nn->client_lock); 2477 } 2478 2479 static __be32 mark_client_expired_locked(struct nfs4_client *clp) 2480 { 2481 int users = atomic_read(&clp->cl_rpc_users); 2482 2483 trace_nfsd_mark_client_expired(clp, users); 2484 2485 if (users) 2486 return nfserr_jukebox; 2487 unhash_client_locked(clp); 2488 return nfs_ok; 2489 } 2490 2491 static void 2492 __destroy_client(struct nfs4_client *clp) 2493 { 2494 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2495 int i; 2496 struct nfs4_openowner *oo; 2497 struct nfs4_delegation *dp; 2498 LIST_HEAD(reaplist); 2499 2500 spin_lock(&state_lock); 2501 while (!list_empty(&clp->cl_delegations)) { 2502 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); 2503 unhash_delegation_locked(dp, SC_STATUS_CLOSED); 2504 list_add(&dp->dl_recall_lru, &reaplist); 2505 } 2506 spin_unlock(&state_lock); 2507 while (!list_empty(&reaplist)) { 2508 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); 2509 list_del_init(&dp->dl_recall_lru); 2510 destroy_unhashed_deleg(dp); 2511 } 2512 while (!list_empty(&clp->cl_revoked)) { 2513 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); 2514 list_del_init(&dp->dl_recall_lru); 2515 nfs4_put_stid(&dp->dl_stid); 2516 } 2517 while (!list_empty(&clp->cl_openowners)) { 2518 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); 2519 nfs4_get_stateowner(&oo->oo_owner); 2520 release_openowner(oo); 2521 } 2522 for (i = 0; i < OWNER_HASH_SIZE; i++) { 2523 struct nfs4_stateowner *so, *tmp; 2524 2525 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], 2526 so_strhash) { 2527 /* Should be no openowners at this point */ 2528 WARN_ON_ONCE(so->so_is_open_owner); 2529 remove_blocked_locks(lockowner(so)); 2530 } 2531 } 2532 nfsd4_return_all_client_layouts(clp); 2533 nfsd4_shutdown_copy(clp); 2534 nfsd4_shutdown_callback(clp); 2535 if (clp->cl_cb_conn.cb_xprt) 2536 svc_xprt_put(clp->cl_cb_conn.cb_xprt); 2537 atomic_add_unless(&nn->nfs4_client_count, -1, 0); 2538 nfsd4_dec_courtesy_client_count(nn, clp); 2539 free_client(clp); 2540 wake_up_all(&expiry_wq); 2541 } 2542 2543 static void 2544 destroy_client(struct nfs4_client *clp) 2545 { 2546 unhash_client(clp); 2547 __destroy_client(clp); 2548 } 2549 2550 static void inc_reclaim_complete(struct nfs4_client *clp) 2551 { 2552 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2553 2554 if (!nn->track_reclaim_completes) 2555 return; 2556 if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) 2557 return; 2558 if (atomic_inc_return(&nn->nr_reclaim_complete) == 2559 nn->reclaim_str_hashtbl_size) { 2560 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", 2561 clp->net->ns.inum); 2562 nfsd4_end_grace(nn); 2563 } 2564 } 2565 2566 static void expire_client(struct nfs4_client *clp) 2567 { 2568 unhash_client(clp); 2569 nfsd4_client_record_remove(clp); 2570 __destroy_client(clp); 2571 } 2572 2573 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) 2574 { 2575 memcpy(target->cl_verifier.data, source->data, 2576 sizeof(target->cl_verifier.data)); 2577 } 2578 2579 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) 2580 { 2581 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 2582 target->cl_clientid.cl_id = source->cl_clientid.cl_id; 2583 } 2584 2585 static int copy_cred(struct svc_cred *target, struct svc_cred *source) 2586 { 2587 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); 2588 target->cr_raw_principal = kstrdup(source->cr_raw_principal, 2589 GFP_KERNEL); 2590 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); 2591 if ((source->cr_principal && !target->cr_principal) || 2592 (source->cr_raw_principal && !target->cr_raw_principal) || 2593 (source->cr_targ_princ && !target->cr_targ_princ)) 2594 return -ENOMEM; 2595 2596 target->cr_flavor = source->cr_flavor; 2597 target->cr_uid = source->cr_uid; 2598 target->cr_gid = source->cr_gid; 2599 target->cr_group_info = source->cr_group_info; 2600 get_group_info(target->cr_group_info); 2601 target->cr_gss_mech = source->cr_gss_mech; 2602 if (source->cr_gss_mech) 2603 gss_mech_get(source->cr_gss_mech); 2604 return 0; 2605 } 2606 2607 static int 2608 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) 2609 { 2610 if (o1->len < o2->len) 2611 return -1; 2612 if (o1->len > o2->len) 2613 return 1; 2614 return memcmp(o1->data, o2->data, o1->len); 2615 } 2616 2617 static int 2618 same_verf(nfs4_verifier *v1, nfs4_verifier *v2) 2619 { 2620 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); 2621 } 2622 2623 static int 2624 same_clid(clientid_t *cl1, clientid_t *cl2) 2625 { 2626 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); 2627 } 2628 2629 static bool groups_equal(struct group_info *g1, struct group_info *g2) 2630 { 2631 int i; 2632 2633 if (g1->ngroups != g2->ngroups) 2634 return false; 2635 for (i=0; i<g1->ngroups; i++) 2636 if (!gid_eq(g1->gid[i], g2->gid[i])) 2637 return false; 2638 return true; 2639 } 2640 2641 /* 2642 * RFC 3530 language requires clid_inuse be returned when the 2643 * "principal" associated with a requests differs from that previously 2644 * used. We use uid, gid's, and gss principal string as our best 2645 * approximation. We also don't want to allow non-gss use of a client 2646 * established using gss: in theory cr_principal should catch that 2647 * change, but in practice cr_principal can be null even in the gss case 2648 * since gssd doesn't always pass down a principal string. 2649 */ 2650 static bool is_gss_cred(struct svc_cred *cr) 2651 { 2652 /* Is cr_flavor one of the gss "pseudoflavors"?: */ 2653 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); 2654 } 2655 2656 2657 static bool 2658 same_creds(struct svc_cred *cr1, struct svc_cred *cr2) 2659 { 2660 if ((is_gss_cred(cr1) != is_gss_cred(cr2)) 2661 || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) 2662 || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) 2663 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) 2664 return false; 2665 /* XXX: check that cr_targ_princ fields match ? */ 2666 if (cr1->cr_principal == cr2->cr_principal) 2667 return true; 2668 if (!cr1->cr_principal || !cr2->cr_principal) 2669 return false; 2670 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); 2671 } 2672 2673 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) 2674 { 2675 struct svc_cred *cr = &rqstp->rq_cred; 2676 u32 service; 2677 2678 if (!cr->cr_gss_mech) 2679 return false; 2680 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); 2681 return service == RPC_GSS_SVC_INTEGRITY || 2682 service == RPC_GSS_SVC_PRIVACY; 2683 } 2684 2685 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) 2686 { 2687 struct svc_cred *cr = &rqstp->rq_cred; 2688 2689 if (!cl->cl_mach_cred) 2690 return true; 2691 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) 2692 return false; 2693 if (!svc_rqst_integrity_protected(rqstp)) 2694 return false; 2695 if (cl->cl_cred.cr_raw_principal) 2696 return 0 == strcmp(cl->cl_cred.cr_raw_principal, 2697 cr->cr_raw_principal); 2698 if (!cr->cr_principal) 2699 return false; 2700 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); 2701 } 2702 2703 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) 2704 { 2705 __be32 verf[2]; 2706 2707 /* 2708 * This is opaque to client, so no need to byte-swap. Use 2709 * __force to keep sparse happy 2710 */ 2711 verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); 2712 verf[1] = (__force __be32)nn->clverifier_counter++; 2713 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); 2714 } 2715 2716 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) 2717 { 2718 clp->cl_clientid.cl_boot = (u32)nn->boot_time; 2719 clp->cl_clientid.cl_id = nn->clientid_counter++; 2720 gen_confirm(clp, nn); 2721 } 2722 2723 static struct nfs4_stid * 2724 find_stateid_locked(struct nfs4_client *cl, stateid_t *t) 2725 { 2726 struct nfs4_stid *ret; 2727 2728 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); 2729 if (!ret || !ret->sc_type) 2730 return NULL; 2731 return ret; 2732 } 2733 2734 static struct nfs4_stid * 2735 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, 2736 unsigned short typemask, unsigned short ok_states) 2737 { 2738 struct nfs4_stid *s; 2739 2740 spin_lock(&cl->cl_lock); 2741 s = find_stateid_locked(cl, t); 2742 if (s != NULL) { 2743 if ((s->sc_status & ~ok_states) == 0 && 2744 (typemask & s->sc_type)) 2745 refcount_inc(&s->sc_count); 2746 else 2747 s = NULL; 2748 } 2749 spin_unlock(&cl->cl_lock); 2750 return s; 2751 } 2752 2753 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) 2754 { 2755 struct nfsdfs_client *nc; 2756 nc = get_nfsdfs_client(inode); 2757 if (!nc) 2758 return NULL; 2759 return container_of(nc, struct nfs4_client, cl_nfsdfs); 2760 } 2761 2762 static void seq_quote_mem(struct seq_file *m, char *data, int len) 2763 { 2764 seq_puts(m, "\""); 2765 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\"); 2766 seq_puts(m, "\""); 2767 } 2768 2769 static const char *cb_state2str(int state) 2770 { 2771 switch (state) { 2772 case NFSD4_CB_UP: 2773 return "UP"; 2774 case NFSD4_CB_UNKNOWN: 2775 return "UNKNOWN"; 2776 case NFSD4_CB_DOWN: 2777 return "DOWN"; 2778 case NFSD4_CB_FAULT: 2779 return "FAULT"; 2780 } 2781 return "UNDEFINED"; 2782 } 2783 2784 static int client_info_show(struct seq_file *m, void *v) 2785 { 2786 struct inode *inode = file_inode(m->file); 2787 struct nfsd4_session *ses; 2788 struct nfs4_client *clp; 2789 u64 clid; 2790 2791 clp = get_nfsdfs_clp(inode); 2792 if (!clp) 2793 return -ENXIO; 2794 memcpy(&clid, &clp->cl_clientid, sizeof(clid)); 2795 seq_printf(m, "clientid: 0x%llx\n", clid); 2796 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); 2797 2798 if (clp->cl_state == NFSD4_COURTESY) 2799 seq_puts(m, "status: courtesy\n"); 2800 else if (clp->cl_state == NFSD4_EXPIRABLE) 2801 seq_puts(m, "status: expirable\n"); 2802 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) 2803 seq_puts(m, "status: confirmed\n"); 2804 else 2805 seq_puts(m, "status: unconfirmed\n"); 2806 seq_printf(m, "seconds from last renew: %lld\n", 2807 ktime_get_boottime_seconds() - clp->cl_time); 2808 seq_puts(m, "name: "); 2809 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); 2810 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); 2811 if (clp->cl_nii_domain.data) { 2812 seq_puts(m, "Implementation domain: "); 2813 seq_quote_mem(m, clp->cl_nii_domain.data, 2814 clp->cl_nii_domain.len); 2815 seq_puts(m, "\nImplementation name: "); 2816 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len); 2817 seq_printf(m, "\nImplementation time: [%lld, %ld]\n", 2818 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); 2819 } 2820 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state)); 2821 seq_printf(m, "callback address: \"%pISpc\"\n", &clp->cl_cb_conn.cb_addr); 2822 seq_printf(m, "admin-revoked states: %d\n", 2823 atomic_read(&clp->cl_admin_revoked)); 2824 spin_lock(&clp->cl_lock); 2825 seq_printf(m, "session slots:"); 2826 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) 2827 seq_printf(m, " %u", ses->se_fchannel.maxreqs); 2828 seq_printf(m, "\nsession target slots:"); 2829 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) 2830 seq_printf(m, " %u", ses->se_target_maxslots); 2831 spin_unlock(&clp->cl_lock); 2832 seq_puts(m, "\n"); 2833 2834 drop_client(clp); 2835 2836 return 0; 2837 } 2838 2839 DEFINE_SHOW_ATTRIBUTE(client_info); 2840 2841 static void *states_start(struct seq_file *s, loff_t *pos) 2842 __acquires(&clp->cl_lock) 2843 { 2844 struct nfs4_client *clp = s->private; 2845 unsigned long id = *pos; 2846 void *ret; 2847 2848 spin_lock(&clp->cl_lock); 2849 ret = idr_get_next_ul(&clp->cl_stateids, &id); 2850 *pos = id; 2851 return ret; 2852 } 2853 2854 static void *states_next(struct seq_file *s, void *v, loff_t *pos) 2855 { 2856 struct nfs4_client *clp = s->private; 2857 unsigned long id = *pos; 2858 void *ret; 2859 2860 id = *pos; 2861 id++; 2862 ret = idr_get_next_ul(&clp->cl_stateids, &id); 2863 *pos = id; 2864 return ret; 2865 } 2866 2867 static void states_stop(struct seq_file *s, void *v) 2868 __releases(&clp->cl_lock) 2869 { 2870 struct nfs4_client *clp = s->private; 2871 2872 spin_unlock(&clp->cl_lock); 2873 } 2874 2875 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f) 2876 { 2877 seq_printf(s, "filename: \"%pD2\"", f->nf_file); 2878 } 2879 2880 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f) 2881 { 2882 struct inode *inode = file_inode(f->nf_file); 2883 2884 seq_printf(s, "superblock: \"%02x:%02x:%ld\"", 2885 MAJOR(inode->i_sb->s_dev), 2886 MINOR(inode->i_sb->s_dev), 2887 inode->i_ino); 2888 } 2889 2890 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) 2891 { 2892 seq_puts(s, "owner: "); 2893 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); 2894 } 2895 2896 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid) 2897 { 2898 seq_printf(s, "0x%.8x", stid->si_generation); 2899 seq_printf(s, "%12phN", &stid->si_opaque); 2900 } 2901 2902 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) 2903 { 2904 struct nfs4_ol_stateid *ols; 2905 struct nfs4_file *nf; 2906 struct nfsd_file *file; 2907 struct nfs4_stateowner *oo; 2908 unsigned int access, deny; 2909 2910 ols = openlockstateid(st); 2911 oo = ols->st_stateowner; 2912 nf = st->sc_file; 2913 2914 seq_puts(s, "- "); 2915 nfs4_show_stateid(s, &st->sc_stateid); 2916 seq_puts(s, ": { type: open, "); 2917 2918 access = bmap_to_share_mode(ols->st_access_bmap); 2919 deny = bmap_to_share_mode(ols->st_deny_bmap); 2920 2921 seq_printf(s, "access: %s%s, ", 2922 access & NFS4_SHARE_ACCESS_READ ? "r" : "-", 2923 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); 2924 seq_printf(s, "deny: %s%s, ", 2925 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", 2926 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); 2927 2928 if (nf) { 2929 spin_lock(&nf->fi_lock); 2930 file = find_any_file_locked(nf); 2931 if (file) { 2932 nfs4_show_superblock(s, file); 2933 seq_puts(s, ", "); 2934 nfs4_show_fname(s, file); 2935 seq_puts(s, ", "); 2936 } 2937 spin_unlock(&nf->fi_lock); 2938 } else 2939 seq_puts(s, "closed, "); 2940 nfs4_show_owner(s, oo); 2941 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 2942 seq_puts(s, ", admin-revoked"); 2943 seq_puts(s, " }\n"); 2944 return 0; 2945 } 2946 2947 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) 2948 { 2949 struct nfs4_ol_stateid *ols; 2950 struct nfs4_file *nf; 2951 struct nfsd_file *file; 2952 struct nfs4_stateowner *oo; 2953 2954 ols = openlockstateid(st); 2955 oo = ols->st_stateowner; 2956 nf = st->sc_file; 2957 2958 seq_puts(s, "- "); 2959 nfs4_show_stateid(s, &st->sc_stateid); 2960 seq_puts(s, ": { type: lock, "); 2961 2962 spin_lock(&nf->fi_lock); 2963 file = find_any_file_locked(nf); 2964 if (file) { 2965 /* 2966 * Note: a lock stateid isn't really the same thing as a lock, 2967 * it's the locking state held by one owner on a file, and there 2968 * may be multiple (or no) lock ranges associated with it. 2969 * (Same for the matter is true of open stateids.) 2970 */ 2971 2972 nfs4_show_superblock(s, file); 2973 /* XXX: open stateid? */ 2974 seq_puts(s, ", "); 2975 nfs4_show_fname(s, file); 2976 seq_puts(s, ", "); 2977 } 2978 nfs4_show_owner(s, oo); 2979 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 2980 seq_puts(s, ", admin-revoked"); 2981 seq_puts(s, " }\n"); 2982 spin_unlock(&nf->fi_lock); 2983 return 0; 2984 } 2985 2986 static char *nfs4_show_deleg_type(u32 dl_type) 2987 { 2988 switch (dl_type) { 2989 case OPEN_DELEGATE_READ: 2990 return "r"; 2991 case OPEN_DELEGATE_WRITE: 2992 return "w"; 2993 case OPEN_DELEGATE_READ_ATTRS_DELEG: 2994 return "ra"; 2995 case OPEN_DELEGATE_WRITE_ATTRS_DELEG: 2996 return "wa"; 2997 } 2998 return "?"; 2999 } 3000 3001 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) 3002 { 3003 struct nfs4_delegation *ds; 3004 struct nfs4_file *nf; 3005 struct nfsd_file *file; 3006 3007 ds = delegstateid(st); 3008 nf = st->sc_file; 3009 3010 seq_puts(s, "- "); 3011 nfs4_show_stateid(s, &st->sc_stateid); 3012 seq_puts(s, ": { type: deleg, "); 3013 3014 seq_printf(s, "access: %s", nfs4_show_deleg_type(ds->dl_type)); 3015 3016 /* XXX: lease time, whether it's being recalled. */ 3017 3018 spin_lock(&nf->fi_lock); 3019 file = nf->fi_deleg_file; 3020 if (file) { 3021 seq_puts(s, ", "); 3022 nfs4_show_superblock(s, file); 3023 seq_puts(s, ", "); 3024 nfs4_show_fname(s, file); 3025 } 3026 spin_unlock(&nf->fi_lock); 3027 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 3028 seq_puts(s, ", admin-revoked"); 3029 seq_puts(s, " }\n"); 3030 return 0; 3031 } 3032 3033 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) 3034 { 3035 struct nfs4_layout_stateid *ls; 3036 struct nfsd_file *file; 3037 3038 ls = container_of(st, struct nfs4_layout_stateid, ls_stid); 3039 3040 seq_puts(s, "- "); 3041 nfs4_show_stateid(s, &st->sc_stateid); 3042 seq_puts(s, ": { type: layout"); 3043 3044 /* XXX: What else would be useful? */ 3045 3046 spin_lock(&ls->ls_stid.sc_file->fi_lock); 3047 file = ls->ls_file; 3048 if (file) { 3049 seq_puts(s, ", "); 3050 nfs4_show_superblock(s, file); 3051 seq_puts(s, ", "); 3052 nfs4_show_fname(s, file); 3053 } 3054 spin_unlock(&ls->ls_stid.sc_file->fi_lock); 3055 if (st->sc_status & SC_STATUS_ADMIN_REVOKED) 3056 seq_puts(s, ", admin-revoked"); 3057 seq_puts(s, " }\n"); 3058 3059 return 0; 3060 } 3061 3062 static int states_show(struct seq_file *s, void *v) 3063 { 3064 struct nfs4_stid *st = v; 3065 3066 switch (st->sc_type) { 3067 case SC_TYPE_OPEN: 3068 return nfs4_show_open(s, st); 3069 case SC_TYPE_LOCK: 3070 return nfs4_show_lock(s, st); 3071 case SC_TYPE_DELEG: 3072 return nfs4_show_deleg(s, st); 3073 case SC_TYPE_LAYOUT: 3074 return nfs4_show_layout(s, st); 3075 default: 3076 return 0; /* XXX: or SEQ_SKIP? */ 3077 } 3078 /* XXX: copy stateids? */ 3079 } 3080 3081 static struct seq_operations states_seq_ops = { 3082 .start = states_start, 3083 .next = states_next, 3084 .stop = states_stop, 3085 .show = states_show 3086 }; 3087 3088 static int client_states_open(struct inode *inode, struct file *file) 3089 { 3090 struct seq_file *s; 3091 struct nfs4_client *clp; 3092 int ret; 3093 3094 clp = get_nfsdfs_clp(inode); 3095 if (!clp) 3096 return -ENXIO; 3097 3098 ret = seq_open(file, &states_seq_ops); 3099 if (ret) 3100 return ret; 3101 s = file->private_data; 3102 s->private = clp; 3103 return 0; 3104 } 3105 3106 static int client_opens_release(struct inode *inode, struct file *file) 3107 { 3108 struct seq_file *m = file->private_data; 3109 struct nfs4_client *clp = m->private; 3110 3111 /* XXX: alternatively, we could get/drop in seq start/stop */ 3112 drop_client(clp); 3113 return seq_release(inode, file); 3114 } 3115 3116 static const struct file_operations client_states_fops = { 3117 .open = client_states_open, 3118 .read = seq_read, 3119 .llseek = seq_lseek, 3120 .release = client_opens_release, 3121 }; 3122 3123 /* 3124 * Normally we refuse to destroy clients that are in use, but here the 3125 * administrator is telling us to just do it. We also want to wait 3126 * so the caller has a guarantee that the client's locks are gone by 3127 * the time the write returns: 3128 */ 3129 static void force_expire_client(struct nfs4_client *clp) 3130 { 3131 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 3132 bool already_expired; 3133 3134 trace_nfsd_clid_admin_expired(&clp->cl_clientid); 3135 3136 spin_lock(&nn->client_lock); 3137 clp->cl_time = 0; 3138 spin_unlock(&nn->client_lock); 3139 3140 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); 3141 spin_lock(&nn->client_lock); 3142 already_expired = list_empty(&clp->cl_lru); 3143 if (!already_expired) 3144 unhash_client_locked(clp); 3145 spin_unlock(&nn->client_lock); 3146 3147 if (!already_expired) 3148 expire_client(clp); 3149 else 3150 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); 3151 } 3152 3153 static ssize_t client_ctl_write(struct file *file, const char __user *buf, 3154 size_t size, loff_t *pos) 3155 { 3156 char *data; 3157 struct nfs4_client *clp; 3158 3159 data = simple_transaction_get(file, buf, size); 3160 if (IS_ERR(data)) 3161 return PTR_ERR(data); 3162 if (size != 7 || 0 != memcmp(data, "expire\n", 7)) 3163 return -EINVAL; 3164 clp = get_nfsdfs_clp(file_inode(file)); 3165 if (!clp) 3166 return -ENXIO; 3167 force_expire_client(clp); 3168 drop_client(clp); 3169 return 7; 3170 } 3171 3172 static const struct file_operations client_ctl_fops = { 3173 .write = client_ctl_write, 3174 .release = simple_transaction_release, 3175 }; 3176 3177 static const struct tree_descr client_files[] = { 3178 [0] = {"info", &client_info_fops, S_IRUSR}, 3179 [1] = {"states", &client_states_fops, S_IRUSR}, 3180 [2] = {"ctl", &client_ctl_fops, S_IWUSR}, 3181 [3] = {""}, 3182 }; 3183 3184 static int 3185 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb, 3186 struct rpc_task *task) 3187 { 3188 trace_nfsd_cb_recall_any_done(cb, task); 3189 switch (task->tk_status) { 3190 case -NFS4ERR_DELAY: 3191 rpc_delay(task, 2 * HZ); 3192 return 0; 3193 default: 3194 return 1; 3195 } 3196 } 3197 3198 static void 3199 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb) 3200 { 3201 struct nfs4_client *clp = cb->cb_clp; 3202 3203 drop_client(clp); 3204 } 3205 3206 static int 3207 nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task) 3208 { 3209 struct nfs4_cb_fattr *ncf = 3210 container_of(cb, struct nfs4_cb_fattr, ncf_getattr); 3211 struct nfs4_delegation *dp = 3212 container_of(ncf, struct nfs4_delegation, dl_cb_fattr); 3213 3214 trace_nfsd_cb_getattr_done(&dp->dl_stid.sc_stateid, task); 3215 ncf->ncf_cb_status = task->tk_status; 3216 switch (task->tk_status) { 3217 case -NFS4ERR_DELAY: 3218 rpc_delay(task, 2 * HZ); 3219 return 0; 3220 default: 3221 return 1; 3222 } 3223 } 3224 3225 static void 3226 nfsd4_cb_getattr_release(struct nfsd4_callback *cb) 3227 { 3228 struct nfs4_cb_fattr *ncf = 3229 container_of(cb, struct nfs4_cb_fattr, ncf_getattr); 3230 struct nfs4_delegation *dp = 3231 container_of(ncf, struct nfs4_delegation, dl_cb_fattr); 3232 3233 nfs4_put_stid(&dp->dl_stid); 3234 } 3235 3236 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = { 3237 .done = nfsd4_cb_recall_any_done, 3238 .release = nfsd4_cb_recall_any_release, 3239 .opcode = OP_CB_RECALL_ANY, 3240 }; 3241 3242 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = { 3243 .done = nfsd4_cb_getattr_done, 3244 .release = nfsd4_cb_getattr_release, 3245 .opcode = OP_CB_GETATTR, 3246 }; 3247 3248 static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf) 3249 { 3250 struct nfs4_delegation *dp = 3251 container_of(ncf, struct nfs4_delegation, dl_cb_fattr); 3252 3253 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &ncf->ncf_getattr.cb_flags)) 3254 return; 3255 3256 /* set to proper status when nfsd4_cb_getattr_done runs */ 3257 ncf->ncf_cb_status = NFS4ERR_IO; 3258 3259 /* ensure that wake_bit is done when RUNNING is cleared */ 3260 set_bit(NFSD4_CALLBACK_WAKE, &ncf->ncf_getattr.cb_flags); 3261 3262 refcount_inc(&dp->dl_stid.sc_count); 3263 nfsd4_run_cb(&ncf->ncf_getattr); 3264 } 3265 3266 static struct nfs4_client *create_client(struct xdr_netobj name, 3267 struct svc_rqst *rqstp, nfs4_verifier *verf) 3268 { 3269 struct nfs4_client *clp; 3270 struct sockaddr *sa = svc_addr(rqstp); 3271 int ret; 3272 struct net *net = SVC_NET(rqstp); 3273 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 3274 struct dentry *dentries[ARRAY_SIZE(client_files)]; 3275 3276 clp = alloc_client(name, nn); 3277 if (clp == NULL) 3278 return NULL; 3279 3280 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); 3281 if (ret) { 3282 free_client(clp); 3283 return NULL; 3284 } 3285 gen_clid(clp, nn); 3286 kref_init(&clp->cl_nfsdfs.cl_ref); 3287 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); 3288 clp->cl_time = ktime_get_boottime_seconds(); 3289 copy_verf(clp, verf); 3290 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); 3291 clp->cl_cb_session = NULL; 3292 clp->net = net; 3293 clp->cl_nfsd_dentry = nfsd_client_mkdir( 3294 nn, &clp->cl_nfsdfs, 3295 clp->cl_clientid.cl_id - nn->clientid_base, 3296 client_files, dentries); 3297 clp->cl_nfsd_info_dentry = dentries[0]; 3298 if (!clp->cl_nfsd_dentry) { 3299 free_client(clp); 3300 return NULL; 3301 } 3302 clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL); 3303 if (!clp->cl_ra) { 3304 free_client(clp); 3305 return NULL; 3306 } 3307 clp->cl_ra_time = 0; 3308 nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops, 3309 NFSPROC4_CLNT_CB_RECALL_ANY); 3310 return clp; 3311 } 3312 3313 static void 3314 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) 3315 { 3316 struct rb_node **new = &(root->rb_node), *parent = NULL; 3317 struct nfs4_client *clp; 3318 3319 while (*new) { 3320 clp = rb_entry(*new, struct nfs4_client, cl_namenode); 3321 parent = *new; 3322 3323 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) 3324 new = &((*new)->rb_left); 3325 else 3326 new = &((*new)->rb_right); 3327 } 3328 3329 rb_link_node(&new_clp->cl_namenode, parent, new); 3330 rb_insert_color(&new_clp->cl_namenode, root); 3331 } 3332 3333 static struct nfs4_client * 3334 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) 3335 { 3336 int cmp; 3337 struct rb_node *node = root->rb_node; 3338 struct nfs4_client *clp; 3339 3340 while (node) { 3341 clp = rb_entry(node, struct nfs4_client, cl_namenode); 3342 cmp = compare_blob(&clp->cl_name, name); 3343 if (cmp > 0) 3344 node = node->rb_left; 3345 else if (cmp < 0) 3346 node = node->rb_right; 3347 else 3348 return clp; 3349 } 3350 return NULL; 3351 } 3352 3353 static void 3354 add_to_unconfirmed(struct nfs4_client *clp) 3355 { 3356 unsigned int idhashval; 3357 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 3358 3359 lockdep_assert_held(&nn->client_lock); 3360 3361 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); 3362 add_clp_to_name_tree(clp, &nn->unconf_name_tree); 3363 idhashval = clientid_hashval(clp->cl_clientid.cl_id); 3364 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); 3365 renew_client_locked(clp); 3366 } 3367 3368 static void 3369 move_to_confirmed(struct nfs4_client *clp) 3370 { 3371 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); 3372 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 3373 3374 lockdep_assert_held(&nn->client_lock); 3375 3376 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); 3377 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); 3378 add_clp_to_name_tree(clp, &nn->conf_name_tree); 3379 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); 3380 trace_nfsd_clid_confirmed(&clp->cl_clientid); 3381 renew_client_locked(clp); 3382 } 3383 3384 static struct nfs4_client * 3385 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) 3386 { 3387 struct nfs4_client *clp; 3388 unsigned int idhashval = clientid_hashval(clid->cl_id); 3389 3390 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { 3391 if (same_clid(&clp->cl_clientid, clid)) { 3392 if ((bool)clp->cl_minorversion != sessions) 3393 return NULL; 3394 renew_client_locked(clp); 3395 return clp; 3396 } 3397 } 3398 return NULL; 3399 } 3400 3401 static struct nfs4_client * 3402 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) 3403 { 3404 struct list_head *tbl = nn->conf_id_hashtbl; 3405 3406 lockdep_assert_held(&nn->client_lock); 3407 return find_client_in_id_table(tbl, clid, sessions); 3408 } 3409 3410 static struct nfs4_client * 3411 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) 3412 { 3413 struct list_head *tbl = nn->unconf_id_hashtbl; 3414 3415 lockdep_assert_held(&nn->client_lock); 3416 return find_client_in_id_table(tbl, clid, sessions); 3417 } 3418 3419 static bool clp_used_exchangeid(struct nfs4_client *clp) 3420 { 3421 return clp->cl_exchange_flags != 0; 3422 } 3423 3424 static struct nfs4_client * 3425 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) 3426 { 3427 lockdep_assert_held(&nn->client_lock); 3428 return find_clp_in_name_tree(name, &nn->conf_name_tree); 3429 } 3430 3431 static struct nfs4_client * 3432 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) 3433 { 3434 lockdep_assert_held(&nn->client_lock); 3435 return find_clp_in_name_tree(name, &nn->unconf_name_tree); 3436 } 3437 3438 static void 3439 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) 3440 { 3441 struct nfs4_cb_conn *conn = &clp->cl_cb_conn; 3442 struct sockaddr *sa = svc_addr(rqstp); 3443 u32 scopeid = rpc_get_scope_id(sa); 3444 unsigned short expected_family; 3445 3446 /* Currently, we only support tcp and tcp6 for the callback channel */ 3447 if (se->se_callback_netid_len == 3 && 3448 !memcmp(se->se_callback_netid_val, "tcp", 3)) 3449 expected_family = AF_INET; 3450 else if (se->se_callback_netid_len == 4 && 3451 !memcmp(se->se_callback_netid_val, "tcp6", 4)) 3452 expected_family = AF_INET6; 3453 else 3454 goto out_err; 3455 3456 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, 3457 se->se_callback_addr_len, 3458 (struct sockaddr *)&conn->cb_addr, 3459 sizeof(conn->cb_addr)); 3460 3461 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) 3462 goto out_err; 3463 3464 if (conn->cb_addr.ss_family == AF_INET6) 3465 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; 3466 3467 conn->cb_prog = se->se_callback_prog; 3468 conn->cb_ident = se->se_callback_ident; 3469 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); 3470 trace_nfsd_cb_args(clp, conn); 3471 return; 3472 out_err: 3473 conn->cb_addr.ss_family = AF_UNSPEC; 3474 conn->cb_addrlen = 0; 3475 trace_nfsd_cb_nodelegs(clp); 3476 return; 3477 } 3478 3479 /* 3480 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. 3481 */ 3482 static void 3483 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) 3484 { 3485 struct xdr_buf *buf = resp->xdr->buf; 3486 struct nfsd4_slot *slot = resp->cstate.slot; 3487 unsigned int base; 3488 3489 dprintk("--> %s slot %p\n", __func__, slot); 3490 3491 slot->sl_flags |= NFSD4_SLOT_INITIALIZED; 3492 slot->sl_opcnt = resp->opcnt; 3493 slot->sl_status = resp->cstate.status; 3494 free_svc_cred(&slot->sl_cred); 3495 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); 3496 3497 if (!nfsd4_cache_this(resp)) { 3498 slot->sl_flags &= ~NFSD4_SLOT_CACHED; 3499 return; 3500 } 3501 slot->sl_flags |= NFSD4_SLOT_CACHED; 3502 3503 base = resp->cstate.data_offset; 3504 slot->sl_datalen = buf->len - base; 3505 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) 3506 WARN(1, "%s: sessions DRC could not cache compound\n", 3507 __func__); 3508 return; 3509 } 3510 3511 /* 3512 * Encode the replay sequence operation from the slot values. 3513 * If cachethis is FALSE encode the uncached rep error on the next 3514 * operation which sets resp->p and increments resp->opcnt for 3515 * nfs4svc_encode_compoundres. 3516 * 3517 */ 3518 static __be32 3519 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, 3520 struct nfsd4_compoundres *resp) 3521 { 3522 struct nfsd4_op *op; 3523 struct nfsd4_slot *slot = resp->cstate.slot; 3524 3525 /* Encode the replayed sequence operation */ 3526 op = &args->ops[resp->opcnt - 1]; 3527 nfsd4_encode_operation(resp, op); 3528 3529 if (slot->sl_flags & NFSD4_SLOT_CACHED) 3530 return op->status; 3531 if (args->opcnt == 1) { 3532 /* 3533 * The original operation wasn't a solo sequence--we 3534 * always cache those--so this retry must not match the 3535 * original: 3536 */ 3537 op->status = nfserr_seq_false_retry; 3538 } else { 3539 op = &args->ops[resp->opcnt++]; 3540 op->status = nfserr_retry_uncached_rep; 3541 nfsd4_encode_operation(resp, op); 3542 } 3543 return op->status; 3544 } 3545 3546 /* 3547 * The sequence operation is not cached because we can use the slot and 3548 * session values. 3549 */ 3550 static __be32 3551 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, 3552 struct nfsd4_sequence *seq) 3553 { 3554 struct nfsd4_slot *slot = resp->cstate.slot; 3555 struct xdr_stream *xdr = resp->xdr; 3556 __be32 *p; 3557 __be32 status; 3558 3559 dprintk("--> %s slot %p\n", __func__, slot); 3560 3561 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); 3562 if (status) 3563 return status; 3564 3565 p = xdr_reserve_space(xdr, slot->sl_datalen); 3566 if (!p) { 3567 WARN_ON_ONCE(1); 3568 return nfserr_serverfault; 3569 } 3570 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); 3571 xdr_commit_encode(xdr); 3572 3573 resp->opcnt = slot->sl_opcnt; 3574 return slot->sl_status; 3575 } 3576 3577 /* 3578 * Set the exchange_id flags returned by the server. 3579 */ 3580 static void 3581 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) 3582 { 3583 #ifdef CONFIG_NFSD_PNFS 3584 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; 3585 #else 3586 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; 3587 #endif 3588 3589 /* Referrals are supported, Migration is not. */ 3590 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; 3591 3592 /* set the wire flags to return to client. */ 3593 clid->flags = new->cl_exchange_flags; 3594 } 3595 3596 static bool client_has_openowners(struct nfs4_client *clp) 3597 { 3598 struct nfs4_openowner *oo; 3599 3600 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { 3601 if (!list_empty(&oo->oo_owner.so_stateids)) 3602 return true; 3603 } 3604 return false; 3605 } 3606 3607 static bool client_has_state(struct nfs4_client *clp) 3608 { 3609 return client_has_openowners(clp) 3610 #ifdef CONFIG_NFSD_PNFS 3611 || !list_empty(&clp->cl_lo_states) 3612 #endif 3613 || !list_empty(&clp->cl_delegations) 3614 || !list_empty(&clp->cl_sessions) 3615 || nfsd4_has_active_async_copies(clp); 3616 } 3617 3618 static __be32 copy_impl_id(struct nfs4_client *clp, 3619 struct nfsd4_exchange_id *exid) 3620 { 3621 if (!exid->nii_domain.data) 3622 return 0; 3623 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); 3624 if (!clp->cl_nii_domain.data) 3625 return nfserr_jukebox; 3626 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); 3627 if (!clp->cl_nii_name.data) 3628 return nfserr_jukebox; 3629 clp->cl_nii_time = exid->nii_time; 3630 return 0; 3631 } 3632 3633 __be32 3634 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 3635 union nfsd4_op_u *u) 3636 { 3637 struct nfsd4_exchange_id *exid = &u->exchange_id; 3638 struct nfs4_client *conf, *new; 3639 struct nfs4_client *unconf = NULL; 3640 __be32 status; 3641 char addr_str[INET6_ADDRSTRLEN]; 3642 nfs4_verifier verf = exid->verifier; 3643 struct sockaddr *sa = svc_addr(rqstp); 3644 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; 3645 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 3646 3647 rpc_ntop(sa, addr_str, sizeof(addr_str)); 3648 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " 3649 "ip_addr=%s flags %x, spa_how %u\n", 3650 __func__, rqstp, exid, exid->clname.len, exid->clname.data, 3651 addr_str, exid->flags, exid->spa_how); 3652 3653 exid->server_impl_name = kasprintf(GFP_KERNEL, "%s %s %s %s", 3654 utsname()->sysname, utsname()->release, 3655 utsname()->version, utsname()->machine); 3656 if (!exid->server_impl_name) 3657 return nfserr_jukebox; 3658 3659 if (exid->flags & ~EXCHGID4_FLAG_MASK_A) 3660 return nfserr_inval; 3661 3662 new = create_client(exid->clname, rqstp, &verf); 3663 if (new == NULL) 3664 return nfserr_jukebox; 3665 status = copy_impl_id(new, exid); 3666 if (status) 3667 goto out_nolock; 3668 3669 switch (exid->spa_how) { 3670 case SP4_MACH_CRED: 3671 exid->spo_must_enforce[0] = 0; 3672 exid->spo_must_enforce[1] = ( 3673 1 << (OP_BIND_CONN_TO_SESSION - 32) | 3674 1 << (OP_EXCHANGE_ID - 32) | 3675 1 << (OP_CREATE_SESSION - 32) | 3676 1 << (OP_DESTROY_SESSION - 32) | 3677 1 << (OP_DESTROY_CLIENTID - 32)); 3678 3679 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | 3680 1 << (OP_OPEN_DOWNGRADE) | 3681 1 << (OP_LOCKU) | 3682 1 << (OP_DELEGRETURN)); 3683 3684 exid->spo_must_allow[1] &= ( 3685 1 << (OP_TEST_STATEID - 32) | 3686 1 << (OP_FREE_STATEID - 32)); 3687 if (!svc_rqst_integrity_protected(rqstp)) { 3688 status = nfserr_inval; 3689 goto out_nolock; 3690 } 3691 /* 3692 * Sometimes userspace doesn't give us a principal. 3693 * Which is a bug, really. Anyway, we can't enforce 3694 * MACH_CRED in that case, better to give up now: 3695 */ 3696 if (!new->cl_cred.cr_principal && 3697 !new->cl_cred.cr_raw_principal) { 3698 status = nfserr_serverfault; 3699 goto out_nolock; 3700 } 3701 new->cl_mach_cred = true; 3702 break; 3703 case SP4_NONE: 3704 break; 3705 default: /* checked by xdr code */ 3706 WARN_ON_ONCE(1); 3707 fallthrough; 3708 case SP4_SSV: 3709 status = nfserr_encr_alg_unsupp; 3710 goto out_nolock; 3711 } 3712 3713 /* Cases below refer to rfc 5661 section 18.35.4: */ 3714 spin_lock(&nn->client_lock); 3715 conf = find_confirmed_client_by_name(&exid->clname, nn); 3716 if (conf) { 3717 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); 3718 bool verfs_match = same_verf(&verf, &conf->cl_verifier); 3719 3720 if (update) { 3721 if (!clp_used_exchangeid(conf)) { /* buggy client */ 3722 status = nfserr_inval; 3723 goto out; 3724 } 3725 if (!nfsd4_mach_creds_match(conf, rqstp)) { 3726 status = nfserr_wrong_cred; 3727 goto out; 3728 } 3729 if (!creds_match) { /* case 9 */ 3730 status = nfserr_perm; 3731 goto out; 3732 } 3733 if (!verfs_match) { /* case 8 */ 3734 status = nfserr_not_same; 3735 goto out; 3736 } 3737 /* case 6 */ 3738 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; 3739 trace_nfsd_clid_confirmed_r(conf); 3740 goto out_copy; 3741 } 3742 if (!creds_match) { /* case 3 */ 3743 if (client_has_state(conf)) { 3744 status = nfserr_clid_inuse; 3745 trace_nfsd_clid_cred_mismatch(conf, rqstp); 3746 goto out; 3747 } 3748 goto out_new; 3749 } 3750 if (verfs_match) { /* case 2 */ 3751 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; 3752 trace_nfsd_clid_confirmed_r(conf); 3753 goto out_copy; 3754 } 3755 /* case 5, client reboot */ 3756 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf); 3757 conf = NULL; 3758 goto out_new; 3759 } 3760 3761 if (update) { /* case 7 */ 3762 status = nfserr_noent; 3763 goto out; 3764 } 3765 3766 unconf = find_unconfirmed_client_by_name(&exid->clname, nn); 3767 if (unconf) /* case 4, possible retry or client restart */ 3768 unhash_client_locked(unconf); 3769 3770 /* case 1, new owner ID */ 3771 trace_nfsd_clid_fresh(new); 3772 3773 out_new: 3774 if (conf) { 3775 status = mark_client_expired_locked(conf); 3776 if (status) 3777 goto out; 3778 trace_nfsd_clid_replaced(&conf->cl_clientid); 3779 } 3780 new->cl_minorversion = cstate->minorversion; 3781 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; 3782 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; 3783 3784 /* Contrived initial CREATE_SESSION response */ 3785 new->cl_cs_slot.sl_status = nfserr_seq_misordered; 3786 3787 add_to_unconfirmed(new); 3788 swap(new, conf); 3789 out_copy: 3790 exid->clientid.cl_boot = conf->cl_clientid.cl_boot; 3791 exid->clientid.cl_id = conf->cl_clientid.cl_id; 3792 3793 exid->seqid = conf->cl_cs_slot.sl_seqid + 1; 3794 nfsd4_set_ex_flags(conf, exid); 3795 3796 exid->nii_domain.len = sizeof("kernel.org") - 1; 3797 exid->nii_domain.data = "kernel.org"; 3798 3799 /* 3800 * Note that RFC 8881 places no length limit on 3801 * nii_name, but this implementation permits no 3802 * more than NFS4_OPAQUE_LIMIT bytes. 3803 */ 3804 exid->nii_name.len = strlen(exid->server_impl_name); 3805 if (exid->nii_name.len > NFS4_OPAQUE_LIMIT) 3806 exid->nii_name.len = NFS4_OPAQUE_LIMIT; 3807 exid->nii_name.data = exid->server_impl_name; 3808 3809 /* just send zeros - the date is in nii_name */ 3810 exid->nii_time.tv_sec = 0; 3811 exid->nii_time.tv_nsec = 0; 3812 3813 dprintk("nfsd4_exchange_id seqid %d flags %x\n", 3814 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); 3815 status = nfs_ok; 3816 3817 out: 3818 spin_unlock(&nn->client_lock); 3819 out_nolock: 3820 if (new) 3821 expire_client(new); 3822 if (unconf) { 3823 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); 3824 expire_client(unconf); 3825 } 3826 return status; 3827 } 3828 3829 void 3830 nfsd4_exchange_id_release(union nfsd4_op_u *u) 3831 { 3832 struct nfsd4_exchange_id *exid = &u->exchange_id; 3833 3834 kfree(exid->server_impl_name); 3835 } 3836 3837 static __be32 check_slot_seqid(u32 seqid, u32 slot_seqid, u8 flags) 3838 { 3839 /* The slot is in use, and no response has been sent. */ 3840 if (flags & NFSD4_SLOT_INUSE) { 3841 if (seqid == slot_seqid) 3842 return nfserr_jukebox; 3843 else 3844 return nfserr_seq_misordered; 3845 } 3846 /* Note unsigned 32-bit arithmetic handles wraparound: */ 3847 if (likely(seqid == slot_seqid + 1)) 3848 return nfs_ok; 3849 if ((flags & NFSD4_SLOT_REUSED) && seqid == 1) 3850 return nfs_ok; 3851 if (seqid == slot_seqid) 3852 return nfserr_replay_cache; 3853 return nfserr_seq_misordered; 3854 } 3855 3856 /* 3857 * Cache the create session result into the create session single DRC 3858 * slot cache by saving the xdr structure. sl_seqid has been set. 3859 * Do this for solo or embedded create session operations. 3860 */ 3861 static void 3862 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, 3863 struct nfsd4_clid_slot *slot, __be32 nfserr) 3864 { 3865 slot->sl_status = nfserr; 3866 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); 3867 } 3868 3869 static __be32 3870 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, 3871 struct nfsd4_clid_slot *slot) 3872 { 3873 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); 3874 return slot->sl_status; 3875 } 3876 3877 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ 3878 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ 3879 1 + /* MIN tag is length with zero, only length */ \ 3880 3 + /* version, opcount, opcode */ \ 3881 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ 3882 /* seqid, slotID, slotID, cache */ \ 3883 4 ) * sizeof(__be32)) 3884 3885 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ 3886 2 + /* verifier: AUTH_NULL, length 0 */\ 3887 1 + /* status */ \ 3888 1 + /* MIN tag is length with zero, only length */ \ 3889 3 + /* opcount, opcode, opstatus*/ \ 3890 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ 3891 /* seqid, slotID, slotID, slotID, status */ \ 3892 5 ) * sizeof(__be32)) 3893 3894 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) 3895 { 3896 u32 maxrpc = nn->nfsd_serv->sv_max_mesg; 3897 3898 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) 3899 return nfserr_toosmall; 3900 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) 3901 return nfserr_toosmall; 3902 ca->headerpadsz = 0; 3903 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); 3904 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); 3905 ca->maxresp_cached = min_t(u32, ca->maxresp_cached, 3906 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); 3907 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); 3908 3909 return nfs_ok; 3910 } 3911 3912 /* 3913 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. 3914 * These are based on similar macros in linux/sunrpc/msg_prot.h . 3915 */ 3916 #define RPC_MAX_HEADER_WITH_AUTH_SYS \ 3917 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) 3918 3919 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ 3920 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) 3921 3922 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ 3923 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) 3924 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ 3925 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ 3926 sizeof(__be32)) 3927 3928 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) 3929 { 3930 ca->headerpadsz = 0; 3931 3932 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) 3933 return nfserr_toosmall; 3934 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) 3935 return nfserr_toosmall; 3936 ca->maxresp_cached = 0; 3937 if (ca->maxops < 2) 3938 return nfserr_toosmall; 3939 3940 return nfs_ok; 3941 } 3942 3943 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) 3944 { 3945 switch (cbs->flavor) { 3946 case RPC_AUTH_NULL: 3947 case RPC_AUTH_UNIX: 3948 return nfs_ok; 3949 default: 3950 /* 3951 * GSS case: the spec doesn't allow us to return this 3952 * error. But it also doesn't allow us not to support 3953 * GSS. 3954 * I'd rather this fail hard than return some error the 3955 * client might think it can already handle: 3956 */ 3957 return nfserr_encr_alg_unsupp; 3958 } 3959 } 3960 3961 __be32 3962 nfsd4_create_session(struct svc_rqst *rqstp, 3963 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 3964 { 3965 struct nfsd4_create_session *cr_ses = &u->create_session; 3966 struct sockaddr *sa = svc_addr(rqstp); 3967 struct nfs4_client *conf, *unconf; 3968 struct nfsd4_clid_slot *cs_slot; 3969 struct nfs4_client *old = NULL; 3970 struct nfsd4_session *new; 3971 struct nfsd4_conn *conn; 3972 __be32 status = 0; 3973 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 3974 3975 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) 3976 return nfserr_inval; 3977 status = nfsd4_check_cb_sec(&cr_ses->cb_sec); 3978 if (status) 3979 return status; 3980 status = check_forechannel_attrs(&cr_ses->fore_channel, nn); 3981 if (status) 3982 return status; 3983 status = check_backchannel_attrs(&cr_ses->back_channel); 3984 if (status) 3985 goto out_err; 3986 status = nfserr_jukebox; 3987 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); 3988 if (!new) 3989 goto out_err; 3990 conn = alloc_conn_from_crses(rqstp, cr_ses); 3991 if (!conn) 3992 goto out_free_session; 3993 3994 spin_lock(&nn->client_lock); 3995 3996 /* RFC 8881 Section 18.36.4 Phase 1: Client record look-up. */ 3997 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); 3998 conf = find_confirmed_client(&cr_ses->clientid, true, nn); 3999 if (!conf && !unconf) { 4000 status = nfserr_stale_clientid; 4001 goto out_free_conn; 4002 } 4003 4004 /* RFC 8881 Section 18.36.4 Phase 2: Sequence ID processing. */ 4005 if (conf) { 4006 cs_slot = &conf->cl_cs_slot; 4007 trace_nfsd_slot_seqid_conf(conf, cr_ses); 4008 } else { 4009 cs_slot = &unconf->cl_cs_slot; 4010 trace_nfsd_slot_seqid_unconf(unconf, cr_ses); 4011 } 4012 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); 4013 switch (status) { 4014 case nfs_ok: 4015 cs_slot->sl_seqid++; 4016 cr_ses->seqid = cs_slot->sl_seqid; 4017 break; 4018 case nfserr_replay_cache: 4019 status = nfsd4_replay_create_session(cr_ses, cs_slot); 4020 fallthrough; 4021 case nfserr_jukebox: 4022 /* The server MUST NOT cache NFS4ERR_DELAY */ 4023 goto out_free_conn; 4024 default: 4025 goto out_cache_error; 4026 } 4027 4028 /* RFC 8881 Section 18.36.4 Phase 3: Client ID confirmation. */ 4029 if (conf) { 4030 status = nfserr_wrong_cred; 4031 if (!nfsd4_mach_creds_match(conf, rqstp)) 4032 goto out_cache_error; 4033 } else { 4034 status = nfserr_clid_inuse; 4035 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || 4036 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { 4037 trace_nfsd_clid_cred_mismatch(unconf, rqstp); 4038 goto out_cache_error; 4039 } 4040 status = nfserr_wrong_cred; 4041 if (!nfsd4_mach_creds_match(unconf, rqstp)) 4042 goto out_cache_error; 4043 old = find_confirmed_client_by_name(&unconf->cl_name, nn); 4044 if (old) { 4045 status = mark_client_expired_locked(old); 4046 if (status) 4047 goto out_expired_error; 4048 trace_nfsd_clid_replaced(&old->cl_clientid); 4049 } 4050 move_to_confirmed(unconf); 4051 conf = unconf; 4052 } 4053 4054 /* RFC 8881 Section 18.36.4 Phase 4: Session creation. */ 4055 status = nfs_ok; 4056 /* Persistent sessions are not supported */ 4057 cr_ses->flags &= ~SESSION4_PERSIST; 4058 /* Upshifting from TCP to RDMA is not supported */ 4059 cr_ses->flags &= ~SESSION4_RDMA; 4060 /* Report the correct number of backchannel slots */ 4061 cr_ses->back_channel.maxreqs = new->se_cb_highest_slot + 1; 4062 4063 init_session(rqstp, new, conf, cr_ses); 4064 nfsd4_get_session_locked(new); 4065 4066 memcpy(cr_ses->sessionid.data, new->se_sessionid.data, 4067 NFS4_MAX_SESSIONID_LEN); 4068 4069 /* cache solo and embedded create sessions under the client_lock */ 4070 nfsd4_cache_create_session(cr_ses, cs_slot, status); 4071 spin_unlock(&nn->client_lock); 4072 if (conf == unconf) 4073 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); 4074 /* init connection and backchannel */ 4075 nfsd4_init_conn(rqstp, conn, new); 4076 nfsd4_put_session(new); 4077 if (old) 4078 expire_client(old); 4079 return status; 4080 4081 out_expired_error: 4082 /* 4083 * Revert the slot seq_nr change so the server will process 4084 * the client's resend instead of returning a cached response. 4085 */ 4086 if (status == nfserr_jukebox) { 4087 cs_slot->sl_seqid--; 4088 cr_ses->seqid = cs_slot->sl_seqid; 4089 goto out_free_conn; 4090 } 4091 out_cache_error: 4092 nfsd4_cache_create_session(cr_ses, cs_slot, status); 4093 out_free_conn: 4094 spin_unlock(&nn->client_lock); 4095 free_conn(conn); 4096 out_free_session: 4097 __free_session(new); 4098 out_err: 4099 return status; 4100 } 4101 4102 static __be32 nfsd4_map_bcts_dir(u32 *dir) 4103 { 4104 switch (*dir) { 4105 case NFS4_CDFC4_FORE: 4106 case NFS4_CDFC4_BACK: 4107 return nfs_ok; 4108 case NFS4_CDFC4_FORE_OR_BOTH: 4109 case NFS4_CDFC4_BACK_OR_BOTH: 4110 *dir = NFS4_CDFC4_BOTH; 4111 return nfs_ok; 4112 } 4113 return nfserr_inval; 4114 } 4115 4116 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, 4117 struct nfsd4_compound_state *cstate, 4118 union nfsd4_op_u *u) 4119 { 4120 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; 4121 struct nfsd4_session *session = cstate->session; 4122 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4123 __be32 status; 4124 4125 status = nfsd4_check_cb_sec(&bc->bc_cb_sec); 4126 if (status) 4127 return status; 4128 spin_lock(&nn->client_lock); 4129 session->se_cb_prog = bc->bc_cb_program; 4130 session->se_cb_sec = bc->bc_cb_sec; 4131 spin_unlock(&nn->client_lock); 4132 4133 nfsd4_probe_callback(session->se_client); 4134 4135 return nfs_ok; 4136 } 4137 4138 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) 4139 { 4140 struct nfsd4_conn *c; 4141 4142 list_for_each_entry(c, &s->se_conns, cn_persession) { 4143 if (c->cn_xprt == xpt) { 4144 return c; 4145 } 4146 } 4147 return NULL; 4148 } 4149 4150 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst, 4151 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn) 4152 { 4153 struct nfs4_client *clp = session->se_client; 4154 struct svc_xprt *xpt = rqst->rq_xprt; 4155 struct nfsd4_conn *c; 4156 __be32 status; 4157 4158 /* Following the last paragraph of RFC 5661 Section 18.34.3: */ 4159 spin_lock(&clp->cl_lock); 4160 c = __nfsd4_find_conn(xpt, session); 4161 if (!c) 4162 status = nfserr_noent; 4163 else if (req == c->cn_flags) 4164 status = nfs_ok; 4165 else if (req == NFS4_CDFC4_FORE_OR_BOTH && 4166 c->cn_flags != NFS4_CDFC4_BACK) 4167 status = nfs_ok; 4168 else if (req == NFS4_CDFC4_BACK_OR_BOTH && 4169 c->cn_flags != NFS4_CDFC4_FORE) 4170 status = nfs_ok; 4171 else 4172 status = nfserr_inval; 4173 spin_unlock(&clp->cl_lock); 4174 if (status == nfs_ok && conn) 4175 *conn = c; 4176 return status; 4177 } 4178 4179 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, 4180 struct nfsd4_compound_state *cstate, 4181 union nfsd4_op_u *u) 4182 { 4183 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session; 4184 __be32 status; 4185 struct nfsd4_conn *conn; 4186 struct nfsd4_session *session; 4187 struct net *net = SVC_NET(rqstp); 4188 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4189 4190 if (!nfsd4_last_compound_op(rqstp)) 4191 return nfserr_not_only_op; 4192 spin_lock(&nn->client_lock); 4193 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); 4194 spin_unlock(&nn->client_lock); 4195 if (!session) 4196 goto out_no_session; 4197 status = nfserr_wrong_cred; 4198 if (!nfsd4_mach_creds_match(session->se_client, rqstp)) 4199 goto out; 4200 status = nfsd4_match_existing_connection(rqstp, session, 4201 bcts->dir, &conn); 4202 if (status == nfs_ok) { 4203 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH || 4204 bcts->dir == NFS4_CDFC4_BACK) 4205 conn->cn_flags |= NFS4_CDFC4_BACK; 4206 nfsd4_probe_callback(session->se_client); 4207 goto out; 4208 } 4209 if (status == nfserr_inval) 4210 goto out; 4211 status = nfsd4_map_bcts_dir(&bcts->dir); 4212 if (status) 4213 goto out; 4214 conn = alloc_conn(rqstp, bcts->dir); 4215 status = nfserr_jukebox; 4216 if (!conn) 4217 goto out; 4218 nfsd4_init_conn(rqstp, conn, session); 4219 status = nfs_ok; 4220 out: 4221 nfsd4_put_session(session); 4222 out_no_session: 4223 return status; 4224 } 4225 4226 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid) 4227 { 4228 if (!cstate->session) 4229 return false; 4230 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); 4231 } 4232 4233 __be32 4234 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, 4235 union nfsd4_op_u *u) 4236 { 4237 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; 4238 struct nfsd4_session *ses; 4239 __be32 status; 4240 int ref_held_by_me = 0; 4241 struct net *net = SVC_NET(r); 4242 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4243 4244 status = nfserr_not_only_op; 4245 if (nfsd4_compound_in_session(cstate, sessionid)) { 4246 if (!nfsd4_last_compound_op(r)) 4247 goto out; 4248 ref_held_by_me++; 4249 } 4250 dump_sessionid(__func__, sessionid); 4251 spin_lock(&nn->client_lock); 4252 ses = find_in_sessionid_hashtbl(sessionid, net, &status); 4253 if (!ses) 4254 goto out_client_lock; 4255 status = nfserr_wrong_cred; 4256 if (!nfsd4_mach_creds_match(ses->se_client, r)) 4257 goto out_put_session; 4258 status = mark_session_dead_locked(ses, 1 + ref_held_by_me); 4259 if (status) 4260 goto out_put_session; 4261 unhash_session(ses); 4262 spin_unlock(&nn->client_lock); 4263 4264 nfsd4_probe_callback_sync(ses->se_client); 4265 4266 spin_lock(&nn->client_lock); 4267 status = nfs_ok; 4268 out_put_session: 4269 nfsd4_put_session_locked(ses); 4270 out_client_lock: 4271 spin_unlock(&nn->client_lock); 4272 out: 4273 return status; 4274 } 4275 4276 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) 4277 { 4278 struct nfs4_client *clp = ses->se_client; 4279 struct nfsd4_conn *c; 4280 __be32 status = nfs_ok; 4281 int ret; 4282 4283 spin_lock(&clp->cl_lock); 4284 c = __nfsd4_find_conn(new->cn_xprt, ses); 4285 if (c) 4286 goto out_free; 4287 status = nfserr_conn_not_bound_to_session; 4288 if (clp->cl_mach_cred) 4289 goto out_free; 4290 __nfsd4_hash_conn(new, ses); 4291 spin_unlock(&clp->cl_lock); 4292 ret = nfsd4_register_conn(new); 4293 if (ret) 4294 /* oops; xprt is already down: */ 4295 nfsd4_conn_lost(&new->cn_xpt_user); 4296 return nfs_ok; 4297 out_free: 4298 spin_unlock(&clp->cl_lock); 4299 free_conn(new); 4300 return status; 4301 } 4302 4303 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) 4304 { 4305 struct nfsd4_compoundargs *args = rqstp->rq_argp; 4306 4307 return args->opcnt > session->se_fchannel.maxops; 4308 } 4309 4310 static bool nfsd4_request_too_big(struct svc_rqst *rqstp, 4311 struct nfsd4_session *session) 4312 { 4313 struct xdr_buf *xb = &rqstp->rq_arg; 4314 4315 return xb->len > session->se_fchannel.maxreq_sz; 4316 } 4317 4318 static bool replay_matches_cache(struct svc_rqst *rqstp, 4319 struct nfsd4_sequence *seq, struct nfsd4_slot *slot) 4320 { 4321 struct nfsd4_compoundargs *argp = rqstp->rq_argp; 4322 4323 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != 4324 (bool)seq->cachethis) 4325 return false; 4326 /* 4327 * If there's an error then the reply can have fewer ops than 4328 * the call. 4329 */ 4330 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) 4331 return false; 4332 /* 4333 * But if we cached a reply with *more* ops than the call you're 4334 * sending us now, then this new call is clearly not really a 4335 * replay of the old one: 4336 */ 4337 if (slot->sl_opcnt > argp->opcnt) 4338 return false; 4339 /* This is the only check explicitly called by spec: */ 4340 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) 4341 return false; 4342 /* 4343 * There may be more comparisons we could actually do, but the 4344 * spec doesn't require us to catch every case where the calls 4345 * don't match (that would require caching the call as well as 4346 * the reply), so we don't bother. 4347 */ 4348 return true; 4349 } 4350 4351 __be32 4352 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 4353 union nfsd4_op_u *u) 4354 { 4355 struct nfsd4_sequence *seq = &u->sequence; 4356 struct nfsd4_compoundres *resp = rqstp->rq_resp; 4357 struct xdr_stream *xdr = resp->xdr; 4358 struct nfsd4_session *session; 4359 struct nfs4_client *clp; 4360 struct nfsd4_slot *slot; 4361 struct nfsd4_conn *conn; 4362 __be32 status; 4363 int buflen; 4364 struct net *net = SVC_NET(rqstp); 4365 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4366 4367 if (resp->opcnt != 1) 4368 return nfserr_sequence_pos; 4369 4370 /* 4371 * Will be either used or freed by nfsd4_sequence_check_conn 4372 * below. 4373 */ 4374 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); 4375 if (!conn) 4376 return nfserr_jukebox; 4377 4378 spin_lock(&nn->client_lock); 4379 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); 4380 if (!session) 4381 goto out_no_session; 4382 clp = session->se_client; 4383 4384 status = nfserr_too_many_ops; 4385 if (nfsd4_session_too_many_ops(rqstp, session)) 4386 goto out_put_session; 4387 4388 status = nfserr_req_too_big; 4389 if (nfsd4_request_too_big(rqstp, session)) 4390 goto out_put_session; 4391 4392 status = nfserr_badslot; 4393 if (seq->slotid >= session->se_fchannel.maxreqs) 4394 goto out_put_session; 4395 4396 slot = xa_load(&session->se_slots, seq->slotid); 4397 dprintk("%s: slotid %d\n", __func__, seq->slotid); 4398 4399 trace_nfsd_slot_seqid_sequence(clp, seq, slot); 4400 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_flags); 4401 if (status == nfserr_replay_cache) { 4402 status = nfserr_seq_misordered; 4403 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) 4404 goto out_put_session; 4405 status = nfserr_seq_false_retry; 4406 if (!replay_matches_cache(rqstp, seq, slot)) 4407 goto out_put_session; 4408 cstate->slot = slot; 4409 cstate->session = session; 4410 cstate->clp = clp; 4411 /* Return the cached reply status and set cstate->status 4412 * for nfsd4_proc_compound processing */ 4413 status = nfsd4_replay_cache_entry(resp, seq); 4414 cstate->status = nfserr_replay_cache; 4415 goto out; 4416 } 4417 if (status) 4418 goto out_put_session; 4419 4420 status = nfsd4_sequence_check_conn(conn, session); 4421 conn = NULL; 4422 if (status) 4423 goto out_put_session; 4424 4425 if (session->se_target_maxslots < session->se_fchannel.maxreqs && 4426 slot->sl_generation == session->se_slot_gen && 4427 seq->maxslots <= session->se_target_maxslots) 4428 /* Client acknowledged our reduce maxreqs */ 4429 free_session_slots(session, session->se_target_maxslots); 4430 4431 buflen = (seq->cachethis) ? 4432 session->se_fchannel.maxresp_cached : 4433 session->se_fchannel.maxresp_sz; 4434 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : 4435 nfserr_rep_too_big; 4436 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) 4437 goto out_put_session; 4438 svc_reserve_auth(rqstp, buflen); 4439 4440 status = nfs_ok; 4441 /* Success! accept new slot seqid */ 4442 slot->sl_seqid = seq->seqid; 4443 slot->sl_flags &= ~NFSD4_SLOT_REUSED; 4444 slot->sl_flags |= NFSD4_SLOT_INUSE; 4445 slot->sl_generation = session->se_slot_gen; 4446 if (seq->cachethis) 4447 slot->sl_flags |= NFSD4_SLOT_CACHETHIS; 4448 else 4449 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; 4450 4451 cstate->slot = slot; 4452 cstate->session = session; 4453 cstate->clp = clp; 4454 4455 /* 4456 * If the client ever uses the highest available slot, 4457 * gently try to allocate another 20%. This allows 4458 * fairly quick growth without grossly over-shooting what 4459 * the client might use. 4460 */ 4461 if (seq->slotid == session->se_fchannel.maxreqs - 1 && 4462 session->se_target_maxslots >= session->se_fchannel.maxreqs && 4463 session->se_fchannel.maxreqs < NFSD_MAX_SLOTS_PER_SESSION) { 4464 int s = session->se_fchannel.maxreqs; 4465 int cnt = DIV_ROUND_UP(s, 5); 4466 void *prev_slot; 4467 4468 do { 4469 /* 4470 * GFP_NOWAIT both allows allocation under a 4471 * spinlock, and only succeeds if there is 4472 * plenty of memory. 4473 */ 4474 slot = nfsd4_alloc_slot(&session->se_fchannel, s, 4475 GFP_NOWAIT); 4476 prev_slot = xa_load(&session->se_slots, s); 4477 if (xa_is_value(prev_slot) && slot) { 4478 slot->sl_seqid = xa_to_value(prev_slot); 4479 slot->sl_flags |= NFSD4_SLOT_REUSED; 4480 } 4481 if (slot && 4482 !xa_is_err(xa_store(&session->se_slots, s, slot, 4483 GFP_NOWAIT))) { 4484 s += 1; 4485 session->se_fchannel.maxreqs = s; 4486 atomic_add(s - session->se_target_maxslots, 4487 &nfsd_total_target_slots); 4488 session->se_target_maxslots = s; 4489 } else { 4490 kfree(slot); 4491 slot = NULL; 4492 } 4493 } while (slot && --cnt > 0); 4494 } 4495 4496 out: 4497 seq->maxslots = max(session->se_target_maxslots, seq->maxslots); 4498 seq->target_maxslots = session->se_target_maxslots; 4499 4500 switch (clp->cl_cb_state) { 4501 case NFSD4_CB_DOWN: 4502 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; 4503 break; 4504 case NFSD4_CB_FAULT: 4505 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; 4506 break; 4507 default: 4508 seq->status_flags = 0; 4509 } 4510 if (!list_empty(&clp->cl_revoked)) 4511 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; 4512 if (atomic_read(&clp->cl_admin_revoked)) 4513 seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED; 4514 trace_nfsd_seq4_status(rqstp, seq); 4515 out_no_session: 4516 if (conn) 4517 free_conn(conn); 4518 spin_unlock(&nn->client_lock); 4519 return status; 4520 out_put_session: 4521 nfsd4_put_session_locked(session); 4522 goto out_no_session; 4523 } 4524 4525 void 4526 nfsd4_sequence_done(struct nfsd4_compoundres *resp) 4527 { 4528 struct nfsd4_compound_state *cs = &resp->cstate; 4529 4530 if (nfsd4_has_session(cs)) { 4531 if (cs->status != nfserr_replay_cache) { 4532 nfsd4_store_cache_entry(resp); 4533 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; 4534 } 4535 /* Drop session reference that was taken in nfsd4_sequence() */ 4536 nfsd4_put_session(cs->session); 4537 } else if (cs->clp) 4538 put_client_renew(cs->clp); 4539 } 4540 4541 __be32 4542 nfsd4_destroy_clientid(struct svc_rqst *rqstp, 4543 struct nfsd4_compound_state *cstate, 4544 union nfsd4_op_u *u) 4545 { 4546 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; 4547 struct nfs4_client *conf, *unconf; 4548 struct nfs4_client *clp = NULL; 4549 __be32 status = 0; 4550 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4551 4552 spin_lock(&nn->client_lock); 4553 unconf = find_unconfirmed_client(&dc->clientid, true, nn); 4554 conf = find_confirmed_client(&dc->clientid, true, nn); 4555 WARN_ON_ONCE(conf && unconf); 4556 4557 if (conf) { 4558 if (client_has_state(conf)) { 4559 status = nfserr_clientid_busy; 4560 goto out; 4561 } 4562 status = mark_client_expired_locked(conf); 4563 if (status) 4564 goto out; 4565 clp = conf; 4566 } else if (unconf) 4567 clp = unconf; 4568 else { 4569 status = nfserr_stale_clientid; 4570 goto out; 4571 } 4572 if (!nfsd4_mach_creds_match(clp, rqstp)) { 4573 clp = NULL; 4574 status = nfserr_wrong_cred; 4575 goto out; 4576 } 4577 trace_nfsd_clid_destroyed(&clp->cl_clientid); 4578 unhash_client_locked(clp); 4579 out: 4580 spin_unlock(&nn->client_lock); 4581 if (clp) 4582 expire_client(clp); 4583 return status; 4584 } 4585 4586 __be32 4587 nfsd4_reclaim_complete(struct svc_rqst *rqstp, 4588 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 4589 { 4590 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; 4591 struct nfs4_client *clp = cstate->clp; 4592 __be32 status = 0; 4593 4594 if (rc->rca_one_fs) { 4595 if (!cstate->current_fh.fh_dentry) 4596 return nfserr_nofilehandle; 4597 /* 4598 * We don't take advantage of the rca_one_fs case. 4599 * That's OK, it's optional, we can safely ignore it. 4600 */ 4601 return nfs_ok; 4602 } 4603 4604 status = nfserr_complete_already; 4605 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) 4606 goto out; 4607 4608 status = nfserr_stale_clientid; 4609 if (is_client_expired(clp)) 4610 /* 4611 * The following error isn't really legal. 4612 * But we only get here if the client just explicitly 4613 * destroyed the client. Surely it no longer cares what 4614 * error it gets back on an operation for the dead 4615 * client. 4616 */ 4617 goto out; 4618 4619 status = nfs_ok; 4620 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid); 4621 nfsd4_client_record_create(clp); 4622 inc_reclaim_complete(clp); 4623 out: 4624 return status; 4625 } 4626 4627 __be32 4628 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 4629 union nfsd4_op_u *u) 4630 { 4631 struct nfsd4_setclientid *setclid = &u->setclientid; 4632 struct xdr_netobj clname = setclid->se_name; 4633 nfs4_verifier clverifier = setclid->se_verf; 4634 struct nfs4_client *conf, *new; 4635 struct nfs4_client *unconf = NULL; 4636 __be32 status; 4637 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4638 4639 new = create_client(clname, rqstp, &clverifier); 4640 if (new == NULL) 4641 return nfserr_jukebox; 4642 spin_lock(&nn->client_lock); 4643 conf = find_confirmed_client_by_name(&clname, nn); 4644 if (conf && client_has_state(conf)) { 4645 status = nfserr_clid_inuse; 4646 if (clp_used_exchangeid(conf)) 4647 goto out; 4648 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { 4649 trace_nfsd_clid_cred_mismatch(conf, rqstp); 4650 goto out; 4651 } 4652 } 4653 unconf = find_unconfirmed_client_by_name(&clname, nn); 4654 if (unconf) 4655 unhash_client_locked(unconf); 4656 if (conf) { 4657 if (same_verf(&conf->cl_verifier, &clverifier)) { 4658 copy_clid(new, conf); 4659 gen_confirm(new, nn); 4660 } else 4661 trace_nfsd_clid_verf_mismatch(conf, rqstp, 4662 &clverifier); 4663 } else 4664 trace_nfsd_clid_fresh(new); 4665 new->cl_minorversion = 0; 4666 gen_callback(new, setclid, rqstp); 4667 add_to_unconfirmed(new); 4668 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; 4669 setclid->se_clientid.cl_id = new->cl_clientid.cl_id; 4670 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); 4671 new = NULL; 4672 status = nfs_ok; 4673 out: 4674 spin_unlock(&nn->client_lock); 4675 if (new) 4676 free_client(new); 4677 if (unconf) { 4678 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); 4679 expire_client(unconf); 4680 } 4681 return status; 4682 } 4683 4684 __be32 4685 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, 4686 struct nfsd4_compound_state *cstate, 4687 union nfsd4_op_u *u) 4688 { 4689 struct nfsd4_setclientid_confirm *setclientid_confirm = 4690 &u->setclientid_confirm; 4691 struct nfs4_client *conf, *unconf; 4692 struct nfs4_client *old = NULL; 4693 nfs4_verifier confirm = setclientid_confirm->sc_confirm; 4694 clientid_t * clid = &setclientid_confirm->sc_clientid; 4695 __be32 status; 4696 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 4697 4698 if (STALE_CLIENTID(clid, nn)) 4699 return nfserr_stale_clientid; 4700 4701 spin_lock(&nn->client_lock); 4702 conf = find_confirmed_client(clid, false, nn); 4703 unconf = find_unconfirmed_client(clid, false, nn); 4704 /* 4705 * We try hard to give out unique clientid's, so if we get an 4706 * attempt to confirm the same clientid with a different cred, 4707 * the client may be buggy; this should never happen. 4708 * 4709 * Nevertheless, RFC 7530 recommends INUSE for this case: 4710 */ 4711 status = nfserr_clid_inuse; 4712 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) { 4713 trace_nfsd_clid_cred_mismatch(unconf, rqstp); 4714 goto out; 4715 } 4716 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) { 4717 trace_nfsd_clid_cred_mismatch(conf, rqstp); 4718 goto out; 4719 } 4720 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { 4721 if (conf && same_verf(&confirm, &conf->cl_confirm)) { 4722 status = nfs_ok; 4723 } else 4724 status = nfserr_stale_clientid; 4725 goto out; 4726 } 4727 status = nfs_ok; 4728 if (conf) { 4729 if (get_client_locked(conf) == nfs_ok) { 4730 old = unconf; 4731 unhash_client_locked(old); 4732 nfsd4_change_callback(conf, &unconf->cl_cb_conn); 4733 } else { 4734 conf = NULL; 4735 } 4736 } 4737 4738 if (!conf) { 4739 old = find_confirmed_client_by_name(&unconf->cl_name, nn); 4740 if (old) { 4741 status = nfserr_clid_inuse; 4742 if (client_has_state(old) 4743 && !same_creds(&unconf->cl_cred, 4744 &old->cl_cred)) { 4745 old = NULL; 4746 goto out; 4747 } 4748 status = mark_client_expired_locked(old); 4749 if (status) { 4750 old = NULL; 4751 goto out; 4752 } 4753 trace_nfsd_clid_replaced(&old->cl_clientid); 4754 } 4755 status = get_client_locked(unconf); 4756 if (status != nfs_ok) { 4757 old = NULL; 4758 goto out; 4759 } 4760 move_to_confirmed(unconf); 4761 conf = unconf; 4762 } 4763 spin_unlock(&nn->client_lock); 4764 if (conf == unconf) 4765 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); 4766 nfsd4_probe_callback(conf); 4767 spin_lock(&nn->client_lock); 4768 put_client_renew_locked(conf); 4769 out: 4770 spin_unlock(&nn->client_lock); 4771 if (old) 4772 expire_client(old); 4773 return status; 4774 } 4775 4776 static struct nfs4_file *nfsd4_alloc_file(void) 4777 { 4778 return kmem_cache_alloc(file_slab, GFP_KERNEL); 4779 } 4780 4781 /* OPEN Share state helper functions */ 4782 4783 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp) 4784 { 4785 refcount_set(&fp->fi_ref, 1); 4786 spin_lock_init(&fp->fi_lock); 4787 INIT_LIST_HEAD(&fp->fi_stateids); 4788 INIT_LIST_HEAD(&fp->fi_delegations); 4789 INIT_LIST_HEAD(&fp->fi_clnt_odstate); 4790 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle); 4791 fp->fi_deleg_file = NULL; 4792 fp->fi_rdeleg_file = NULL; 4793 fp->fi_had_conflict = false; 4794 fp->fi_share_deny = 0; 4795 memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); 4796 memset(fp->fi_access, 0, sizeof(fp->fi_access)); 4797 fp->fi_aliased = false; 4798 fp->fi_inode = d_inode(fh->fh_dentry); 4799 #ifdef CONFIG_NFSD_PNFS 4800 INIT_LIST_HEAD(&fp->fi_lo_states); 4801 atomic_set(&fp->fi_lo_recalls, 0); 4802 #endif 4803 } 4804 4805 void 4806 nfsd4_free_slabs(void) 4807 { 4808 kmem_cache_destroy(client_slab); 4809 kmem_cache_destroy(openowner_slab); 4810 kmem_cache_destroy(lockowner_slab); 4811 kmem_cache_destroy(file_slab); 4812 kmem_cache_destroy(stateid_slab); 4813 kmem_cache_destroy(deleg_slab); 4814 kmem_cache_destroy(odstate_slab); 4815 } 4816 4817 int 4818 nfsd4_init_slabs(void) 4819 { 4820 client_slab = KMEM_CACHE(nfs4_client, 0); 4821 if (client_slab == NULL) 4822 goto out; 4823 openowner_slab = KMEM_CACHE(nfs4_openowner, 0); 4824 if (openowner_slab == NULL) 4825 goto out_free_client_slab; 4826 lockowner_slab = KMEM_CACHE(nfs4_lockowner, 0); 4827 if (lockowner_slab == NULL) 4828 goto out_free_openowner_slab; 4829 file_slab = KMEM_CACHE(nfs4_file, 0); 4830 if (file_slab == NULL) 4831 goto out_free_lockowner_slab; 4832 stateid_slab = KMEM_CACHE(nfs4_ol_stateid, 0); 4833 if (stateid_slab == NULL) 4834 goto out_free_file_slab; 4835 deleg_slab = KMEM_CACHE(nfs4_delegation, 0); 4836 if (deleg_slab == NULL) 4837 goto out_free_stateid_slab; 4838 odstate_slab = KMEM_CACHE(nfs4_clnt_odstate, 0); 4839 if (odstate_slab == NULL) 4840 goto out_free_deleg_slab; 4841 return 0; 4842 4843 out_free_deleg_slab: 4844 kmem_cache_destroy(deleg_slab); 4845 out_free_stateid_slab: 4846 kmem_cache_destroy(stateid_slab); 4847 out_free_file_slab: 4848 kmem_cache_destroy(file_slab); 4849 out_free_lockowner_slab: 4850 kmem_cache_destroy(lockowner_slab); 4851 out_free_openowner_slab: 4852 kmem_cache_destroy(openowner_slab); 4853 out_free_client_slab: 4854 kmem_cache_destroy(client_slab); 4855 out: 4856 return -ENOMEM; 4857 } 4858 4859 static unsigned long 4860 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc) 4861 { 4862 struct nfsd_net *nn = shrink->private_data; 4863 long count; 4864 4865 count = atomic_read(&nn->nfsd_courtesy_clients); 4866 if (!count) 4867 count = atomic_long_read(&num_delegations); 4868 if (count) 4869 queue_work(laundry_wq, &nn->nfsd_shrinker_work); 4870 return (unsigned long)count; 4871 } 4872 4873 static unsigned long 4874 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc) 4875 { 4876 return SHRINK_STOP; 4877 } 4878 4879 void 4880 nfsd4_init_leases_net(struct nfsd_net *nn) 4881 { 4882 struct sysinfo si; 4883 u64 max_clients; 4884 4885 nn->nfsd4_lease = 90; /* default lease time */ 4886 nn->nfsd4_grace = 90; 4887 nn->somebody_reclaimed = false; 4888 nn->track_reclaim_completes = false; 4889 nn->clverifier_counter = get_random_u32(); 4890 nn->clientid_base = get_random_u32(); 4891 nn->clientid_counter = nn->clientid_base + 1; 4892 nn->s2s_cp_cl_id = nn->clientid_counter++; 4893 4894 atomic_set(&nn->nfs4_client_count, 0); 4895 si_meminfo(&si); 4896 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024); 4897 max_clients *= NFS4_CLIENTS_PER_GB; 4898 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB); 4899 4900 atomic_set(&nn->nfsd_courtesy_clients, 0); 4901 } 4902 4903 enum rp_lock { 4904 RP_UNLOCKED, 4905 RP_LOCKED, 4906 RP_UNHASHED, 4907 }; 4908 4909 static void init_nfs4_replay(struct nfs4_replay *rp) 4910 { 4911 rp->rp_status = nfserr_serverfault; 4912 rp->rp_buflen = 0; 4913 rp->rp_buf = rp->rp_ibuf; 4914 rp->rp_locked = RP_UNLOCKED; 4915 } 4916 4917 static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, 4918 struct nfs4_stateowner *so) 4919 { 4920 if (!nfsd4_has_session(cstate)) { 4921 wait_var_event(&so->so_replay.rp_locked, 4922 cmpxchg(&so->so_replay.rp_locked, 4923 RP_UNLOCKED, RP_LOCKED) != RP_LOCKED); 4924 if (so->so_replay.rp_locked == RP_UNHASHED) 4925 return -EAGAIN; 4926 cstate->replay_owner = nfs4_get_stateowner(so); 4927 } 4928 return 0; 4929 } 4930 4931 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) 4932 { 4933 struct nfs4_stateowner *so = cstate->replay_owner; 4934 4935 if (so != NULL) { 4936 cstate->replay_owner = NULL; 4937 store_release_wake_up(&so->so_replay.rp_locked, RP_UNLOCKED); 4938 nfs4_put_stateowner(so); 4939 } 4940 } 4941 4942 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) 4943 { 4944 struct nfs4_stateowner *sop; 4945 4946 sop = kmem_cache_alloc(slab, GFP_KERNEL); 4947 if (!sop) 4948 return NULL; 4949 4950 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); 4951 if (!sop->so_owner.data) { 4952 kmem_cache_free(slab, sop); 4953 return NULL; 4954 } 4955 4956 INIT_LIST_HEAD(&sop->so_stateids); 4957 sop->so_client = clp; 4958 init_nfs4_replay(&sop->so_replay); 4959 atomic_set(&sop->so_count, 1); 4960 return sop; 4961 } 4962 4963 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) 4964 { 4965 lockdep_assert_held(&clp->cl_lock); 4966 4967 list_add(&oo->oo_owner.so_strhash, 4968 &clp->cl_ownerstr_hashtbl[strhashval]); 4969 list_add(&oo->oo_perclient, &clp->cl_openowners); 4970 } 4971 4972 static void nfs4_unhash_openowner(struct nfs4_stateowner *so) 4973 { 4974 unhash_openowner_locked(openowner(so)); 4975 } 4976 4977 static void nfs4_free_openowner(struct nfs4_stateowner *so) 4978 { 4979 struct nfs4_openowner *oo = openowner(so); 4980 4981 kmem_cache_free(openowner_slab, oo); 4982 } 4983 4984 static const struct nfs4_stateowner_operations openowner_ops = { 4985 .so_unhash = nfs4_unhash_openowner, 4986 .so_free = nfs4_free_openowner, 4987 }; 4988 4989 static struct nfs4_ol_stateid * 4990 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) 4991 { 4992 struct nfs4_ol_stateid *local, *ret = NULL; 4993 struct nfs4_openowner *oo = open->op_openowner; 4994 4995 lockdep_assert_held(&fp->fi_lock); 4996 4997 list_for_each_entry(local, &fp->fi_stateids, st_perfile) { 4998 /* ignore lock owners */ 4999 if (local->st_stateowner->so_is_open_owner == 0) 5000 continue; 5001 if (local->st_stateowner != &oo->oo_owner) 5002 continue; 5003 if (local->st_stid.sc_type == SC_TYPE_OPEN && 5004 !local->st_stid.sc_status) { 5005 ret = local; 5006 refcount_inc(&ret->st_stid.sc_count); 5007 break; 5008 } 5009 } 5010 return ret; 5011 } 5012 5013 static void nfsd4_drop_revoked_stid(struct nfs4_stid *s) 5014 __releases(&s->sc_client->cl_lock) 5015 { 5016 struct nfs4_client *cl = s->sc_client; 5017 LIST_HEAD(reaplist); 5018 struct nfs4_ol_stateid *stp; 5019 struct nfs4_delegation *dp; 5020 bool unhashed; 5021 5022 switch (s->sc_type) { 5023 case SC_TYPE_OPEN: 5024 stp = openlockstateid(s); 5025 if (unhash_open_stateid(stp, &reaplist)) 5026 put_ol_stateid_locked(stp, &reaplist); 5027 spin_unlock(&cl->cl_lock); 5028 free_ol_stateid_reaplist(&reaplist); 5029 break; 5030 case SC_TYPE_LOCK: 5031 stp = openlockstateid(s); 5032 unhashed = unhash_lock_stateid(stp); 5033 spin_unlock(&cl->cl_lock); 5034 if (unhashed) 5035 nfs4_put_stid(s); 5036 break; 5037 case SC_TYPE_DELEG: 5038 dp = delegstateid(s); 5039 list_del_init(&dp->dl_recall_lru); 5040 spin_unlock(&cl->cl_lock); 5041 nfs4_put_stid(s); 5042 break; 5043 default: 5044 spin_unlock(&cl->cl_lock); 5045 } 5046 } 5047 5048 static void nfsd40_drop_revoked_stid(struct nfs4_client *cl, 5049 stateid_t *stid) 5050 { 5051 /* NFSv4.0 has no way for the client to tell the server 5052 * that it can forget an admin-revoked stateid. 5053 * So we keep it around until the first time that the 5054 * client uses it, and drop it the first time 5055 * nfserr_admin_revoked is returned. 5056 * For v4.1 and later we wait until explicitly told 5057 * to free the stateid. 5058 */ 5059 if (cl->cl_minorversion == 0) { 5060 struct nfs4_stid *st; 5061 5062 spin_lock(&cl->cl_lock); 5063 st = find_stateid_locked(cl, stid); 5064 if (st) 5065 nfsd4_drop_revoked_stid(st); 5066 else 5067 spin_unlock(&cl->cl_lock); 5068 } 5069 } 5070 5071 static __be32 5072 nfsd4_verify_open_stid(struct nfs4_stid *s) 5073 { 5074 __be32 ret = nfs_ok; 5075 5076 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) 5077 ret = nfserr_admin_revoked; 5078 else if (s->sc_status & SC_STATUS_REVOKED) 5079 ret = nfserr_deleg_revoked; 5080 else if (s->sc_status & SC_STATUS_CLOSED) 5081 ret = nfserr_bad_stateid; 5082 return ret; 5083 } 5084 5085 /* Lock the stateid st_mutex, and deal with races with CLOSE */ 5086 static __be32 5087 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) 5088 { 5089 __be32 ret; 5090 5091 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); 5092 ret = nfsd4_verify_open_stid(&stp->st_stid); 5093 if (ret == nfserr_admin_revoked) 5094 nfsd40_drop_revoked_stid(stp->st_stid.sc_client, 5095 &stp->st_stid.sc_stateid); 5096 5097 if (ret != nfs_ok) 5098 mutex_unlock(&stp->st_mutex); 5099 return ret; 5100 } 5101 5102 static struct nfs4_ol_stateid * 5103 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) 5104 { 5105 struct nfs4_ol_stateid *stp; 5106 for (;;) { 5107 spin_lock(&fp->fi_lock); 5108 stp = nfsd4_find_existing_open(fp, open); 5109 spin_unlock(&fp->fi_lock); 5110 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) 5111 break; 5112 nfs4_put_stid(&stp->st_stid); 5113 } 5114 return stp; 5115 } 5116 5117 static struct nfs4_openowner * 5118 find_or_alloc_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, 5119 struct nfsd4_compound_state *cstate) 5120 { 5121 struct nfs4_client *clp = cstate->clp; 5122 struct nfs4_openowner *oo, *new = NULL; 5123 5124 retry: 5125 spin_lock(&clp->cl_lock); 5126 oo = find_openstateowner_str(strhashval, open, clp); 5127 if (!oo && new) { 5128 hash_openowner(new, clp, strhashval); 5129 spin_unlock(&clp->cl_lock); 5130 return new; 5131 } 5132 spin_unlock(&clp->cl_lock); 5133 5134 if (oo && !(oo->oo_flags & NFS4_OO_CONFIRMED)) { 5135 /* Replace unconfirmed owners without checking for replay. */ 5136 release_openowner(oo); 5137 oo = NULL; 5138 } 5139 if (oo) { 5140 if (new) 5141 nfs4_free_stateowner(&new->oo_owner); 5142 return oo; 5143 } 5144 5145 new = alloc_stateowner(openowner_slab, &open->op_owner, clp); 5146 if (!new) 5147 return NULL; 5148 new->oo_owner.so_ops = &openowner_ops; 5149 new->oo_owner.so_is_open_owner = 1; 5150 new->oo_owner.so_seqid = open->op_seqid; 5151 new->oo_flags = 0; 5152 if (nfsd4_has_session(cstate)) 5153 new->oo_flags |= NFS4_OO_CONFIRMED; 5154 new->oo_time = 0; 5155 new->oo_last_closed_stid = NULL; 5156 INIT_LIST_HEAD(&new->oo_close_lru); 5157 goto retry; 5158 } 5159 5160 static struct nfs4_ol_stateid * 5161 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) 5162 { 5163 5164 struct nfs4_openowner *oo = open->op_openowner; 5165 struct nfs4_ol_stateid *retstp = NULL; 5166 struct nfs4_ol_stateid *stp; 5167 5168 stp = open->op_stp; 5169 /* We are moving these outside of the spinlocks to avoid the warnings */ 5170 mutex_init(&stp->st_mutex); 5171 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 5172 5173 retry: 5174 spin_lock(&oo->oo_owner.so_client->cl_lock); 5175 spin_lock(&fp->fi_lock); 5176 5177 if (nfs4_openowner_unhashed(oo)) { 5178 mutex_unlock(&stp->st_mutex); 5179 stp = NULL; 5180 goto out_unlock; 5181 } 5182 5183 retstp = nfsd4_find_existing_open(fp, open); 5184 if (retstp) 5185 goto out_unlock; 5186 5187 open->op_stp = NULL; 5188 refcount_inc(&stp->st_stid.sc_count); 5189 stp->st_stid.sc_type = SC_TYPE_OPEN; 5190 INIT_LIST_HEAD(&stp->st_locks); 5191 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); 5192 get_nfs4_file(fp); 5193 stp->st_stid.sc_file = fp; 5194 stp->st_access_bmap = 0; 5195 stp->st_deny_bmap = 0; 5196 stp->st_openstp = NULL; 5197 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); 5198 list_add(&stp->st_perfile, &fp->fi_stateids); 5199 5200 out_unlock: 5201 spin_unlock(&fp->fi_lock); 5202 spin_unlock(&oo->oo_owner.so_client->cl_lock); 5203 if (retstp) { 5204 /* Handle races with CLOSE */ 5205 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { 5206 nfs4_put_stid(&retstp->st_stid); 5207 goto retry; 5208 } 5209 /* To keep mutex tracking happy */ 5210 mutex_unlock(&stp->st_mutex); 5211 stp = retstp; 5212 } 5213 return stp; 5214 } 5215 5216 /* 5217 * In the 4.0 case we need to keep the owners around a little while to handle 5218 * CLOSE replay. We still do need to release any file access that is held by 5219 * them before returning however. 5220 */ 5221 static void 5222 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) 5223 { 5224 struct nfs4_ol_stateid *last; 5225 struct nfs4_openowner *oo = openowner(s->st_stateowner); 5226 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, 5227 nfsd_net_id); 5228 5229 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); 5230 5231 /* 5232 * We know that we hold one reference via nfsd4_close, and another 5233 * "persistent" reference for the client. If the refcount is higher 5234 * than 2, then there are still calls in progress that are using this 5235 * stateid. We can't put the sc_file reference until they are finished. 5236 * Wait for the refcount to drop to 2. Since it has been unhashed, 5237 * there should be no danger of the refcount going back up again at 5238 * this point. 5239 * Some threads with a reference might be waiting for rp_locked, 5240 * so tell them to stop waiting. 5241 */ 5242 store_release_wake_up(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED); 5243 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); 5244 5245 release_all_access(s); 5246 if (s->st_stid.sc_file) { 5247 put_nfs4_file(s->st_stid.sc_file); 5248 s->st_stid.sc_file = NULL; 5249 } 5250 5251 spin_lock(&nn->client_lock); 5252 last = oo->oo_last_closed_stid; 5253 oo->oo_last_closed_stid = s; 5254 list_move_tail(&oo->oo_close_lru, &nn->close_lru); 5255 oo->oo_time = ktime_get_boottime_seconds(); 5256 spin_unlock(&nn->client_lock); 5257 if (last) 5258 nfs4_put_stid(&last->st_stid); 5259 } 5260 5261 static noinline_for_stack struct nfs4_file * 5262 nfsd4_file_hash_lookup(const struct svc_fh *fhp) 5263 { 5264 struct inode *inode = d_inode(fhp->fh_dentry); 5265 struct rhlist_head *tmp, *list; 5266 struct nfs4_file *fi; 5267 5268 rcu_read_lock(); 5269 list = rhltable_lookup(&nfs4_file_rhltable, &inode, 5270 nfs4_file_rhash_params); 5271 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) { 5272 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) { 5273 if (refcount_inc_not_zero(&fi->fi_ref)) { 5274 rcu_read_unlock(); 5275 return fi; 5276 } 5277 } 5278 } 5279 rcu_read_unlock(); 5280 return NULL; 5281 } 5282 5283 /* 5284 * On hash insertion, identify entries with the same inode but 5285 * distinct filehandles. They will all be on the list returned 5286 * by rhltable_lookup(). 5287 * 5288 * inode->i_lock prevents racing insertions from adding an entry 5289 * for the same inode/fhp pair twice. 5290 */ 5291 static noinline_for_stack struct nfs4_file * 5292 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp) 5293 { 5294 struct inode *inode = d_inode(fhp->fh_dentry); 5295 struct rhlist_head *tmp, *list; 5296 struct nfs4_file *ret = NULL; 5297 bool alias_found = false; 5298 struct nfs4_file *fi; 5299 int err; 5300 5301 rcu_read_lock(); 5302 spin_lock(&inode->i_lock); 5303 5304 list = rhltable_lookup(&nfs4_file_rhltable, &inode, 5305 nfs4_file_rhash_params); 5306 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) { 5307 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) { 5308 if (refcount_inc_not_zero(&fi->fi_ref)) 5309 ret = fi; 5310 } else 5311 fi->fi_aliased = alias_found = true; 5312 } 5313 if (ret) 5314 goto out_unlock; 5315 5316 nfsd4_file_init(fhp, new); 5317 err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist, 5318 nfs4_file_rhash_params); 5319 if (err) 5320 goto out_unlock; 5321 5322 new->fi_aliased = alias_found; 5323 ret = new; 5324 5325 out_unlock: 5326 spin_unlock(&inode->i_lock); 5327 rcu_read_unlock(); 5328 return ret; 5329 } 5330 5331 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi) 5332 { 5333 rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist, 5334 nfs4_file_rhash_params); 5335 } 5336 5337 /* 5338 * Called to check deny when READ with all zero stateid or 5339 * WRITE with all zero or all one stateid 5340 */ 5341 static __be32 5342 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) 5343 { 5344 struct nfs4_file *fp; 5345 __be32 ret = nfs_ok; 5346 5347 fp = nfsd4_file_hash_lookup(current_fh); 5348 if (!fp) 5349 return ret; 5350 5351 /* Check for conflicting share reservations */ 5352 spin_lock(&fp->fi_lock); 5353 if (fp->fi_share_deny & deny_type) 5354 ret = nfserr_locked; 5355 spin_unlock(&fp->fi_lock); 5356 put_nfs4_file(fp); 5357 return ret; 5358 } 5359 5360 static bool nfsd4_deleg_present(const struct inode *inode) 5361 { 5362 struct file_lock_context *ctx = locks_inode_context(inode); 5363 5364 return ctx && !list_empty_careful(&ctx->flc_lease); 5365 } 5366 5367 /** 5368 * nfsd_wait_for_delegreturn - wait for delegations to be returned 5369 * @rqstp: the RPC transaction being executed 5370 * @inode: in-core inode of the file being waited for 5371 * 5372 * The timeout prevents deadlock if all nfsd threads happen to be 5373 * tied up waiting for returning delegations. 5374 * 5375 * Return values: 5376 * %true: delegation was returned 5377 * %false: timed out waiting for delegreturn 5378 */ 5379 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode) 5380 { 5381 long __maybe_unused timeo; 5382 5383 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode), 5384 NFSD_DELEGRETURN_TIMEOUT); 5385 trace_nfsd_delegret_wakeup(rqstp, inode, timeo); 5386 return timeo > 0; 5387 } 5388 5389 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) 5390 { 5391 struct nfs4_delegation *dp = cb_to_delegation(cb); 5392 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, 5393 nfsd_net_id); 5394 5395 block_delegations(&dp->dl_stid.sc_file->fi_fhandle); 5396 5397 /* 5398 * We can't do this in nfsd_break_deleg_cb because it is 5399 * already holding inode->i_lock. 5400 * 5401 * If the dl_time != 0, then we know that it has already been 5402 * queued for a lease break. Don't queue it again. 5403 */ 5404 spin_lock(&state_lock); 5405 if (delegation_hashed(dp) && dp->dl_time == 0) { 5406 dp->dl_time = ktime_get_boottime_seconds(); 5407 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); 5408 } 5409 spin_unlock(&state_lock); 5410 } 5411 5412 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, 5413 struct rpc_task *task) 5414 { 5415 struct nfs4_delegation *dp = cb_to_delegation(cb); 5416 5417 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task); 5418 5419 if (dp->dl_stid.sc_status) 5420 /* CLOSED or REVOKED */ 5421 return 1; 5422 5423 switch (task->tk_status) { 5424 case 0: 5425 return 1; 5426 case -NFS4ERR_DELAY: 5427 rpc_delay(task, 2 * HZ); 5428 return 0; 5429 case -EBADHANDLE: 5430 case -NFS4ERR_BAD_STATEID: 5431 /* 5432 * Race: client probably got cb_recall before open reply 5433 * granting delegation. 5434 */ 5435 if (dp->dl_retries--) { 5436 rpc_delay(task, 2 * HZ); 5437 return 0; 5438 } 5439 fallthrough; 5440 default: 5441 return 1; 5442 } 5443 } 5444 5445 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) 5446 { 5447 struct nfs4_delegation *dp = cb_to_delegation(cb); 5448 5449 nfs4_put_stid(&dp->dl_stid); 5450 } 5451 5452 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { 5453 .prepare = nfsd4_cb_recall_prepare, 5454 .done = nfsd4_cb_recall_done, 5455 .release = nfsd4_cb_recall_release, 5456 .opcode = OP_CB_RECALL, 5457 }; 5458 5459 static void nfsd_break_one_deleg(struct nfs4_delegation *dp) 5460 { 5461 bool queued; 5462 5463 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &dp->dl_recall.cb_flags)) 5464 return; 5465 5466 /* 5467 * We're assuming the state code never drops its reference 5468 * without first removing the lease. Since we're in this lease 5469 * callback (and since the lease code is serialized by the 5470 * flc_lock) we know the server hasn't removed the lease yet, and 5471 * we know it's safe to take a reference. 5472 */ 5473 refcount_inc(&dp->dl_stid.sc_count); 5474 queued = nfsd4_run_cb(&dp->dl_recall); 5475 WARN_ON_ONCE(!queued); 5476 if (!queued) 5477 refcount_dec(&dp->dl_stid.sc_count); 5478 } 5479 5480 /* Called from break_lease() with flc_lock held. */ 5481 static bool 5482 nfsd_break_deleg_cb(struct file_lease *fl) 5483 { 5484 struct nfs4_delegation *dp = (struct nfs4_delegation *) fl->c.flc_owner; 5485 struct nfs4_file *fp = dp->dl_stid.sc_file; 5486 struct nfs4_client *clp = dp->dl_stid.sc_client; 5487 struct nfsd_net *nn; 5488 5489 trace_nfsd_cb_recall(&dp->dl_stid); 5490 5491 dp->dl_recalled = true; 5492 atomic_inc(&clp->cl_delegs_in_recall); 5493 if (try_to_expire_client(clp)) { 5494 nn = net_generic(clp->net, nfsd_net_id); 5495 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 5496 } 5497 5498 /* 5499 * We don't want the locks code to timeout the lease for us; 5500 * we'll remove it ourself if a delegation isn't returned 5501 * in time: 5502 */ 5503 fl->fl_break_time = 0; 5504 5505 fp->fi_had_conflict = true; 5506 nfsd_break_one_deleg(dp); 5507 return false; 5508 } 5509 5510 /** 5511 * nfsd_breaker_owns_lease - Check if lease conflict was resolved 5512 * @fl: Lock state to check 5513 * 5514 * Return values: 5515 * %true: Lease conflict was resolved 5516 * %false: Lease conflict was not resolved. 5517 */ 5518 static bool nfsd_breaker_owns_lease(struct file_lease *fl) 5519 { 5520 struct nfs4_delegation *dl = fl->c.flc_owner; 5521 struct svc_rqst *rqst; 5522 struct nfs4_client *clp; 5523 5524 rqst = nfsd_current_rqst(); 5525 if (!nfsd_v4client(rqst)) 5526 return false; 5527 clp = *(rqst->rq_lease_breaker); 5528 return dl->dl_stid.sc_client == clp; 5529 } 5530 5531 static int 5532 nfsd_change_deleg_cb(struct file_lease *onlist, int arg, 5533 struct list_head *dispose) 5534 { 5535 struct nfs4_delegation *dp = (struct nfs4_delegation *) onlist->c.flc_owner; 5536 struct nfs4_client *clp = dp->dl_stid.sc_client; 5537 5538 if (arg & F_UNLCK) { 5539 if (dp->dl_recalled) 5540 atomic_dec(&clp->cl_delegs_in_recall); 5541 return lease_modify(onlist, arg, dispose); 5542 } else 5543 return -EAGAIN; 5544 } 5545 5546 static const struct lease_manager_operations nfsd_lease_mng_ops = { 5547 .lm_breaker_owns_lease = nfsd_breaker_owns_lease, 5548 .lm_break = nfsd_break_deleg_cb, 5549 .lm_change = nfsd_change_deleg_cb, 5550 }; 5551 5552 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) 5553 { 5554 if (nfsd4_has_session(cstate)) 5555 return nfs_ok; 5556 if (seqid == so->so_seqid - 1) 5557 return nfserr_replay_me; 5558 if (seqid == so->so_seqid) 5559 return nfs_ok; 5560 return nfserr_bad_seqid; 5561 } 5562 5563 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions, 5564 struct nfsd_net *nn) 5565 { 5566 struct nfs4_client *found; 5567 5568 spin_lock(&nn->client_lock); 5569 found = find_confirmed_client(clid, sessions, nn); 5570 if (found) 5571 atomic_inc(&found->cl_rpc_users); 5572 spin_unlock(&nn->client_lock); 5573 return found; 5574 } 5575 5576 static __be32 set_client(clientid_t *clid, 5577 struct nfsd4_compound_state *cstate, 5578 struct nfsd_net *nn) 5579 { 5580 if (cstate->clp) { 5581 if (!same_clid(&cstate->clp->cl_clientid, clid)) 5582 return nfserr_stale_clientid; 5583 return nfs_ok; 5584 } 5585 if (STALE_CLIENTID(clid, nn)) 5586 return nfserr_stale_clientid; 5587 /* 5588 * We're in the 4.0 case (otherwise the SEQUENCE op would have 5589 * set cstate->clp), so session = false: 5590 */ 5591 cstate->clp = lookup_clientid(clid, false, nn); 5592 if (!cstate->clp) 5593 return nfserr_expired; 5594 return nfs_ok; 5595 } 5596 5597 __be32 5598 nfsd4_process_open1(struct nfsd4_compound_state *cstate, 5599 struct nfsd4_open *open, struct nfsd_net *nn) 5600 { 5601 clientid_t *clientid = &open->op_clientid; 5602 struct nfs4_client *clp = NULL; 5603 unsigned int strhashval; 5604 struct nfs4_openowner *oo = NULL; 5605 __be32 status; 5606 5607 /* 5608 * In case we need it later, after we've already created the 5609 * file and don't want to risk a further failure: 5610 */ 5611 open->op_file = nfsd4_alloc_file(); 5612 if (open->op_file == NULL) 5613 return nfserr_jukebox; 5614 5615 status = set_client(clientid, cstate, nn); 5616 if (status) 5617 return status; 5618 clp = cstate->clp; 5619 5620 strhashval = ownerstr_hashval(&open->op_owner); 5621 retry: 5622 oo = find_or_alloc_open_stateowner(strhashval, open, cstate); 5623 open->op_openowner = oo; 5624 if (!oo) 5625 return nfserr_jukebox; 5626 if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) { 5627 nfs4_put_stateowner(&oo->oo_owner); 5628 goto retry; 5629 } 5630 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); 5631 if (status) 5632 return status; 5633 5634 open->op_stp = nfs4_alloc_open_stateid(clp); 5635 if (!open->op_stp) 5636 return nfserr_jukebox; 5637 5638 if (nfsd4_has_session(cstate) && 5639 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { 5640 open->op_odstate = alloc_clnt_odstate(clp); 5641 if (!open->op_odstate) 5642 return nfserr_jukebox; 5643 } 5644 5645 return nfs_ok; 5646 } 5647 5648 static inline __be32 5649 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) 5650 { 5651 if (!(flags & RD_STATE) && deleg_is_read(dp->dl_type)) 5652 return nfserr_openmode; 5653 else 5654 return nfs_ok; 5655 } 5656 5657 static int share_access_to_flags(u32 share_access) 5658 { 5659 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; 5660 } 5661 5662 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, 5663 stateid_t *s) 5664 { 5665 struct nfs4_stid *ret; 5666 5667 ret = find_stateid_by_type(cl, s, SC_TYPE_DELEG, SC_STATUS_REVOKED); 5668 if (!ret) 5669 return NULL; 5670 return delegstateid(ret); 5671 } 5672 5673 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) 5674 { 5675 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || 5676 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; 5677 } 5678 5679 static __be32 5680 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, 5681 struct nfs4_delegation **dp) 5682 { 5683 int flags; 5684 __be32 status = nfserr_bad_stateid; 5685 struct nfs4_delegation *deleg; 5686 5687 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); 5688 if (deleg == NULL) 5689 goto out; 5690 if (deleg->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED) { 5691 nfs4_put_stid(&deleg->dl_stid); 5692 status = nfserr_admin_revoked; 5693 goto out; 5694 } 5695 if (deleg->dl_stid.sc_status & SC_STATUS_REVOKED) { 5696 nfs4_put_stid(&deleg->dl_stid); 5697 nfsd40_drop_revoked_stid(cl, &open->op_delegate_stateid); 5698 status = nfserr_deleg_revoked; 5699 goto out; 5700 } 5701 flags = share_access_to_flags(open->op_share_access); 5702 status = nfs4_check_delegmode(deleg, flags); 5703 if (status) { 5704 nfs4_put_stid(&deleg->dl_stid); 5705 goto out; 5706 } 5707 *dp = deleg; 5708 out: 5709 if (!nfsd4_is_deleg_cur(open)) 5710 return nfs_ok; 5711 if (status) 5712 return status; 5713 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 5714 return nfs_ok; 5715 } 5716 5717 static inline int nfs4_access_to_access(u32 nfs4_access) 5718 { 5719 int flags = 0; 5720 5721 if (nfs4_access & NFS4_SHARE_ACCESS_READ) 5722 flags |= NFSD_MAY_READ; 5723 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) 5724 flags |= NFSD_MAY_WRITE; 5725 return flags; 5726 } 5727 5728 static inline __be32 5729 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, 5730 struct nfsd4_open *open) 5731 { 5732 struct iattr iattr = { 5733 .ia_valid = ATTR_SIZE, 5734 .ia_size = 0, 5735 }; 5736 struct nfsd_attrs attrs = { 5737 .na_iattr = &iattr, 5738 }; 5739 if (!open->op_truncate) 5740 return 0; 5741 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) 5742 return nfserr_inval; 5743 return nfsd_setattr(rqstp, fh, &attrs, NULL); 5744 } 5745 5746 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, 5747 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, 5748 struct nfsd4_open *open, bool new_stp) 5749 { 5750 struct nfsd_file *nf = NULL; 5751 __be32 status; 5752 int oflag = nfs4_access_to_omode(open->op_share_access); 5753 int access = nfs4_access_to_access(open->op_share_access); 5754 unsigned char old_access_bmap, old_deny_bmap; 5755 5756 spin_lock(&fp->fi_lock); 5757 5758 /* 5759 * Are we trying to set a deny mode that would conflict with 5760 * current access? 5761 */ 5762 status = nfs4_file_check_deny(fp, open->op_share_deny); 5763 if (status != nfs_ok) { 5764 if (status != nfserr_share_denied) { 5765 spin_unlock(&fp->fi_lock); 5766 goto out; 5767 } 5768 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp, 5769 stp, open->op_share_deny, false)) 5770 status = nfserr_jukebox; 5771 spin_unlock(&fp->fi_lock); 5772 goto out; 5773 } 5774 5775 /* set access to the file */ 5776 status = nfs4_file_get_access(fp, open->op_share_access); 5777 if (status != nfs_ok) { 5778 if (status != nfserr_share_denied) { 5779 spin_unlock(&fp->fi_lock); 5780 goto out; 5781 } 5782 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp, 5783 stp, open->op_share_access, true)) 5784 status = nfserr_jukebox; 5785 spin_unlock(&fp->fi_lock); 5786 goto out; 5787 } 5788 5789 /* Set access bits in stateid */ 5790 old_access_bmap = stp->st_access_bmap; 5791 set_access(open->op_share_access, stp); 5792 5793 /* Set new deny mask */ 5794 old_deny_bmap = stp->st_deny_bmap; 5795 set_deny(open->op_share_deny, stp); 5796 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); 5797 5798 if (!fp->fi_fds[oflag]) { 5799 spin_unlock(&fp->fi_lock); 5800 5801 status = nfsd_file_acquire_opened(rqstp, cur_fh, access, 5802 open->op_filp, &nf); 5803 if (status != nfs_ok) 5804 goto out_put_access; 5805 5806 spin_lock(&fp->fi_lock); 5807 if (!fp->fi_fds[oflag]) { 5808 fp->fi_fds[oflag] = nf; 5809 nf = NULL; 5810 } 5811 } 5812 spin_unlock(&fp->fi_lock); 5813 if (nf) 5814 nfsd_file_put(nf); 5815 5816 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode, 5817 access)); 5818 if (status) 5819 goto out_put_access; 5820 5821 status = nfsd4_truncate(rqstp, cur_fh, open); 5822 if (status) 5823 goto out_put_access; 5824 out: 5825 return status; 5826 out_put_access: 5827 stp->st_access_bmap = old_access_bmap; 5828 nfs4_file_put_access(fp, open->op_share_access); 5829 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); 5830 goto out; 5831 } 5832 5833 static __be32 5834 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, 5835 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, 5836 struct nfsd4_open *open) 5837 { 5838 __be32 status; 5839 unsigned char old_deny_bmap = stp->st_deny_bmap; 5840 5841 if (!test_access(open->op_share_access, stp)) 5842 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false); 5843 5844 /* test and set deny mode */ 5845 spin_lock(&fp->fi_lock); 5846 status = nfs4_file_check_deny(fp, open->op_share_deny); 5847 switch (status) { 5848 case nfs_ok: 5849 set_deny(open->op_share_deny, stp); 5850 fp->fi_share_deny |= 5851 (open->op_share_deny & NFS4_SHARE_DENY_BOTH); 5852 break; 5853 case nfserr_share_denied: 5854 if (nfs4_resolve_deny_conflicts_locked(fp, false, 5855 stp, open->op_share_deny, false)) 5856 status = nfserr_jukebox; 5857 break; 5858 } 5859 spin_unlock(&fp->fi_lock); 5860 5861 if (status != nfs_ok) 5862 return status; 5863 5864 status = nfsd4_truncate(rqstp, cur_fh, open); 5865 if (status != nfs_ok) 5866 reset_union_bmap_deny(old_deny_bmap, stp); 5867 return status; 5868 } 5869 5870 /* Should we give out recallable state?: */ 5871 static bool nfsd4_cb_channel_good(struct nfs4_client *clp) 5872 { 5873 if (clp->cl_cb_state == NFSD4_CB_UP) 5874 return true; 5875 /* 5876 * In the sessions case, since we don't have to establish a 5877 * separate connection for callbacks, we assume it's OK 5878 * until we hear otherwise: 5879 */ 5880 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; 5881 } 5882 5883 static struct file_lease *nfs4_alloc_init_lease(struct nfs4_delegation *dp) 5884 { 5885 struct file_lease *fl; 5886 5887 fl = locks_alloc_lease(); 5888 if (!fl) 5889 return NULL; 5890 fl->fl_lmops = &nfsd_lease_mng_ops; 5891 fl->c.flc_flags = FL_DELEG; 5892 fl->c.flc_type = deleg_is_read(dp->dl_type) ? F_RDLCK : F_WRLCK; 5893 fl->c.flc_owner = (fl_owner_t)dp; 5894 fl->c.flc_pid = current->tgid; 5895 fl->c.flc_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file; 5896 return fl; 5897 } 5898 5899 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, 5900 struct nfs4_file *fp) 5901 { 5902 struct nfs4_ol_stateid *st; 5903 struct file *f = fp->fi_deleg_file->nf_file; 5904 struct inode *ino = file_inode(f); 5905 int writes; 5906 5907 writes = atomic_read(&ino->i_writecount); 5908 if (!writes) 5909 return 0; 5910 /* 5911 * There could be multiple filehandles (hence multiple 5912 * nfs4_files) referencing this file, but that's not too 5913 * common; let's just give up in that case rather than 5914 * trying to go look up all the clients using that other 5915 * nfs4_file as well: 5916 */ 5917 if (fp->fi_aliased) 5918 return -EAGAIN; 5919 /* 5920 * If there's a close in progress, make sure that we see it 5921 * clear any fi_fds[] entries before we see it decrement 5922 * i_writecount: 5923 */ 5924 smp_mb__after_atomic(); 5925 5926 if (fp->fi_fds[O_WRONLY]) 5927 writes--; 5928 if (fp->fi_fds[O_RDWR]) 5929 writes--; 5930 if (writes > 0) 5931 return -EAGAIN; /* There may be non-NFSv4 writers */ 5932 /* 5933 * It's possible there are non-NFSv4 write opens in progress, 5934 * but if they haven't incremented i_writecount yet then they 5935 * also haven't called break lease yet; so, they'll break this 5936 * lease soon enough. So, all that's left to check for is NFSv4 5937 * opens: 5938 */ 5939 spin_lock(&fp->fi_lock); 5940 list_for_each_entry(st, &fp->fi_stateids, st_perfile) { 5941 if (st->st_openstp == NULL /* it's an open */ && 5942 access_permit_write(st) && 5943 st->st_stid.sc_client != clp) { 5944 spin_unlock(&fp->fi_lock); 5945 return -EAGAIN; 5946 } 5947 } 5948 spin_unlock(&fp->fi_lock); 5949 /* 5950 * There's a small chance that we could be racing with another 5951 * NFSv4 open. However, any open that hasn't added itself to 5952 * the fi_stateids list also hasn't called break_lease yet; so, 5953 * they'll break this lease soon enough. 5954 */ 5955 return 0; 5956 } 5957 5958 /* 5959 * It's possible that between opening the dentry and setting the delegation, 5960 * that it has been renamed or unlinked. Redo the lookup to verify that this 5961 * hasn't happened. 5962 */ 5963 static int 5964 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp, 5965 struct svc_fh *parent) 5966 { 5967 struct svc_export *exp; 5968 struct dentry *child; 5969 __be32 err; 5970 5971 err = nfsd_lookup_dentry(open->op_rqstp, parent, 5972 open->op_fname, open->op_fnamelen, 5973 &exp, &child); 5974 5975 if (err) 5976 return -EAGAIN; 5977 5978 exp_put(exp); 5979 dput(child); 5980 if (child != file_dentry(fp->fi_deleg_file->nf_file)) 5981 return -EAGAIN; 5982 5983 return 0; 5984 } 5985 5986 /* 5987 * We avoid breaking delegations held by a client due to its own activity, but 5988 * clearing setuid/setgid bits on a write is an implicit activity and the client 5989 * may not notice and continue using the old mode. Avoid giving out a delegation 5990 * on setuid/setgid files when the client is requesting an open for write. 5991 */ 5992 static int 5993 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf) 5994 { 5995 struct inode *inode = file_inode(nf->nf_file); 5996 5997 if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) && 5998 (inode->i_mode & (S_ISUID|S_ISGID))) 5999 return -EAGAIN; 6000 return 0; 6001 } 6002 6003 #ifdef CONFIG_NFSD_V4_DELEG_TIMESTAMPS 6004 static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open) 6005 { 6006 return open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS; 6007 } 6008 #else /* CONFIG_NFSD_V4_DELEG_TIMESTAMPS */ 6009 static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open) 6010 { 6011 return false; 6012 } 6013 #endif /* CONFIG NFSD_V4_DELEG_TIMESTAMPS */ 6014 6015 static struct nfs4_delegation * 6016 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, 6017 struct svc_fh *parent) 6018 { 6019 bool deleg_ts = nfsd4_want_deleg_timestamps(open); 6020 struct nfs4_client *clp = stp->st_stid.sc_client; 6021 struct nfs4_file *fp = stp->st_stid.sc_file; 6022 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate; 6023 struct nfs4_delegation *dp; 6024 struct nfsd_file *nf = NULL; 6025 struct file_lease *fl; 6026 int status = 0; 6027 u32 dl_type; 6028 6029 /* 6030 * The fi_had_conflict and nfs_get_existing_delegation checks 6031 * here are just optimizations; we'll need to recheck them at 6032 * the end: 6033 */ 6034 if (fp->fi_had_conflict) 6035 return ERR_PTR(-EAGAIN); 6036 6037 /* 6038 * Try for a write delegation first. RFC8881 section 10.4 says: 6039 * 6040 * "An OPEN_DELEGATE_WRITE delegation allows the client to handle, 6041 * on its own, all opens." 6042 * 6043 * Furthermore, section 9.1.2 says: 6044 * 6045 * "In the case of READ, the server may perform the corresponding 6046 * check on the access mode, or it may choose to allow READ for 6047 * OPEN4_SHARE_ACCESS_WRITE, to accommodate clients whose WRITE 6048 * implementation may unavoidably do reads (e.g., due to buffer 6049 * cache constraints)." 6050 * 6051 * We choose to offer a write delegation for OPEN with the 6052 * OPEN4_SHARE_ACCESS_WRITE access mode to accommodate such clients. 6053 */ 6054 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) { 6055 nf = find_writeable_file(fp); 6056 dl_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG : OPEN_DELEGATE_WRITE; 6057 } 6058 6059 /* 6060 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR 6061 * file for some reason, then try for a read delegation instead. 6062 */ 6063 if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) { 6064 nf = find_readable_file(fp); 6065 dl_type = deleg_ts ? OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ; 6066 } 6067 6068 if (!nf) 6069 return ERR_PTR(-EAGAIN); 6070 6071 /* 6072 * File delegations and associated locks cannot be recovered if the 6073 * export is from an NFS proxy server. 6074 */ 6075 if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) { 6076 nfsd_file_put(nf); 6077 return ERR_PTR(-EOPNOTSUPP); 6078 } 6079 6080 spin_lock(&state_lock); 6081 spin_lock(&fp->fi_lock); 6082 if (nfs4_delegation_exists(clp, fp)) 6083 status = -EAGAIN; 6084 else if (nfsd4_verify_setuid_write(open, nf)) 6085 status = -EAGAIN; 6086 else if (!fp->fi_deleg_file) { 6087 fp->fi_deleg_file = nf; 6088 /* increment early to prevent fi_deleg_file from being 6089 * cleared */ 6090 fp->fi_delegees = 1; 6091 nf = NULL; 6092 } else 6093 fp->fi_delegees++; 6094 spin_unlock(&fp->fi_lock); 6095 spin_unlock(&state_lock); 6096 if (nf) 6097 nfsd_file_put(nf); 6098 if (status) 6099 return ERR_PTR(status); 6100 6101 status = -ENOMEM; 6102 dp = alloc_init_deleg(clp, fp, odstate, dl_type); 6103 if (!dp) 6104 goto out_delegees; 6105 6106 fl = nfs4_alloc_init_lease(dp); 6107 if (!fl) 6108 goto out_clnt_odstate; 6109 6110 status = kernel_setlease(fp->fi_deleg_file->nf_file, 6111 fl->c.flc_type, &fl, NULL); 6112 if (fl) 6113 locks_free_lease(fl); 6114 if (status) 6115 goto out_clnt_odstate; 6116 6117 if (parent) { 6118 status = nfsd4_verify_deleg_dentry(open, fp, parent); 6119 if (status) 6120 goto out_unlock; 6121 } 6122 6123 status = nfsd4_check_conflicting_opens(clp, fp); 6124 if (status) 6125 goto out_unlock; 6126 6127 /* 6128 * Now that the deleg is set, check again to ensure that nothing 6129 * raced in and changed the mode while we weren't looking. 6130 */ 6131 status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file); 6132 if (status) 6133 goto out_unlock; 6134 6135 status = -EAGAIN; 6136 if (fp->fi_had_conflict) 6137 goto out_unlock; 6138 6139 spin_lock(&state_lock); 6140 spin_lock(&clp->cl_lock); 6141 spin_lock(&fp->fi_lock); 6142 status = hash_delegation_locked(dp, fp); 6143 spin_unlock(&fp->fi_lock); 6144 spin_unlock(&clp->cl_lock); 6145 spin_unlock(&state_lock); 6146 6147 if (status) 6148 goto out_unlock; 6149 6150 return dp; 6151 out_unlock: 6152 kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp); 6153 out_clnt_odstate: 6154 put_clnt_odstate(dp->dl_clnt_odstate); 6155 nfs4_put_stid(&dp->dl_stid); 6156 out_delegees: 6157 put_deleg_file(fp); 6158 return ERR_PTR(status); 6159 } 6160 6161 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) 6162 { 6163 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT; 6164 if (status == -EAGAIN) 6165 open->op_why_no_deleg = WND4_CONTENTION; 6166 else { 6167 open->op_why_no_deleg = WND4_RESOURCE; 6168 switch (open->op_deleg_want) { 6169 case OPEN4_SHARE_ACCESS_WANT_READ_DELEG: 6170 case OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG: 6171 case OPEN4_SHARE_ACCESS_WANT_ANY_DELEG: 6172 break; 6173 case OPEN4_SHARE_ACCESS_WANT_CANCEL: 6174 open->op_why_no_deleg = WND4_CANCELLED; 6175 break; 6176 case OPEN4_SHARE_ACCESS_WANT_NO_DELEG: 6177 WARN_ON_ONCE(1); 6178 } 6179 } 6180 } 6181 6182 static bool 6183 nfs4_delegation_stat(struct nfs4_delegation *dp, struct svc_fh *currentfh, 6184 struct kstat *stat) 6185 { 6186 struct nfsd_file *nf = find_writeable_file(dp->dl_stid.sc_file); 6187 struct path path; 6188 int rc; 6189 6190 if (!nf) 6191 return false; 6192 6193 path.mnt = currentfh->fh_export->ex_path.mnt; 6194 path.dentry = file_dentry(nf->nf_file); 6195 6196 rc = vfs_getattr(&path, stat, 6197 STATX_MODE | STATX_SIZE | STATX_ATIME | 6198 STATX_MTIME | STATX_CTIME | STATX_CHANGE_COOKIE, 6199 AT_STATX_SYNC_AS_STAT); 6200 6201 nfsd_file_put(nf); 6202 return rc == 0; 6203 } 6204 6205 /* 6206 * Add NFS4_SHARE_ACCESS_READ to the write delegation granted on OPEN 6207 * with NFS4_SHARE_ACCESS_WRITE by allocating separate nfsd_file and 6208 * struct file to be used for read with delegation stateid. 6209 * 6210 */ 6211 static bool 6212 nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst *rqstp, struct nfsd4_open *open, 6213 struct svc_fh *fh, struct nfs4_ol_stateid *stp) 6214 { 6215 struct nfs4_file *fp; 6216 struct nfsd_file *nf = NULL; 6217 6218 if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == 6219 NFS4_SHARE_ACCESS_WRITE) { 6220 if (nfsd_file_acquire_opened(rqstp, fh, NFSD_MAY_READ, NULL, &nf)) 6221 return (false); 6222 fp = stp->st_stid.sc_file; 6223 spin_lock(&fp->fi_lock); 6224 __nfs4_file_get_access(fp, NFS4_SHARE_ACCESS_READ); 6225 fp = stp->st_stid.sc_file; 6226 fp->fi_fds[O_RDONLY] = nf; 6227 fp->fi_rdeleg_file = nf; 6228 spin_unlock(&fp->fi_lock); 6229 } 6230 return true; 6231 } 6232 6233 /* 6234 * The Linux NFS server does not offer write delegations to NFSv4.0 6235 * clients in order to avoid conflicts between write delegations and 6236 * GETATTRs requesting CHANGE or SIZE attributes. 6237 * 6238 * With NFSv4.1 and later minorversions, the SEQUENCE operation that 6239 * begins each COMPOUND contains a client ID. Delegation recall can 6240 * be avoided when the server recognizes the client sending a 6241 * GETATTR also holds write delegation it conflicts with. 6242 * 6243 * However, the NFSv4.0 protocol does not enable a server to 6244 * determine that a GETATTR originated from the client holding the 6245 * conflicting delegation versus coming from some other client. Per 6246 * RFC 7530 Section 16.7.5, the server must recall or send a 6247 * CB_GETATTR even when the GETATTR originates from the client that 6248 * holds the conflicting delegation. 6249 * 6250 * An NFSv4.0 client can trigger a pathological situation if it 6251 * always sends a DELEGRETURN preceded by a conflicting GETATTR in 6252 * the same COMPOUND. COMPOUND execution will always stop at the 6253 * GETATTR and the DELEGRETURN will never get executed. The server 6254 * eventually revokes the delegation, which can result in loss of 6255 * open or lock state. 6256 */ 6257 static void 6258 nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open, 6259 struct nfs4_ol_stateid *stp, struct svc_fh *currentfh, 6260 struct svc_fh *fh) 6261 { 6262 struct nfs4_openowner *oo = openowner(stp->st_stateowner); 6263 bool deleg_ts = nfsd4_want_deleg_timestamps(open); 6264 struct nfs4_client *clp = stp->st_stid.sc_client; 6265 struct svc_fh *parent = NULL; 6266 struct nfs4_delegation *dp; 6267 struct kstat stat; 6268 int status = 0; 6269 int cb_up; 6270 6271 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); 6272 open->op_recall = false; 6273 switch (open->op_claim_type) { 6274 case NFS4_OPEN_CLAIM_PREVIOUS: 6275 if (!cb_up) 6276 open->op_recall = true; 6277 break; 6278 case NFS4_OPEN_CLAIM_NULL: 6279 parent = currentfh; 6280 fallthrough; 6281 case NFS4_OPEN_CLAIM_FH: 6282 /* 6283 * Let's not give out any delegations till everyone's 6284 * had the chance to reclaim theirs, *and* until 6285 * NLM locks have all been reclaimed: 6286 */ 6287 if (locks_in_grace(clp->net)) 6288 goto out_no_deleg; 6289 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) 6290 goto out_no_deleg; 6291 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE && 6292 !clp->cl_minorversion) 6293 goto out_no_deleg; 6294 break; 6295 default: 6296 goto out_no_deleg; 6297 } 6298 dp = nfs4_set_delegation(open, stp, parent); 6299 if (IS_ERR(dp)) 6300 goto out_no_deleg; 6301 6302 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); 6303 6304 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) { 6305 struct file *f = dp->dl_stid.sc_file->fi_deleg_file->nf_file; 6306 6307 if (!nfsd4_add_rdaccess_to_wrdeleg(rqstp, open, fh, stp) || 6308 !nfs4_delegation_stat(dp, currentfh, &stat)) { 6309 nfs4_put_stid(&dp->dl_stid); 6310 destroy_delegation(dp); 6311 goto out_no_deleg; 6312 } 6313 open->op_delegate_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG : 6314 OPEN_DELEGATE_WRITE; 6315 dp->dl_cb_fattr.ncf_cur_fsize = stat.size; 6316 dp->dl_cb_fattr.ncf_initial_cinfo = nfsd4_change_attribute(&stat); 6317 dp->dl_atime = stat.atime; 6318 dp->dl_ctime = stat.ctime; 6319 dp->dl_mtime = stat.mtime; 6320 spin_lock(&f->f_lock); 6321 f->f_mode |= FMODE_NOCMTIME; 6322 spin_unlock(&f->f_lock); 6323 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid); 6324 } else { 6325 open->op_delegate_type = deleg_ts && nfs4_delegation_stat(dp, currentfh, &stat) ? 6326 OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ; 6327 dp->dl_atime = stat.atime; 6328 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid); 6329 } 6330 nfs4_put_stid(&dp->dl_stid); 6331 return; 6332 out_no_deleg: 6333 open->op_delegate_type = OPEN_DELEGATE_NONE; 6334 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && 6335 open->op_delegate_type != OPEN_DELEGATE_NONE) { 6336 dprintk("NFSD: WARNING: refusing delegation reclaim\n"); 6337 open->op_recall = true; 6338 } 6339 6340 /* 4.1 client asking for a delegation? */ 6341 if (open->op_deleg_want) 6342 nfsd4_open_deleg_none_ext(open, status); 6343 return; 6344 } 6345 6346 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, 6347 struct nfs4_delegation *dp) 6348 { 6349 if (deleg_is_write(dp->dl_type)) { 6350 if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_READ_DELEG) { 6351 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT; 6352 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; 6353 } else if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG) { 6354 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT; 6355 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; 6356 } 6357 } 6358 /* Otherwise the client must be confused wanting a delegation 6359 * it already has, therefore we don't return 6360 * OPEN_DELEGATE_NONE_EXT and reason. 6361 */ 6362 } 6363 6364 /* Are we returning only a delegation stateid? */ 6365 static bool open_xor_delegation(struct nfsd4_open *open) 6366 { 6367 if (!(open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION)) 6368 return false; 6369 /* Did we actually get a delegation? */ 6370 if (!deleg_is_read(open->op_delegate_type) && !deleg_is_write(open->op_delegate_type)) 6371 return false; 6372 return true; 6373 } 6374 6375 /** 6376 * nfsd4_process_open2 - finish open processing 6377 * @rqstp: the RPC transaction being executed 6378 * @current_fh: NFSv4 COMPOUND's current filehandle 6379 * @open: OPEN arguments 6380 * 6381 * If successful, (1) truncate the file if open->op_truncate was 6382 * set, (2) set open->op_stateid, (3) set open->op_delegation. 6383 * 6384 * Returns %nfs_ok on success; otherwise an nfs4stat value in 6385 * network byte order is returned. 6386 */ 6387 __be32 6388 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) 6389 { 6390 struct nfsd4_compoundres *resp = rqstp->rq_resp; 6391 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; 6392 struct nfs4_file *fp = NULL; 6393 struct nfs4_ol_stateid *stp = NULL; 6394 struct nfs4_delegation *dp = NULL; 6395 __be32 status; 6396 bool new_stp = false; 6397 6398 /* 6399 * Lookup file; if found, lookup stateid and check open request, 6400 * and check for delegations in the process of being recalled. 6401 * If not found, create the nfs4_file struct 6402 */ 6403 fp = nfsd4_file_hash_insert(open->op_file, current_fh); 6404 if (unlikely(!fp)) 6405 return nfserr_jukebox; 6406 if (fp != open->op_file) { 6407 status = nfs4_check_deleg(cl, open, &dp); 6408 if (status) 6409 goto out; 6410 if (dp && nfsd4_is_deleg_cur(open) && 6411 (dp->dl_stid.sc_file != fp)) { 6412 /* 6413 * RFC8881 section 8.2.4 mandates the server to return 6414 * NFS4ERR_BAD_STATEID if the selected table entry does 6415 * not match the current filehandle. However returning 6416 * NFS4ERR_BAD_STATEID in the OPEN can cause the client 6417 * to repeatedly retry the operation with the same 6418 * stateid, since the stateid itself is valid. To avoid 6419 * this situation NFSD returns NFS4ERR_INVAL instead. 6420 */ 6421 status = nfserr_inval; 6422 goto out; 6423 } 6424 stp = nfsd4_find_and_lock_existing_open(fp, open); 6425 } else { 6426 open->op_file = NULL; 6427 status = nfserr_bad_stateid; 6428 if (nfsd4_is_deleg_cur(open)) 6429 goto out; 6430 } 6431 6432 if (!stp) { 6433 stp = init_open_stateid(fp, open); 6434 if (!stp) { 6435 status = nfserr_jukebox; 6436 goto out; 6437 } 6438 6439 if (!open->op_stp) 6440 new_stp = true; 6441 } 6442 6443 /* 6444 * OPEN the file, or upgrade an existing OPEN. 6445 * If truncate fails, the OPEN fails. 6446 * 6447 * stp is already locked. 6448 */ 6449 if (!new_stp) { 6450 /* Stateid was found, this is an OPEN upgrade */ 6451 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); 6452 if (status) { 6453 mutex_unlock(&stp->st_mutex); 6454 goto out; 6455 } 6456 } else { 6457 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true); 6458 if (status) { 6459 release_open_stateid(stp); 6460 mutex_unlock(&stp->st_mutex); 6461 goto out; 6462 } 6463 6464 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, 6465 open->op_odstate); 6466 if (stp->st_clnt_odstate == open->op_odstate) 6467 open->op_odstate = NULL; 6468 } 6469 6470 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); 6471 mutex_unlock(&stp->st_mutex); 6472 6473 if (nfsd4_has_session(&resp->cstate)) { 6474 if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_NO_DELEG) { 6475 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT; 6476 open->op_why_no_deleg = WND4_NOT_WANTED; 6477 goto nodeleg; 6478 } 6479 } 6480 6481 /* 6482 * Attempt to hand out a delegation. No error return, because the 6483 * OPEN succeeds even if we fail. 6484 */ 6485 nfs4_open_delegation(rqstp, open, stp, 6486 &resp->cstate.current_fh, current_fh); 6487 6488 /* 6489 * If there is an existing open stateid, it must be updated and 6490 * returned. Only respect WANT_OPEN_XOR_DELEGATION when a new 6491 * open stateid would have to be created. 6492 */ 6493 if (new_stp && open_xor_delegation(open)) { 6494 memcpy(&open->op_stateid, &zero_stateid, sizeof(open->op_stateid)); 6495 open->op_rflags |= OPEN4_RESULT_NO_OPEN_STATEID; 6496 release_open_stateid(stp); 6497 } 6498 nodeleg: 6499 status = nfs_ok; 6500 trace_nfsd_open(&stp->st_stid.sc_stateid); 6501 out: 6502 /* 4.1 client trying to upgrade/downgrade delegation? */ 6503 if (open->op_delegate_type == OPEN_DELEGATE_NONE && dp && 6504 open->op_deleg_want) 6505 nfsd4_deleg_xgrade_none_ext(open, dp); 6506 6507 if (fp) 6508 put_nfs4_file(fp); 6509 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) 6510 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 6511 /* 6512 * To finish the open response, we just need to set the rflags. 6513 */ 6514 open->op_rflags |= NFS4_OPEN_RESULT_LOCKTYPE_POSIX; 6515 if (nfsd4_has_session(&resp->cstate)) 6516 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; 6517 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) 6518 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; 6519 6520 if (dp) 6521 nfs4_put_stid(&dp->dl_stid); 6522 if (stp) 6523 nfs4_put_stid(&stp->st_stid); 6524 6525 return status; 6526 } 6527 6528 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, 6529 struct nfsd4_open *open) 6530 { 6531 if (open->op_openowner) 6532 nfs4_put_stateowner(&open->op_openowner->oo_owner); 6533 if (open->op_file) 6534 kmem_cache_free(file_slab, open->op_file); 6535 if (open->op_stp) 6536 nfs4_put_stid(&open->op_stp->st_stid); 6537 if (open->op_odstate) 6538 kmem_cache_free(odstate_slab, open->op_odstate); 6539 } 6540 6541 __be32 6542 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 6543 union nfsd4_op_u *u) 6544 { 6545 clientid_t *clid = &u->renew; 6546 struct nfs4_client *clp; 6547 __be32 status; 6548 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 6549 6550 trace_nfsd_clid_renew(clid); 6551 status = set_client(clid, cstate, nn); 6552 if (status) 6553 return status; 6554 clp = cstate->clp; 6555 if (!list_empty(&clp->cl_delegations) 6556 && clp->cl_cb_state != NFSD4_CB_UP) 6557 return nfserr_cb_path_down; 6558 return nfs_ok; 6559 } 6560 6561 void 6562 nfsd4_end_grace(struct nfsd_net *nn) 6563 { 6564 /* do nothing if grace period already ended */ 6565 if (nn->grace_ended) 6566 return; 6567 6568 trace_nfsd_grace_complete(nn); 6569 nn->grace_ended = true; 6570 /* 6571 * If the server goes down again right now, an NFSv4 6572 * client will still be allowed to reclaim after it comes back up, 6573 * even if it hasn't yet had a chance to reclaim state this time. 6574 * 6575 */ 6576 nfsd4_record_grace_done(nn); 6577 /* 6578 * At this point, NFSv4 clients can still reclaim. But if the 6579 * server crashes, any that have not yet reclaimed will be out 6580 * of luck on the next boot. 6581 * 6582 * (NFSv4.1+ clients are considered to have reclaimed once they 6583 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to 6584 * have reclaimed after their first OPEN.) 6585 */ 6586 locks_end_grace(&nn->nfsd4_manager); 6587 /* 6588 * At this point, and once lockd and/or any other containers 6589 * exit their grace period, further reclaims will fail and 6590 * regular locking can resume. 6591 */ 6592 } 6593 6594 /* 6595 * If we've waited a lease period but there are still clients trying to 6596 * reclaim, wait a little longer to give them a chance to finish. 6597 */ 6598 static bool clients_still_reclaiming(struct nfsd_net *nn) 6599 { 6600 time64_t double_grace_period_end = nn->boot_time + 6601 2 * nn->nfsd4_lease; 6602 6603 if (nn->track_reclaim_completes && 6604 atomic_read(&nn->nr_reclaim_complete) == 6605 nn->reclaim_str_hashtbl_size) 6606 return false; 6607 if (!nn->somebody_reclaimed) 6608 return false; 6609 nn->somebody_reclaimed = false; 6610 /* 6611 * If we've given them *two* lease times to reclaim, and they're 6612 * still not done, give up: 6613 */ 6614 if (ktime_get_boottime_seconds() > double_grace_period_end) 6615 return false; 6616 return true; 6617 } 6618 6619 struct laundry_time { 6620 time64_t cutoff; 6621 time64_t new_timeo; 6622 }; 6623 6624 static bool state_expired(struct laundry_time *lt, time64_t last_refresh) 6625 { 6626 time64_t time_remaining; 6627 6628 if (last_refresh < lt->cutoff) 6629 return true; 6630 time_remaining = last_refresh - lt->cutoff; 6631 lt->new_timeo = min(lt->new_timeo, time_remaining); 6632 return false; 6633 } 6634 6635 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 6636 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn) 6637 { 6638 spin_lock_init(&nn->nfsd_ssc_lock); 6639 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list); 6640 init_waitqueue_head(&nn->nfsd_ssc_waitq); 6641 } 6642 6643 /* 6644 * This is called when nfsd is being shutdown, after all inter_ssc 6645 * cleanup were done, to destroy the ssc delayed unmount list. 6646 */ 6647 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn) 6648 { 6649 struct nfsd4_ssc_umount_item *ni = NULL; 6650 struct nfsd4_ssc_umount_item *tmp; 6651 6652 spin_lock(&nn->nfsd_ssc_lock); 6653 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { 6654 list_del(&ni->nsui_list); 6655 spin_unlock(&nn->nfsd_ssc_lock); 6656 mntput(ni->nsui_vfsmount); 6657 kfree(ni); 6658 spin_lock(&nn->nfsd_ssc_lock); 6659 } 6660 spin_unlock(&nn->nfsd_ssc_lock); 6661 } 6662 6663 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn) 6664 { 6665 bool do_wakeup = false; 6666 struct nfsd4_ssc_umount_item *ni = NULL; 6667 struct nfsd4_ssc_umount_item *tmp; 6668 6669 spin_lock(&nn->nfsd_ssc_lock); 6670 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { 6671 if (time_after(jiffies, ni->nsui_expire)) { 6672 if (refcount_read(&ni->nsui_refcnt) > 1) 6673 continue; 6674 6675 /* mark being unmount */ 6676 ni->nsui_busy = true; 6677 spin_unlock(&nn->nfsd_ssc_lock); 6678 mntput(ni->nsui_vfsmount); 6679 spin_lock(&nn->nfsd_ssc_lock); 6680 6681 /* waiters need to start from begin of list */ 6682 list_del(&ni->nsui_list); 6683 kfree(ni); 6684 6685 /* wakeup ssc_connect waiters */ 6686 do_wakeup = true; 6687 continue; 6688 } 6689 break; 6690 } 6691 if (do_wakeup) 6692 wake_up_all(&nn->nfsd_ssc_waitq); 6693 spin_unlock(&nn->nfsd_ssc_lock); 6694 } 6695 #endif 6696 6697 /* Check if any lock belonging to this lockowner has any blockers */ 6698 static bool 6699 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo) 6700 { 6701 struct file_lock_context *ctx; 6702 struct nfs4_ol_stateid *stp; 6703 struct nfs4_file *nf; 6704 6705 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { 6706 nf = stp->st_stid.sc_file; 6707 ctx = locks_inode_context(nf->fi_inode); 6708 if (!ctx) 6709 continue; 6710 if (locks_owner_has_blockers(ctx, lo)) 6711 return true; 6712 } 6713 return false; 6714 } 6715 6716 static bool 6717 nfs4_anylock_blockers(struct nfs4_client *clp) 6718 { 6719 int i; 6720 struct nfs4_stateowner *so; 6721 struct nfs4_lockowner *lo; 6722 6723 if (atomic_read(&clp->cl_delegs_in_recall)) 6724 return true; 6725 spin_lock(&clp->cl_lock); 6726 for (i = 0; i < OWNER_HASH_SIZE; i++) { 6727 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i], 6728 so_strhash) { 6729 if (so->so_is_open_owner) 6730 continue; 6731 lo = lockowner(so); 6732 if (nfs4_lockowner_has_blockers(lo)) { 6733 spin_unlock(&clp->cl_lock); 6734 return true; 6735 } 6736 } 6737 } 6738 spin_unlock(&clp->cl_lock); 6739 return false; 6740 } 6741 6742 static void 6743 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist, 6744 struct laundry_time *lt) 6745 { 6746 unsigned int maxreap, reapcnt = 0; 6747 struct list_head *pos, *next; 6748 struct nfs4_client *clp; 6749 6750 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ? 6751 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0; 6752 INIT_LIST_HEAD(reaplist); 6753 spin_lock(&nn->client_lock); 6754 list_for_each_safe(pos, next, &nn->client_lru) { 6755 clp = list_entry(pos, struct nfs4_client, cl_lru); 6756 if (clp->cl_state == NFSD4_EXPIRABLE) 6757 goto exp_client; 6758 if (!state_expired(lt, clp->cl_time)) 6759 break; 6760 if (!atomic_read(&clp->cl_rpc_users)) { 6761 if (clp->cl_state == NFSD4_ACTIVE) 6762 atomic_inc(&nn->nfsd_courtesy_clients); 6763 clp->cl_state = NFSD4_COURTESY; 6764 } 6765 if (!client_has_state(clp)) 6766 goto exp_client; 6767 if (!nfs4_anylock_blockers(clp)) 6768 if (reapcnt >= maxreap) 6769 continue; 6770 exp_client: 6771 if (!mark_client_expired_locked(clp)) { 6772 list_add(&clp->cl_lru, reaplist); 6773 reapcnt++; 6774 } 6775 } 6776 spin_unlock(&nn->client_lock); 6777 } 6778 6779 static void 6780 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn, 6781 struct list_head *reaplist) 6782 { 6783 unsigned int maxreap = 0, reapcnt = 0; 6784 struct list_head *pos, *next; 6785 struct nfs4_client *clp; 6786 6787 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN; 6788 INIT_LIST_HEAD(reaplist); 6789 6790 spin_lock(&nn->client_lock); 6791 list_for_each_safe(pos, next, &nn->client_lru) { 6792 clp = list_entry(pos, struct nfs4_client, cl_lru); 6793 if (clp->cl_state == NFSD4_ACTIVE) 6794 break; 6795 if (reapcnt >= maxreap) 6796 break; 6797 if (!mark_client_expired_locked(clp)) { 6798 list_add(&clp->cl_lru, reaplist); 6799 reapcnt++; 6800 } 6801 } 6802 spin_unlock(&nn->client_lock); 6803 } 6804 6805 static void 6806 nfs4_process_client_reaplist(struct list_head *reaplist) 6807 { 6808 struct list_head *pos, *next; 6809 struct nfs4_client *clp; 6810 6811 list_for_each_safe(pos, next, reaplist) { 6812 clp = list_entry(pos, struct nfs4_client, cl_lru); 6813 trace_nfsd_clid_purged(&clp->cl_clientid); 6814 list_del_init(&clp->cl_lru); 6815 expire_client(clp); 6816 } 6817 } 6818 6819 static void nfs40_clean_admin_revoked(struct nfsd_net *nn, 6820 struct laundry_time *lt) 6821 { 6822 struct nfs4_client *clp; 6823 6824 spin_lock(&nn->client_lock); 6825 if (nn->nfs40_last_revoke == 0 || 6826 nn->nfs40_last_revoke > lt->cutoff) { 6827 spin_unlock(&nn->client_lock); 6828 return; 6829 } 6830 nn->nfs40_last_revoke = 0; 6831 6832 retry: 6833 list_for_each_entry(clp, &nn->client_lru, cl_lru) { 6834 unsigned long id, tmp; 6835 struct nfs4_stid *stid; 6836 6837 if (atomic_read(&clp->cl_admin_revoked) == 0) 6838 continue; 6839 6840 spin_lock(&clp->cl_lock); 6841 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id) 6842 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) { 6843 refcount_inc(&stid->sc_count); 6844 spin_unlock(&nn->client_lock); 6845 /* this function drops ->cl_lock */ 6846 nfsd4_drop_revoked_stid(stid); 6847 nfs4_put_stid(stid); 6848 spin_lock(&nn->client_lock); 6849 goto retry; 6850 } 6851 spin_unlock(&clp->cl_lock); 6852 } 6853 spin_unlock(&nn->client_lock); 6854 } 6855 6856 static time64_t 6857 nfs4_laundromat(struct nfsd_net *nn) 6858 { 6859 struct nfs4_openowner *oo; 6860 struct nfs4_delegation *dp; 6861 struct nfs4_ol_stateid *stp; 6862 struct nfsd4_blocked_lock *nbl; 6863 struct list_head *pos, *next, reaplist; 6864 struct laundry_time lt = { 6865 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease, 6866 .new_timeo = nn->nfsd4_lease 6867 }; 6868 struct nfs4_cpntf_state *cps; 6869 copy_stateid_t *cps_t; 6870 int i; 6871 6872 if (clients_still_reclaiming(nn)) { 6873 lt.new_timeo = 0; 6874 goto out; 6875 } 6876 nfsd4_end_grace(nn); 6877 6878 spin_lock(&nn->s2s_cp_lock); 6879 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { 6880 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); 6881 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID && 6882 state_expired(<, cps->cpntf_time)) 6883 _free_cpntf_state_locked(nn, cps); 6884 } 6885 spin_unlock(&nn->s2s_cp_lock); 6886 nfsd4_async_copy_reaper(nn); 6887 nfs4_get_client_reaplist(nn, &reaplist, <); 6888 nfs4_process_client_reaplist(&reaplist); 6889 6890 nfs40_clean_admin_revoked(nn, <); 6891 6892 spin_lock(&state_lock); 6893 list_for_each_safe(pos, next, &nn->del_recall_lru) { 6894 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 6895 if (!state_expired(<, dp->dl_time)) 6896 break; 6897 refcount_inc(&dp->dl_stid.sc_count); 6898 unhash_delegation_locked(dp, SC_STATUS_REVOKED); 6899 list_add(&dp->dl_recall_lru, &reaplist); 6900 } 6901 spin_unlock(&state_lock); 6902 while (!list_empty(&reaplist)) { 6903 dp = list_first_entry(&reaplist, struct nfs4_delegation, 6904 dl_recall_lru); 6905 list_del_init(&dp->dl_recall_lru); 6906 revoke_delegation(dp); 6907 } 6908 6909 spin_lock(&nn->client_lock); 6910 while (!list_empty(&nn->close_lru)) { 6911 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, 6912 oo_close_lru); 6913 if (!state_expired(<, oo->oo_time)) 6914 break; 6915 list_del_init(&oo->oo_close_lru); 6916 stp = oo->oo_last_closed_stid; 6917 oo->oo_last_closed_stid = NULL; 6918 spin_unlock(&nn->client_lock); 6919 nfs4_put_stid(&stp->st_stid); 6920 spin_lock(&nn->client_lock); 6921 } 6922 spin_unlock(&nn->client_lock); 6923 6924 /* 6925 * It's possible for a client to try and acquire an already held lock 6926 * that is being held for a long time, and then lose interest in it. 6927 * So, we clean out any un-revisited request after a lease period 6928 * under the assumption that the client is no longer interested. 6929 * 6930 * RFC5661, sec. 9.6 states that the client must not rely on getting 6931 * notifications and must continue to poll for locks, even when the 6932 * server supports them. Thus this shouldn't lead to clients blocking 6933 * indefinitely once the lock does become free. 6934 */ 6935 BUG_ON(!list_empty(&reaplist)); 6936 spin_lock(&nn->blocked_locks_lock); 6937 while (!list_empty(&nn->blocked_locks_lru)) { 6938 nbl = list_first_entry(&nn->blocked_locks_lru, 6939 struct nfsd4_blocked_lock, nbl_lru); 6940 if (!state_expired(<, nbl->nbl_time)) 6941 break; 6942 list_move(&nbl->nbl_lru, &reaplist); 6943 list_del_init(&nbl->nbl_list); 6944 } 6945 spin_unlock(&nn->blocked_locks_lock); 6946 6947 while (!list_empty(&reaplist)) { 6948 nbl = list_first_entry(&reaplist, 6949 struct nfsd4_blocked_lock, nbl_lru); 6950 list_del_init(&nbl->nbl_lru); 6951 free_blocked_lock(nbl); 6952 } 6953 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 6954 /* service the server-to-server copy delayed unmount list */ 6955 nfsd4_ssc_expire_umount(nn); 6956 #endif 6957 if (atomic_long_read(&num_delegations) >= max_delegations) 6958 deleg_reaper(nn); 6959 out: 6960 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); 6961 } 6962 6963 static void laundromat_main(struct work_struct *); 6964 6965 static void 6966 laundromat_main(struct work_struct *laundry) 6967 { 6968 time64_t t; 6969 struct delayed_work *dwork = to_delayed_work(laundry); 6970 struct nfsd_net *nn = container_of(dwork, struct nfsd_net, 6971 laundromat_work); 6972 6973 t = nfs4_laundromat(nn); 6974 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); 6975 } 6976 6977 static void 6978 courtesy_client_reaper(struct nfsd_net *nn) 6979 { 6980 struct list_head reaplist; 6981 6982 nfs4_get_courtesy_client_reaplist(nn, &reaplist); 6983 nfs4_process_client_reaplist(&reaplist); 6984 } 6985 6986 static void 6987 deleg_reaper(struct nfsd_net *nn) 6988 { 6989 struct list_head *pos, *next; 6990 struct nfs4_client *clp; 6991 6992 spin_lock(&nn->client_lock); 6993 list_for_each_safe(pos, next, &nn->client_lru) { 6994 clp = list_entry(pos, struct nfs4_client, cl_lru); 6995 6996 if (clp->cl_state != NFSD4_ACTIVE) 6997 continue; 6998 if (list_empty(&clp->cl_delegations)) 6999 continue; 7000 if (atomic_read(&clp->cl_delegs_in_recall)) 7001 continue; 7002 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &clp->cl_ra->ra_cb.cb_flags)) 7003 continue; 7004 if (ktime_get_boottime_seconds() - clp->cl_ra_time < 5) 7005 continue; 7006 if (clp->cl_cb_state != NFSD4_CB_UP) 7007 continue; 7008 7009 /* release in nfsd4_cb_recall_any_release */ 7010 kref_get(&clp->cl_nfsdfs.cl_ref); 7011 clp->cl_ra_time = ktime_get_boottime_seconds(); 7012 clp->cl_ra->ra_keep = 0; 7013 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG) | 7014 BIT(RCA4_TYPE_MASK_WDATA_DLG); 7015 trace_nfsd_cb_recall_any(clp->cl_ra); 7016 nfsd4_run_cb(&clp->cl_ra->ra_cb); 7017 } 7018 spin_unlock(&nn->client_lock); 7019 } 7020 7021 static void 7022 nfsd4_state_shrinker_worker(struct work_struct *work) 7023 { 7024 struct nfsd_net *nn = container_of(work, struct nfsd_net, 7025 nfsd_shrinker_work); 7026 7027 courtesy_client_reaper(nn); 7028 deleg_reaper(nn); 7029 } 7030 7031 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) 7032 { 7033 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) 7034 return nfserr_bad_stateid; 7035 return nfs_ok; 7036 } 7037 7038 static 7039 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) 7040 { 7041 __be32 status = nfserr_openmode; 7042 7043 /* For lock stateid's, we test the parent open, not the lock: */ 7044 if (stp->st_openstp) 7045 stp = stp->st_openstp; 7046 if ((flags & WR_STATE) && !access_permit_write(stp)) 7047 goto out; 7048 if ((flags & RD_STATE) && !access_permit_read(stp)) 7049 goto out; 7050 status = nfs_ok; 7051 out: 7052 return status; 7053 } 7054 7055 static inline __be32 7056 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) 7057 { 7058 if (ONE_STATEID(stateid) && (flags & RD_STATE)) 7059 return nfs_ok; 7060 else if (opens_in_grace(net)) { 7061 /* Answer in remaining cases depends on existence of 7062 * conflicting state; so we must wait out the grace period. */ 7063 return nfserr_grace; 7064 } else if (flags & WR_STATE) 7065 return nfs4_share_conflict(current_fh, 7066 NFS4_SHARE_DENY_WRITE); 7067 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ 7068 return nfs4_share_conflict(current_fh, 7069 NFS4_SHARE_DENY_READ); 7070 } 7071 7072 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) 7073 { 7074 /* 7075 * When sessions are used the stateid generation number is ignored 7076 * when it is zero. 7077 */ 7078 if (has_session && in->si_generation == 0) 7079 return nfs_ok; 7080 7081 if (in->si_generation == ref->si_generation) 7082 return nfs_ok; 7083 7084 /* If the client sends us a stateid from the future, it's buggy: */ 7085 if (nfsd4_stateid_generation_after(in, ref)) 7086 return nfserr_bad_stateid; 7087 /* 7088 * However, we could see a stateid from the past, even from a 7089 * non-buggy client. For example, if the client sends a lock 7090 * while some IO is outstanding, the lock may bump si_generation 7091 * while the IO is still in flight. The client could avoid that 7092 * situation by waiting for responses on all the IO requests, 7093 * but better performance may result in retrying IO that 7094 * receives an old_stateid error if requests are rarely 7095 * reordered in flight: 7096 */ 7097 return nfserr_old_stateid; 7098 } 7099 7100 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) 7101 { 7102 __be32 ret; 7103 7104 spin_lock(&s->sc_lock); 7105 ret = nfsd4_verify_open_stid(s); 7106 if (ret == nfs_ok) 7107 ret = check_stateid_generation(in, &s->sc_stateid, has_session); 7108 spin_unlock(&s->sc_lock); 7109 if (ret == nfserr_admin_revoked) 7110 nfsd40_drop_revoked_stid(s->sc_client, 7111 &s->sc_stateid); 7112 return ret; 7113 } 7114 7115 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) 7116 { 7117 if (ols->st_stateowner->so_is_open_owner && 7118 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) 7119 return nfserr_bad_stateid; 7120 return nfs_ok; 7121 } 7122 7123 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) 7124 { 7125 struct nfs4_stid *s; 7126 __be32 status = nfserr_bad_stateid; 7127 7128 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || 7129 CLOSE_STATEID(stateid)) 7130 return status; 7131 spin_lock(&cl->cl_lock); 7132 s = find_stateid_locked(cl, stateid); 7133 if (!s) 7134 goto out_unlock; 7135 status = nfsd4_stid_check_stateid_generation(stateid, s, 1); 7136 if (status) 7137 goto out_unlock; 7138 status = nfsd4_verify_open_stid(s); 7139 if (status) 7140 goto out_unlock; 7141 7142 switch (s->sc_type) { 7143 case SC_TYPE_DELEG: 7144 status = nfs_ok; 7145 break; 7146 case SC_TYPE_OPEN: 7147 case SC_TYPE_LOCK: 7148 status = nfsd4_check_openowner_confirmed(openlockstateid(s)); 7149 break; 7150 default: 7151 printk("unknown stateid type %x\n", s->sc_type); 7152 status = nfserr_bad_stateid; 7153 } 7154 out_unlock: 7155 spin_unlock(&cl->cl_lock); 7156 if (status == nfserr_admin_revoked) 7157 nfsd40_drop_revoked_stid(cl, stateid); 7158 return status; 7159 } 7160 7161 __be32 7162 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, 7163 stateid_t *stateid, 7164 unsigned short typemask, unsigned short statusmask, 7165 struct nfs4_stid **s, struct nfsd_net *nn) 7166 { 7167 __be32 status; 7168 struct nfs4_stid *stid; 7169 bool return_revoked = false; 7170 7171 /* 7172 * only return revoked delegations if explicitly asked. 7173 * otherwise we report revoked or bad_stateid status. 7174 */ 7175 if (statusmask & SC_STATUS_REVOKED) 7176 return_revoked = true; 7177 if (typemask & SC_TYPE_DELEG) 7178 /* Always allow REVOKED for DELEG so we can 7179 * return the appropriate error. 7180 */ 7181 statusmask |= SC_STATUS_REVOKED; 7182 7183 statusmask |= SC_STATUS_ADMIN_REVOKED | SC_STATUS_FREEABLE; 7184 7185 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || 7186 CLOSE_STATEID(stateid)) 7187 return nfserr_bad_stateid; 7188 status = set_client(&stateid->si_opaque.so_clid, cstate, nn); 7189 if (status == nfserr_stale_clientid) { 7190 if (cstate->session) 7191 return nfserr_bad_stateid; 7192 return nfserr_stale_stateid; 7193 } 7194 if (status) 7195 return status; 7196 stid = find_stateid_by_type(cstate->clp, stateid, typemask, statusmask); 7197 if (!stid) 7198 return nfserr_bad_stateid; 7199 if ((stid->sc_status & SC_STATUS_REVOKED) && !return_revoked) { 7200 nfs4_put_stid(stid); 7201 return nfserr_deleg_revoked; 7202 } 7203 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) { 7204 nfsd40_drop_revoked_stid(cstate->clp, stateid); 7205 nfs4_put_stid(stid); 7206 return nfserr_admin_revoked; 7207 } 7208 *s = stid; 7209 return nfs_ok; 7210 } 7211 7212 static struct nfsd_file * 7213 nfs4_find_file(struct nfs4_stid *s, int flags) 7214 { 7215 struct nfsd_file *ret = NULL; 7216 7217 if (!s || s->sc_status) 7218 return NULL; 7219 7220 switch (s->sc_type) { 7221 case SC_TYPE_DELEG: 7222 case SC_TYPE_OPEN: 7223 case SC_TYPE_LOCK: 7224 if (flags & RD_STATE) 7225 ret = find_readable_file(s->sc_file); 7226 else 7227 ret = find_writeable_file(s->sc_file); 7228 } 7229 7230 return ret; 7231 } 7232 7233 static __be32 7234 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) 7235 { 7236 __be32 status; 7237 7238 status = nfsd4_check_openowner_confirmed(ols); 7239 if (status) 7240 return status; 7241 return nfs4_check_openmode(ols, flags); 7242 } 7243 7244 static __be32 7245 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, 7246 struct nfsd_file **nfp, int flags) 7247 { 7248 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; 7249 struct nfsd_file *nf; 7250 __be32 status; 7251 7252 nf = nfs4_find_file(s, flags); 7253 if (nf) { 7254 status = nfsd_permission(&rqstp->rq_cred, 7255 fhp->fh_export, fhp->fh_dentry, 7256 acc | NFSD_MAY_OWNER_OVERRIDE); 7257 if (status) { 7258 nfsd_file_put(nf); 7259 goto out; 7260 } 7261 } else { 7262 status = nfsd_file_acquire(rqstp, fhp, acc, &nf); 7263 if (status) 7264 return status; 7265 } 7266 *nfp = nf; 7267 out: 7268 return status; 7269 } 7270 static void 7271 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) 7272 { 7273 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID); 7274 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count)) 7275 return; 7276 list_del(&cps->cp_list); 7277 idr_remove(&nn->s2s_cp_stateids, 7278 cps->cp_stateid.cs_stid.si_opaque.so_id); 7279 kfree(cps); 7280 } 7281 /* 7282 * A READ from an inter server to server COPY will have a 7283 * copy stateid. Look up the copy notify stateid from the 7284 * idr structure and take a reference on it. 7285 */ 7286 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, 7287 struct nfs4_client *clp, 7288 struct nfs4_cpntf_state **cps) 7289 { 7290 copy_stateid_t *cps_t; 7291 struct nfs4_cpntf_state *state = NULL; 7292 7293 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) 7294 return nfserr_bad_stateid; 7295 spin_lock(&nn->s2s_cp_lock); 7296 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); 7297 if (cps_t) { 7298 state = container_of(cps_t, struct nfs4_cpntf_state, 7299 cp_stateid); 7300 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) { 7301 state = NULL; 7302 goto unlock; 7303 } 7304 if (!clp) 7305 refcount_inc(&state->cp_stateid.cs_count); 7306 else 7307 _free_cpntf_state_locked(nn, state); 7308 } 7309 unlock: 7310 spin_unlock(&nn->s2s_cp_lock); 7311 if (!state) 7312 return nfserr_bad_stateid; 7313 if (!clp) 7314 *cps = state; 7315 return 0; 7316 } 7317 7318 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, 7319 struct nfs4_stid **stid) 7320 { 7321 __be32 status; 7322 struct nfs4_cpntf_state *cps = NULL; 7323 struct nfs4_client *found; 7324 7325 status = manage_cpntf_state(nn, st, NULL, &cps); 7326 if (status) 7327 return status; 7328 7329 cps->cpntf_time = ktime_get_boottime_seconds(); 7330 7331 status = nfserr_expired; 7332 found = lookup_clientid(&cps->cp_p_clid, true, nn); 7333 if (!found) 7334 goto out; 7335 7336 *stid = find_stateid_by_type(found, &cps->cp_p_stateid, 7337 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK, 7338 0); 7339 if (*stid) 7340 status = nfs_ok; 7341 else 7342 status = nfserr_bad_stateid; 7343 7344 put_client_renew(found); 7345 out: 7346 nfs4_put_cpntf_state(nn, cps); 7347 return status; 7348 } 7349 7350 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) 7351 { 7352 spin_lock(&nn->s2s_cp_lock); 7353 _free_cpntf_state_locked(nn, cps); 7354 spin_unlock(&nn->s2s_cp_lock); 7355 } 7356 7357 /** 7358 * nfs4_preprocess_stateid_op - find and prep stateid for an operation 7359 * @rqstp: incoming request from client 7360 * @cstate: current compound state 7361 * @fhp: filehandle associated with requested stateid 7362 * @stateid: stateid (provided by client) 7363 * @flags: flags describing type of operation to be done 7364 * @nfp: optional nfsd_file return pointer (may be NULL) 7365 * @cstid: optional returned nfs4_stid pointer (may be NULL) 7366 * 7367 * Given info from the client, look up a nfs4_stid for the operation. On 7368 * success, it returns a reference to the nfs4_stid and/or the nfsd_file 7369 * associated with it. 7370 */ 7371 __be32 7372 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, 7373 struct nfsd4_compound_state *cstate, struct svc_fh *fhp, 7374 stateid_t *stateid, int flags, struct nfsd_file **nfp, 7375 struct nfs4_stid **cstid) 7376 { 7377 struct net *net = SVC_NET(rqstp); 7378 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 7379 struct nfs4_stid *s = NULL; 7380 __be32 status; 7381 7382 if (nfp) 7383 *nfp = NULL; 7384 7385 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { 7386 status = check_special_stateids(net, fhp, stateid, flags); 7387 goto done; 7388 } 7389 7390 status = nfsd4_lookup_stateid(cstate, stateid, 7391 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK, 7392 0, &s, nn); 7393 if (status == nfserr_bad_stateid) 7394 status = find_cpntf_state(nn, stateid, &s); 7395 if (status) 7396 return status; 7397 status = nfsd4_stid_check_stateid_generation(stateid, s, 7398 nfsd4_has_session(cstate)); 7399 if (status) 7400 goto out; 7401 7402 switch (s->sc_type) { 7403 case SC_TYPE_DELEG: 7404 status = nfs4_check_delegmode(delegstateid(s), flags); 7405 break; 7406 case SC_TYPE_OPEN: 7407 case SC_TYPE_LOCK: 7408 status = nfs4_check_olstateid(openlockstateid(s), flags); 7409 break; 7410 } 7411 if (status) 7412 goto out; 7413 status = nfs4_check_fh(fhp, s); 7414 7415 done: 7416 if (status == nfs_ok && nfp) 7417 status = nfs4_check_file(rqstp, fhp, s, nfp, flags); 7418 out: 7419 if (s) { 7420 if (!status && cstid) 7421 *cstid = s; 7422 else 7423 nfs4_put_stid(s); 7424 } 7425 return status; 7426 } 7427 7428 /* 7429 * Test if the stateid is valid 7430 */ 7431 __be32 7432 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7433 union nfsd4_op_u *u) 7434 { 7435 struct nfsd4_test_stateid *test_stateid = &u->test_stateid; 7436 struct nfsd4_test_stateid_id *stateid; 7437 struct nfs4_client *cl = cstate->clp; 7438 7439 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) 7440 stateid->ts_id_status = 7441 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); 7442 7443 return nfs_ok; 7444 } 7445 7446 static __be32 7447 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) 7448 { 7449 struct nfs4_ol_stateid *stp = openlockstateid(s); 7450 __be32 ret; 7451 7452 ret = nfsd4_lock_ol_stateid(stp); 7453 if (ret) 7454 goto out_put_stid; 7455 7456 ret = check_stateid_generation(stateid, &s->sc_stateid, 1); 7457 if (ret) 7458 goto out; 7459 7460 ret = nfserr_locks_held; 7461 if (check_for_locks(stp->st_stid.sc_file, 7462 lockowner(stp->st_stateowner))) 7463 goto out; 7464 7465 release_lock_stateid(stp); 7466 ret = nfs_ok; 7467 7468 out: 7469 mutex_unlock(&stp->st_mutex); 7470 out_put_stid: 7471 nfs4_put_stid(s); 7472 return ret; 7473 } 7474 7475 __be32 7476 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7477 union nfsd4_op_u *u) 7478 { 7479 struct nfsd4_free_stateid *free_stateid = &u->free_stateid; 7480 stateid_t *stateid = &free_stateid->fr_stateid; 7481 struct nfs4_stid *s; 7482 struct nfs4_delegation *dp; 7483 struct nfs4_client *cl = cstate->clp; 7484 __be32 ret = nfserr_bad_stateid; 7485 7486 spin_lock(&cl->cl_lock); 7487 s = find_stateid_locked(cl, stateid); 7488 if (!s || s->sc_status & SC_STATUS_CLOSED) 7489 goto out_unlock; 7490 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) { 7491 nfsd4_drop_revoked_stid(s); 7492 ret = nfs_ok; 7493 goto out; 7494 } 7495 spin_lock(&s->sc_lock); 7496 switch (s->sc_type) { 7497 case SC_TYPE_DELEG: 7498 if (s->sc_status & SC_STATUS_REVOKED) { 7499 s->sc_status |= SC_STATUS_CLOSED; 7500 spin_unlock(&s->sc_lock); 7501 dp = delegstateid(s); 7502 if (s->sc_status & SC_STATUS_FREEABLE) 7503 list_del_init(&dp->dl_recall_lru); 7504 s->sc_status |= SC_STATUS_FREED; 7505 spin_unlock(&cl->cl_lock); 7506 nfs4_put_stid(s); 7507 ret = nfs_ok; 7508 goto out; 7509 } 7510 ret = nfserr_locks_held; 7511 break; 7512 case SC_TYPE_OPEN: 7513 ret = check_stateid_generation(stateid, &s->sc_stateid, 1); 7514 if (ret) 7515 break; 7516 ret = nfserr_locks_held; 7517 break; 7518 case SC_TYPE_LOCK: 7519 spin_unlock(&s->sc_lock); 7520 refcount_inc(&s->sc_count); 7521 spin_unlock(&cl->cl_lock); 7522 ret = nfsd4_free_lock_stateid(stateid, s); 7523 goto out; 7524 } 7525 spin_unlock(&s->sc_lock); 7526 out_unlock: 7527 spin_unlock(&cl->cl_lock); 7528 out: 7529 return ret; 7530 } 7531 7532 static inline int 7533 setlkflg (int type) 7534 { 7535 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? 7536 RD_STATE : WR_STATE; 7537 } 7538 7539 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) 7540 { 7541 struct svc_fh *current_fh = &cstate->current_fh; 7542 struct nfs4_stateowner *sop = stp->st_stateowner; 7543 __be32 status; 7544 7545 status = nfsd4_check_seqid(cstate, sop, seqid); 7546 if (status) 7547 return status; 7548 status = nfsd4_lock_ol_stateid(stp); 7549 if (status != nfs_ok) 7550 return status; 7551 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); 7552 if (status == nfs_ok) 7553 status = nfs4_check_fh(current_fh, &stp->st_stid); 7554 if (status != nfs_ok) 7555 mutex_unlock(&stp->st_mutex); 7556 return status; 7557 } 7558 7559 /** 7560 * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op 7561 * @cstate: compund state 7562 * @seqid: seqid (provided by client) 7563 * @stateid: stateid (provided by client) 7564 * @typemask: mask of allowable types for this operation 7565 * @statusmask: mask of allowed states: 0 or STID_CLOSED 7566 * @stpp: return pointer for the stateid found 7567 * @nn: net namespace for request 7568 * 7569 * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and 7570 * return it in @stpp. On a nfs_ok return, the returned stateid will 7571 * have its st_mutex locked. 7572 */ 7573 static __be32 7574 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, 7575 stateid_t *stateid, 7576 unsigned short typemask, unsigned short statusmask, 7577 struct nfs4_ol_stateid **stpp, 7578 struct nfsd_net *nn) 7579 { 7580 __be32 status; 7581 struct nfs4_stid *s; 7582 struct nfs4_ol_stateid *stp = NULL; 7583 7584 trace_nfsd_preprocess(seqid, stateid); 7585 7586 *stpp = NULL; 7587 retry: 7588 status = nfsd4_lookup_stateid(cstate, stateid, 7589 typemask, statusmask, &s, nn); 7590 if (status) 7591 return status; 7592 stp = openlockstateid(s); 7593 if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) { 7594 nfs4_put_stateowner(stp->st_stateowner); 7595 goto retry; 7596 } 7597 7598 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); 7599 if (!status) 7600 *stpp = stp; 7601 else 7602 nfs4_put_stid(&stp->st_stid); 7603 return status; 7604 } 7605 7606 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, 7607 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) 7608 { 7609 __be32 status; 7610 struct nfs4_openowner *oo; 7611 struct nfs4_ol_stateid *stp; 7612 7613 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, 7614 SC_TYPE_OPEN, 0, &stp, nn); 7615 if (status) 7616 return status; 7617 oo = openowner(stp->st_stateowner); 7618 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { 7619 mutex_unlock(&stp->st_mutex); 7620 nfs4_put_stid(&stp->st_stid); 7621 return nfserr_bad_stateid; 7622 } 7623 *stpp = stp; 7624 return nfs_ok; 7625 } 7626 7627 __be32 7628 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7629 union nfsd4_op_u *u) 7630 { 7631 struct nfsd4_open_confirm *oc = &u->open_confirm; 7632 __be32 status; 7633 struct nfs4_openowner *oo; 7634 struct nfs4_ol_stateid *stp; 7635 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 7636 7637 dprintk("NFSD: nfsd4_open_confirm on file %pd\n", 7638 cstate->current_fh.fh_dentry); 7639 7640 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); 7641 if (status) 7642 return status; 7643 7644 status = nfs4_preprocess_seqid_op(cstate, 7645 oc->oc_seqid, &oc->oc_req_stateid, 7646 SC_TYPE_OPEN, 0, &stp, nn); 7647 if (status) 7648 goto out; 7649 oo = openowner(stp->st_stateowner); 7650 status = nfserr_bad_stateid; 7651 if (oo->oo_flags & NFS4_OO_CONFIRMED) { 7652 mutex_unlock(&stp->st_mutex); 7653 goto put_stateid; 7654 } 7655 oo->oo_flags |= NFS4_OO_CONFIRMED; 7656 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); 7657 mutex_unlock(&stp->st_mutex); 7658 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid); 7659 nfsd4_client_record_create(oo->oo_owner.so_client); 7660 status = nfs_ok; 7661 put_stateid: 7662 nfs4_put_stid(&stp->st_stid); 7663 out: 7664 nfsd4_bump_seqid(cstate, status); 7665 return status; 7666 } 7667 7668 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) 7669 { 7670 if (!test_access(access, stp)) 7671 return; 7672 nfs4_file_put_access(stp->st_stid.sc_file, access); 7673 clear_access(access, stp); 7674 } 7675 7676 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) 7677 { 7678 switch (to_access) { 7679 case NFS4_SHARE_ACCESS_READ: 7680 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); 7681 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); 7682 break; 7683 case NFS4_SHARE_ACCESS_WRITE: 7684 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); 7685 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); 7686 break; 7687 case NFS4_SHARE_ACCESS_BOTH: 7688 break; 7689 default: 7690 WARN_ON_ONCE(1); 7691 } 7692 } 7693 7694 __be32 7695 nfsd4_open_downgrade(struct svc_rqst *rqstp, 7696 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 7697 { 7698 struct nfsd4_open_downgrade *od = &u->open_downgrade; 7699 __be32 status; 7700 struct nfs4_ol_stateid *stp; 7701 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 7702 7703 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 7704 cstate->current_fh.fh_dentry); 7705 7706 /* We don't yet support WANT bits: */ 7707 if (od->od_deleg_want) 7708 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, 7709 od->od_deleg_want); 7710 7711 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, 7712 &od->od_stateid, &stp, nn); 7713 if (status) 7714 goto out; 7715 status = nfserr_inval; 7716 if (!test_access(od->od_share_access, stp)) { 7717 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", 7718 stp->st_access_bmap, od->od_share_access); 7719 goto put_stateid; 7720 } 7721 if (!test_deny(od->od_share_deny, stp)) { 7722 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", 7723 stp->st_deny_bmap, od->od_share_deny); 7724 goto put_stateid; 7725 } 7726 nfs4_stateid_downgrade(stp, od->od_share_access); 7727 reset_union_bmap_deny(od->od_share_deny, stp); 7728 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); 7729 status = nfs_ok; 7730 put_stateid: 7731 mutex_unlock(&stp->st_mutex); 7732 nfs4_put_stid(&stp->st_stid); 7733 out: 7734 nfsd4_bump_seqid(cstate, status); 7735 return status; 7736 } 7737 7738 static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) 7739 { 7740 struct nfs4_client *clp = s->st_stid.sc_client; 7741 bool unhashed; 7742 LIST_HEAD(reaplist); 7743 struct nfs4_ol_stateid *stp; 7744 7745 spin_lock(&clp->cl_lock); 7746 unhashed = unhash_open_stateid(s, &reaplist); 7747 7748 if (clp->cl_minorversion) { 7749 if (unhashed) 7750 put_ol_stateid_locked(s, &reaplist); 7751 spin_unlock(&clp->cl_lock); 7752 list_for_each_entry(stp, &reaplist, st_locks) 7753 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid); 7754 free_ol_stateid_reaplist(&reaplist); 7755 return false; 7756 } else { 7757 spin_unlock(&clp->cl_lock); 7758 free_ol_stateid_reaplist(&reaplist); 7759 return unhashed; 7760 } 7761 } 7762 7763 /* 7764 * nfs4_unlock_state() called after encode 7765 */ 7766 __be32 7767 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7768 union nfsd4_op_u *u) 7769 { 7770 struct nfsd4_close *close = &u->close; 7771 __be32 status; 7772 struct nfs4_ol_stateid *stp; 7773 struct net *net = SVC_NET(rqstp); 7774 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 7775 bool need_move_to_close_list; 7776 7777 dprintk("NFSD: nfsd4_close on file %pd\n", 7778 cstate->current_fh.fh_dentry); 7779 7780 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, 7781 &close->cl_stateid, 7782 SC_TYPE_OPEN, SC_STATUS_CLOSED, 7783 &stp, nn); 7784 nfsd4_bump_seqid(cstate, status); 7785 if (status) 7786 goto out; 7787 7788 spin_lock(&stp->st_stid.sc_client->cl_lock); 7789 stp->st_stid.sc_status |= SC_STATUS_CLOSED; 7790 spin_unlock(&stp->st_stid.sc_client->cl_lock); 7791 7792 /* 7793 * Technically we don't _really_ have to increment or copy it, since 7794 * it should just be gone after this operation and we clobber the 7795 * copied value below, but we continue to do so here just to ensure 7796 * that racing ops see that there was a state change. 7797 */ 7798 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); 7799 7800 need_move_to_close_list = nfsd4_close_open_stateid(stp); 7801 mutex_unlock(&stp->st_mutex); 7802 if (need_move_to_close_list) 7803 move_to_close_lru(stp, net); 7804 7805 /* v4.1+ suggests that we send a special stateid in here, since the 7806 * clients should just ignore this anyway. Since this is not useful 7807 * for v4.0 clients either, we set it to the special close_stateid 7808 * universally. 7809 * 7810 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 7811 */ 7812 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); 7813 7814 /* put reference from nfs4_preprocess_seqid_op */ 7815 nfs4_put_stid(&stp->st_stid); 7816 out: 7817 return status; 7818 } 7819 7820 __be32 7821 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 7822 union nfsd4_op_u *u) 7823 { 7824 struct nfsd4_delegreturn *dr = &u->delegreturn; 7825 struct nfs4_delegation *dp; 7826 stateid_t *stateid = &dr->dr_stateid; 7827 struct nfs4_stid *s; 7828 __be32 status; 7829 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 7830 7831 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) 7832 return status; 7833 7834 status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, SC_STATUS_REVOKED, &s, nn); 7835 if (status) 7836 goto out; 7837 dp = delegstateid(s); 7838 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate)); 7839 if (status) 7840 goto put_stateid; 7841 7842 trace_nfsd_deleg_return(stateid); 7843 destroy_delegation(dp); 7844 smp_mb__after_atomic(); 7845 wake_up_var(d_inode(cstate->current_fh.fh_dentry)); 7846 put_stateid: 7847 nfs4_put_stid(&dp->dl_stid); 7848 out: 7849 return status; 7850 } 7851 7852 /* last octet in a range */ 7853 static inline u64 7854 last_byte_offset(u64 start, u64 len) 7855 { 7856 u64 end; 7857 7858 WARN_ON_ONCE(!len); 7859 end = start + len; 7860 return end > start ? end - 1: NFS4_MAX_UINT64; 7861 } 7862 7863 /* 7864 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that 7865 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th 7866 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit 7867 * locking, this prevents us from being completely protocol-compliant. The 7868 * real solution to this problem is to start using unsigned file offsets in 7869 * the VFS, but this is a very deep change! 7870 */ 7871 static inline void 7872 nfs4_transform_lock_offset(struct file_lock *lock) 7873 { 7874 if (lock->fl_start < 0) 7875 lock->fl_start = OFFSET_MAX; 7876 if (lock->fl_end < 0) 7877 lock->fl_end = OFFSET_MAX; 7878 } 7879 7880 static fl_owner_t 7881 nfsd4_lm_get_owner(fl_owner_t owner) 7882 { 7883 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; 7884 7885 nfs4_get_stateowner(&lo->lo_owner); 7886 return owner; 7887 } 7888 7889 static void 7890 nfsd4_lm_put_owner(fl_owner_t owner) 7891 { 7892 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; 7893 7894 if (lo) 7895 nfs4_put_stateowner(&lo->lo_owner); 7896 } 7897 7898 /* return pointer to struct nfs4_client if client is expirable */ 7899 static bool 7900 nfsd4_lm_lock_expirable(struct file_lock *cfl) 7901 { 7902 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) cfl->c.flc_owner; 7903 struct nfs4_client *clp = lo->lo_owner.so_client; 7904 struct nfsd_net *nn; 7905 7906 if (try_to_expire_client(clp)) { 7907 nn = net_generic(clp->net, nfsd_net_id); 7908 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0); 7909 return true; 7910 } 7911 return false; 7912 } 7913 7914 /* schedule laundromat to run immediately and wait for it to complete */ 7915 static void 7916 nfsd4_lm_expire_lock(void) 7917 { 7918 flush_workqueue(laundry_wq); 7919 } 7920 7921 static void 7922 nfsd4_lm_notify(struct file_lock *fl) 7923 { 7924 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) fl->c.flc_owner; 7925 struct net *net = lo->lo_owner.so_client->net; 7926 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 7927 struct nfsd4_blocked_lock *nbl = container_of(fl, 7928 struct nfsd4_blocked_lock, nbl_lock); 7929 bool queue = false; 7930 7931 /* An empty list means that something else is going to be using it */ 7932 spin_lock(&nn->blocked_locks_lock); 7933 if (!list_empty(&nbl->nbl_list)) { 7934 list_del_init(&nbl->nbl_list); 7935 list_del_init(&nbl->nbl_lru); 7936 queue = true; 7937 } 7938 spin_unlock(&nn->blocked_locks_lock); 7939 7940 if (queue) { 7941 trace_nfsd_cb_notify_lock(lo, nbl); 7942 nfsd4_try_run_cb(&nbl->nbl_cb); 7943 } 7944 } 7945 7946 static const struct lock_manager_operations nfsd_posix_mng_ops = { 7947 .lm_mod_owner = THIS_MODULE, 7948 .lm_notify = nfsd4_lm_notify, 7949 .lm_get_owner = nfsd4_lm_get_owner, 7950 .lm_put_owner = nfsd4_lm_put_owner, 7951 .lm_lock_expirable = nfsd4_lm_lock_expirable, 7952 .lm_expire_lock = nfsd4_lm_expire_lock, 7953 }; 7954 7955 static inline void 7956 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) 7957 { 7958 struct nfs4_lockowner *lo; 7959 7960 if (fl->fl_lmops == &nfsd_posix_mng_ops) { 7961 lo = (struct nfs4_lockowner *) fl->c.flc_owner; 7962 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, 7963 GFP_KERNEL); 7964 if (!deny->ld_owner.data) 7965 /* We just don't care that much */ 7966 goto nevermind; 7967 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; 7968 } else { 7969 nevermind: 7970 deny->ld_owner.len = 0; 7971 deny->ld_owner.data = NULL; 7972 deny->ld_clientid.cl_boot = 0; 7973 deny->ld_clientid.cl_id = 0; 7974 } 7975 deny->ld_start = fl->fl_start; 7976 deny->ld_length = NFS4_MAX_UINT64; 7977 if (fl->fl_end != NFS4_MAX_UINT64) 7978 deny->ld_length = fl->fl_end - fl->fl_start + 1; 7979 deny->ld_type = NFS4_READ_LT; 7980 if (fl->c.flc_type != F_RDLCK) 7981 deny->ld_type = NFS4_WRITE_LT; 7982 } 7983 7984 static struct nfs4_lockowner * 7985 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) 7986 { 7987 unsigned int strhashval = ownerstr_hashval(owner); 7988 struct nfs4_stateowner *so; 7989 7990 lockdep_assert_held(&clp->cl_lock); 7991 7992 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], 7993 so_strhash) { 7994 if (so->so_is_open_owner) 7995 continue; 7996 if (same_owner_str(so, owner)) 7997 return lockowner(nfs4_get_stateowner(so)); 7998 } 7999 return NULL; 8000 } 8001 8002 static struct nfs4_lockowner * 8003 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) 8004 { 8005 struct nfs4_lockowner *lo; 8006 8007 spin_lock(&clp->cl_lock); 8008 lo = find_lockowner_str_locked(clp, owner); 8009 spin_unlock(&clp->cl_lock); 8010 return lo; 8011 } 8012 8013 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) 8014 { 8015 unhash_lockowner_locked(lockowner(sop)); 8016 } 8017 8018 static void nfs4_free_lockowner(struct nfs4_stateowner *sop) 8019 { 8020 struct nfs4_lockowner *lo = lockowner(sop); 8021 8022 kmem_cache_free(lockowner_slab, lo); 8023 } 8024 8025 static const struct nfs4_stateowner_operations lockowner_ops = { 8026 .so_unhash = nfs4_unhash_lockowner, 8027 .so_free = nfs4_free_lockowner, 8028 }; 8029 8030 /* 8031 * Alloc a lock owner structure. 8032 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 8033 * occurred. 8034 * 8035 * strhashval = ownerstr_hashval 8036 */ 8037 static struct nfs4_lockowner * 8038 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, 8039 struct nfs4_ol_stateid *open_stp, 8040 struct nfsd4_lock *lock) 8041 { 8042 struct nfs4_lockowner *lo, *ret; 8043 8044 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); 8045 if (!lo) 8046 return NULL; 8047 INIT_LIST_HEAD(&lo->lo_blocked); 8048 INIT_LIST_HEAD(&lo->lo_owner.so_stateids); 8049 lo->lo_owner.so_is_open_owner = 0; 8050 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; 8051 lo->lo_owner.so_ops = &lockowner_ops; 8052 spin_lock(&clp->cl_lock); 8053 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); 8054 if (ret == NULL) { 8055 list_add(&lo->lo_owner.so_strhash, 8056 &clp->cl_ownerstr_hashtbl[strhashval]); 8057 ret = lo; 8058 } else 8059 nfs4_free_stateowner(&lo->lo_owner); 8060 8061 spin_unlock(&clp->cl_lock); 8062 return ret; 8063 } 8064 8065 static struct nfs4_ol_stateid * 8066 find_lock_stateid(const struct nfs4_lockowner *lo, 8067 const struct nfs4_ol_stateid *ost) 8068 { 8069 struct nfs4_ol_stateid *lst; 8070 8071 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); 8072 8073 /* If ost is not hashed, ost->st_locks will not be valid */ 8074 if (!nfs4_ol_stateid_unhashed(ost)) 8075 list_for_each_entry(lst, &ost->st_locks, st_locks) { 8076 if (lst->st_stateowner == &lo->lo_owner) { 8077 refcount_inc(&lst->st_stid.sc_count); 8078 return lst; 8079 } 8080 } 8081 return NULL; 8082 } 8083 8084 static struct nfs4_ol_stateid * 8085 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, 8086 struct nfs4_file *fp, struct inode *inode, 8087 struct nfs4_ol_stateid *open_stp) 8088 { 8089 struct nfs4_client *clp = lo->lo_owner.so_client; 8090 struct nfs4_ol_stateid *retstp; 8091 8092 mutex_init(&stp->st_mutex); 8093 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 8094 retry: 8095 spin_lock(&clp->cl_lock); 8096 if (nfs4_ol_stateid_unhashed(open_stp)) 8097 goto out_close; 8098 retstp = find_lock_stateid(lo, open_stp); 8099 if (retstp) 8100 goto out_found; 8101 refcount_inc(&stp->st_stid.sc_count); 8102 stp->st_stid.sc_type = SC_TYPE_LOCK; 8103 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); 8104 get_nfs4_file(fp); 8105 stp->st_stid.sc_file = fp; 8106 stp->st_access_bmap = 0; 8107 stp->st_deny_bmap = open_stp->st_deny_bmap; 8108 stp->st_openstp = open_stp; 8109 spin_lock(&fp->fi_lock); 8110 list_add(&stp->st_locks, &open_stp->st_locks); 8111 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); 8112 list_add(&stp->st_perfile, &fp->fi_stateids); 8113 spin_unlock(&fp->fi_lock); 8114 spin_unlock(&clp->cl_lock); 8115 return stp; 8116 out_found: 8117 spin_unlock(&clp->cl_lock); 8118 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { 8119 nfs4_put_stid(&retstp->st_stid); 8120 goto retry; 8121 } 8122 /* To keep mutex tracking happy */ 8123 mutex_unlock(&stp->st_mutex); 8124 return retstp; 8125 out_close: 8126 spin_unlock(&clp->cl_lock); 8127 mutex_unlock(&stp->st_mutex); 8128 return NULL; 8129 } 8130 8131 static struct nfs4_ol_stateid * 8132 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, 8133 struct inode *inode, struct nfs4_ol_stateid *ost, 8134 bool *new) 8135 { 8136 struct nfs4_stid *ns = NULL; 8137 struct nfs4_ol_stateid *lst; 8138 struct nfs4_openowner *oo = openowner(ost->st_stateowner); 8139 struct nfs4_client *clp = oo->oo_owner.so_client; 8140 8141 *new = false; 8142 spin_lock(&clp->cl_lock); 8143 lst = find_lock_stateid(lo, ost); 8144 spin_unlock(&clp->cl_lock); 8145 if (lst != NULL) { 8146 if (nfsd4_lock_ol_stateid(lst) == nfs_ok) 8147 goto out; 8148 nfs4_put_stid(&lst->st_stid); 8149 } 8150 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); 8151 if (ns == NULL) 8152 return NULL; 8153 8154 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); 8155 if (lst == openlockstateid(ns)) 8156 *new = true; 8157 else 8158 nfs4_put_stid(ns); 8159 out: 8160 return lst; 8161 } 8162 8163 static int 8164 check_lock_length(u64 offset, u64 length) 8165 { 8166 return ((length == 0) || ((length != NFS4_MAX_UINT64) && 8167 (length > ~offset))); 8168 } 8169 8170 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) 8171 { 8172 struct nfs4_file *fp = lock_stp->st_stid.sc_file; 8173 8174 lockdep_assert_held(&fp->fi_lock); 8175 8176 if (test_access(access, lock_stp)) 8177 return; 8178 __nfs4_file_get_access(fp, access); 8179 set_access(access, lock_stp); 8180 } 8181 8182 static __be32 8183 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, 8184 struct nfs4_ol_stateid *ost, 8185 struct nfsd4_lock *lock, 8186 struct nfs4_ol_stateid **plst, bool *new) 8187 { 8188 __be32 status; 8189 struct nfs4_file *fi = ost->st_stid.sc_file; 8190 struct nfs4_openowner *oo = openowner(ost->st_stateowner); 8191 struct nfs4_client *cl = oo->oo_owner.so_client; 8192 struct inode *inode = d_inode(cstate->current_fh.fh_dentry); 8193 struct nfs4_lockowner *lo; 8194 struct nfs4_ol_stateid *lst; 8195 unsigned int strhashval; 8196 8197 lo = find_lockowner_str(cl, &lock->lk_new_owner); 8198 if (!lo) { 8199 strhashval = ownerstr_hashval(&lock->lk_new_owner); 8200 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); 8201 if (lo == NULL) 8202 return nfserr_jukebox; 8203 } else { 8204 /* with an existing lockowner, seqids must be the same */ 8205 status = nfserr_bad_seqid; 8206 if (!cstate->minorversion && 8207 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) 8208 goto out; 8209 } 8210 8211 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); 8212 if (lst == NULL) { 8213 status = nfserr_jukebox; 8214 goto out; 8215 } 8216 8217 status = nfs_ok; 8218 *plst = lst; 8219 out: 8220 nfs4_put_stateowner(&lo->lo_owner); 8221 return status; 8222 } 8223 8224 /* 8225 * LOCK operation 8226 */ 8227 __be32 8228 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8229 union nfsd4_op_u *u) 8230 { 8231 struct nfsd4_lock *lock = &u->lock; 8232 struct nfs4_openowner *open_sop = NULL; 8233 struct nfs4_lockowner *lock_sop = NULL; 8234 struct nfs4_ol_stateid *lock_stp = NULL; 8235 struct nfs4_ol_stateid *open_stp = NULL; 8236 struct nfs4_file *fp; 8237 struct nfsd_file *nf = NULL; 8238 struct nfsd4_blocked_lock *nbl = NULL; 8239 struct file_lock *file_lock = NULL; 8240 struct file_lock *conflock = NULL; 8241 __be32 status = 0; 8242 int lkflg; 8243 int err; 8244 bool new = false; 8245 unsigned char type; 8246 unsigned int flags = FL_POSIX; 8247 struct net *net = SVC_NET(rqstp); 8248 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8249 8250 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", 8251 (long long) lock->lk_offset, 8252 (long long) lock->lk_length); 8253 8254 if (check_lock_length(lock->lk_offset, lock->lk_length)) 8255 return nfserr_inval; 8256 8257 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); 8258 if (status != nfs_ok) 8259 return status; 8260 if (exportfs_cannot_lock(cstate->current_fh.fh_dentry->d_sb->s_export_op)) { 8261 status = nfserr_notsupp; 8262 goto out; 8263 } 8264 8265 if (lock->lk_is_new) { 8266 if (nfsd4_has_session(cstate)) 8267 /* See rfc 5661 18.10.3: given clientid is ignored: */ 8268 memcpy(&lock->lk_new_clientid, 8269 &cstate->clp->cl_clientid, 8270 sizeof(clientid_t)); 8271 8272 /* validate and update open stateid and open seqid */ 8273 status = nfs4_preprocess_confirmed_seqid_op(cstate, 8274 lock->lk_new_open_seqid, 8275 &lock->lk_new_open_stateid, 8276 &open_stp, nn); 8277 if (status) 8278 goto out; 8279 mutex_unlock(&open_stp->st_mutex); 8280 open_sop = openowner(open_stp->st_stateowner); 8281 status = nfserr_bad_stateid; 8282 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, 8283 &lock->lk_new_clientid)) 8284 goto out; 8285 status = lookup_or_create_lock_state(cstate, open_stp, lock, 8286 &lock_stp, &new); 8287 } else { 8288 status = nfs4_preprocess_seqid_op(cstate, 8289 lock->lk_old_lock_seqid, 8290 &lock->lk_old_lock_stateid, 8291 SC_TYPE_LOCK, 0, &lock_stp, 8292 nn); 8293 } 8294 if (status) 8295 goto out; 8296 lock_sop = lockowner(lock_stp->st_stateowner); 8297 8298 lkflg = setlkflg(lock->lk_type); 8299 status = nfs4_check_openmode(lock_stp, lkflg); 8300 if (status) 8301 goto out; 8302 8303 status = nfserr_grace; 8304 if (locks_in_grace(net) && !lock->lk_reclaim) 8305 goto out; 8306 status = nfserr_no_grace; 8307 if (!locks_in_grace(net) && lock->lk_reclaim) 8308 goto out; 8309 8310 if (lock->lk_reclaim) 8311 flags |= FL_RECLAIM; 8312 8313 fp = lock_stp->st_stid.sc_file; 8314 switch (lock->lk_type) { 8315 case NFS4_READW_LT: 8316 fallthrough; 8317 case NFS4_READ_LT: 8318 spin_lock(&fp->fi_lock); 8319 nf = find_readable_file_locked(fp); 8320 if (nf) 8321 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); 8322 spin_unlock(&fp->fi_lock); 8323 type = F_RDLCK; 8324 break; 8325 case NFS4_WRITEW_LT: 8326 fallthrough; 8327 case NFS4_WRITE_LT: 8328 spin_lock(&fp->fi_lock); 8329 nf = find_writeable_file_locked(fp); 8330 if (nf) 8331 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); 8332 spin_unlock(&fp->fi_lock); 8333 type = F_WRLCK; 8334 break; 8335 default: 8336 status = nfserr_inval; 8337 goto out; 8338 } 8339 8340 if (!nf) { 8341 status = nfserr_openmode; 8342 goto out; 8343 } 8344 8345 if (lock->lk_type & (NFS4_READW_LT | NFS4_WRITEW_LT) && 8346 nfsd4_has_session(cstate) && 8347 locks_can_async_lock(nf->nf_file->f_op)) 8348 flags |= FL_SLEEP; 8349 8350 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); 8351 if (!nbl) { 8352 dprintk("NFSD: %s: unable to allocate block!\n", __func__); 8353 status = nfserr_jukebox; 8354 goto out; 8355 } 8356 8357 file_lock = &nbl->nbl_lock; 8358 file_lock->c.flc_type = type; 8359 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); 8360 file_lock->c.flc_pid = current->tgid; 8361 file_lock->c.flc_file = nf->nf_file; 8362 file_lock->c.flc_flags = flags; 8363 file_lock->fl_lmops = &nfsd_posix_mng_ops; 8364 file_lock->fl_start = lock->lk_offset; 8365 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); 8366 nfs4_transform_lock_offset(file_lock); 8367 8368 conflock = locks_alloc_lock(); 8369 if (!conflock) { 8370 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 8371 status = nfserr_jukebox; 8372 goto out; 8373 } 8374 8375 if (flags & FL_SLEEP) { 8376 nbl->nbl_time = ktime_get_boottime_seconds(); 8377 spin_lock(&nn->blocked_locks_lock); 8378 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); 8379 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); 8380 kref_get(&nbl->nbl_kref); 8381 spin_unlock(&nn->blocked_locks_lock); 8382 } 8383 8384 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); 8385 switch (err) { 8386 case 0: /* success! */ 8387 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); 8388 status = 0; 8389 if (lock->lk_reclaim) 8390 nn->somebody_reclaimed = true; 8391 break; 8392 case FILE_LOCK_DEFERRED: 8393 kref_put(&nbl->nbl_kref, free_nbl); 8394 nbl = NULL; 8395 fallthrough; 8396 case -EAGAIN: /* conflock holds conflicting lock */ 8397 status = nfserr_denied; 8398 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); 8399 nfs4_set_lock_denied(conflock, &lock->lk_denied); 8400 break; 8401 case -EDEADLK: 8402 status = nfserr_deadlock; 8403 break; 8404 default: 8405 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); 8406 status = nfserrno(err); 8407 break; 8408 } 8409 out: 8410 if (nbl) { 8411 /* dequeue it if we queued it before */ 8412 if (flags & FL_SLEEP) { 8413 spin_lock(&nn->blocked_locks_lock); 8414 if (!list_empty(&nbl->nbl_list) && 8415 !list_empty(&nbl->nbl_lru)) { 8416 list_del_init(&nbl->nbl_list); 8417 list_del_init(&nbl->nbl_lru); 8418 kref_put(&nbl->nbl_kref, free_nbl); 8419 } 8420 /* nbl can use one of lists to be linked to reaplist */ 8421 spin_unlock(&nn->blocked_locks_lock); 8422 } 8423 free_blocked_lock(nbl); 8424 } 8425 if (nf) 8426 nfsd_file_put(nf); 8427 if (lock_stp) { 8428 /* Bump seqid manually if the 4.0 replay owner is openowner */ 8429 if (cstate->replay_owner && 8430 cstate->replay_owner != &lock_sop->lo_owner && 8431 seqid_mutating_err(ntohl(status))) 8432 lock_sop->lo_owner.so_seqid++; 8433 8434 /* 8435 * If this is a new, never-before-used stateid, and we are 8436 * returning an error, then just go ahead and release it. 8437 */ 8438 if (status && new) 8439 release_lock_stateid(lock_stp); 8440 8441 mutex_unlock(&lock_stp->st_mutex); 8442 8443 nfs4_put_stid(&lock_stp->st_stid); 8444 } 8445 if (open_stp) 8446 nfs4_put_stid(&open_stp->st_stid); 8447 nfsd4_bump_seqid(cstate, status); 8448 if (conflock) 8449 locks_free_lock(conflock); 8450 return status; 8451 } 8452 8453 void nfsd4_lock_release(union nfsd4_op_u *u) 8454 { 8455 struct nfsd4_lock *lock = &u->lock; 8456 struct nfsd4_lock_denied *deny = &lock->lk_denied; 8457 8458 kfree(deny->ld_owner.data); 8459 } 8460 8461 /* 8462 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, 8463 * so we do a temporary open here just to get an open file to pass to 8464 * vfs_test_lock. 8465 */ 8466 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) 8467 { 8468 struct nfsd_file *nf; 8469 struct inode *inode; 8470 __be32 err; 8471 8472 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); 8473 if (err) 8474 return err; 8475 inode = fhp->fh_dentry->d_inode; 8476 inode_lock(inode); /* to block new leases till after test_lock: */ 8477 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ)); 8478 if (err) 8479 goto out; 8480 lock->c.flc_file = nf->nf_file; 8481 err = nfserrno(vfs_test_lock(nf->nf_file, lock)); 8482 lock->c.flc_file = NULL; 8483 out: 8484 inode_unlock(inode); 8485 nfsd_file_put(nf); 8486 return err; 8487 } 8488 8489 /* 8490 * LOCKT operation 8491 */ 8492 __be32 8493 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8494 union nfsd4_op_u *u) 8495 { 8496 struct nfsd4_lockt *lockt = &u->lockt; 8497 struct file_lock *file_lock = NULL; 8498 struct nfs4_lockowner *lo = NULL; 8499 __be32 status; 8500 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8501 8502 if (locks_in_grace(SVC_NET(rqstp))) 8503 return nfserr_grace; 8504 8505 if (check_lock_length(lockt->lt_offset, lockt->lt_length)) 8506 return nfserr_inval; 8507 8508 if (!nfsd4_has_session(cstate)) { 8509 status = set_client(&lockt->lt_clientid, cstate, nn); 8510 if (status) 8511 goto out; 8512 } 8513 8514 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) 8515 goto out; 8516 8517 file_lock = locks_alloc_lock(); 8518 if (!file_lock) { 8519 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 8520 status = nfserr_jukebox; 8521 goto out; 8522 } 8523 8524 switch (lockt->lt_type) { 8525 case NFS4_READ_LT: 8526 case NFS4_READW_LT: 8527 file_lock->c.flc_type = F_RDLCK; 8528 break; 8529 case NFS4_WRITE_LT: 8530 case NFS4_WRITEW_LT: 8531 file_lock->c.flc_type = F_WRLCK; 8532 break; 8533 default: 8534 dprintk("NFSD: nfs4_lockt: bad lock type!\n"); 8535 status = nfserr_inval; 8536 goto out; 8537 } 8538 8539 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); 8540 if (lo) 8541 file_lock->c.flc_owner = (fl_owner_t)lo; 8542 file_lock->c.flc_pid = current->tgid; 8543 file_lock->c.flc_flags = FL_POSIX; 8544 8545 file_lock->fl_start = lockt->lt_offset; 8546 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); 8547 8548 nfs4_transform_lock_offset(file_lock); 8549 8550 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); 8551 if (status) 8552 goto out; 8553 8554 if (file_lock->c.flc_type != F_UNLCK) { 8555 status = nfserr_denied; 8556 nfs4_set_lock_denied(file_lock, &lockt->lt_denied); 8557 } 8558 out: 8559 if (lo) 8560 nfs4_put_stateowner(&lo->lo_owner); 8561 if (file_lock) 8562 locks_free_lock(file_lock); 8563 return status; 8564 } 8565 8566 void nfsd4_lockt_release(union nfsd4_op_u *u) 8567 { 8568 struct nfsd4_lockt *lockt = &u->lockt; 8569 struct nfsd4_lock_denied *deny = &lockt->lt_denied; 8570 8571 kfree(deny->ld_owner.data); 8572 } 8573 8574 __be32 8575 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 8576 union nfsd4_op_u *u) 8577 { 8578 struct nfsd4_locku *locku = &u->locku; 8579 struct nfs4_ol_stateid *stp; 8580 struct nfsd_file *nf = NULL; 8581 struct file_lock *file_lock = NULL; 8582 __be32 status; 8583 int err; 8584 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8585 8586 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", 8587 (long long) locku->lu_offset, 8588 (long long) locku->lu_length); 8589 8590 if (check_lock_length(locku->lu_offset, locku->lu_length)) 8591 return nfserr_inval; 8592 8593 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, 8594 &locku->lu_stateid, SC_TYPE_LOCK, 0, 8595 &stp, nn); 8596 if (status) 8597 goto out; 8598 nf = find_any_file(stp->st_stid.sc_file); 8599 if (!nf) { 8600 status = nfserr_lock_range; 8601 goto put_stateid; 8602 } 8603 if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) { 8604 status = nfserr_notsupp; 8605 goto put_file; 8606 } 8607 8608 file_lock = locks_alloc_lock(); 8609 if (!file_lock) { 8610 dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 8611 status = nfserr_jukebox; 8612 goto put_file; 8613 } 8614 8615 file_lock->c.flc_type = F_UNLCK; 8616 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); 8617 file_lock->c.flc_pid = current->tgid; 8618 file_lock->c.flc_file = nf->nf_file; 8619 file_lock->c.flc_flags = FL_POSIX; 8620 file_lock->fl_lmops = &nfsd_posix_mng_ops; 8621 file_lock->fl_start = locku->lu_offset; 8622 8623 file_lock->fl_end = last_byte_offset(locku->lu_offset, 8624 locku->lu_length); 8625 nfs4_transform_lock_offset(file_lock); 8626 8627 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); 8628 if (err) { 8629 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); 8630 goto out_nfserr; 8631 } 8632 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); 8633 put_file: 8634 nfsd_file_put(nf); 8635 put_stateid: 8636 mutex_unlock(&stp->st_mutex); 8637 nfs4_put_stid(&stp->st_stid); 8638 out: 8639 nfsd4_bump_seqid(cstate, status); 8640 if (file_lock) 8641 locks_free_lock(file_lock); 8642 return status; 8643 8644 out_nfserr: 8645 status = nfserrno(err); 8646 goto put_file; 8647 } 8648 8649 /* 8650 * returns 8651 * true: locks held by lockowner 8652 * false: no locks held by lockowner 8653 */ 8654 static bool 8655 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) 8656 { 8657 struct file_lock *fl; 8658 int status = false; 8659 struct nfsd_file *nf; 8660 struct inode *inode; 8661 struct file_lock_context *flctx; 8662 8663 spin_lock(&fp->fi_lock); 8664 nf = find_any_file_locked(fp); 8665 if (!nf) { 8666 /* Any valid lock stateid should have some sort of access */ 8667 WARN_ON_ONCE(1); 8668 goto out; 8669 } 8670 8671 inode = file_inode(nf->nf_file); 8672 flctx = locks_inode_context(inode); 8673 8674 if (flctx && !list_empty_careful(&flctx->flc_posix)) { 8675 spin_lock(&flctx->flc_lock); 8676 for_each_file_lock(fl, &flctx->flc_posix) { 8677 if (fl->c.flc_owner == (fl_owner_t)lowner) { 8678 status = true; 8679 break; 8680 } 8681 } 8682 spin_unlock(&flctx->flc_lock); 8683 } 8684 out: 8685 spin_unlock(&fp->fi_lock); 8686 return status; 8687 } 8688 8689 /** 8690 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations 8691 * @rqstp: RPC transaction 8692 * @cstate: NFSv4 COMPOUND state 8693 * @u: RELEASE_LOCKOWNER arguments 8694 * 8695 * Check if there are any locks still held and if not, free the lockowner 8696 * and any lock state that is owned. 8697 * 8698 * Return values: 8699 * %nfs_ok: lockowner released or not found 8700 * %nfserr_locks_held: lockowner still in use 8701 * %nfserr_stale_clientid: clientid no longer active 8702 * %nfserr_expired: clientid not recognized 8703 */ 8704 __be32 8705 nfsd4_release_lockowner(struct svc_rqst *rqstp, 8706 struct nfsd4_compound_state *cstate, 8707 union nfsd4_op_u *u) 8708 { 8709 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; 8710 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 8711 clientid_t *clid = &rlockowner->rl_clientid; 8712 struct nfs4_ol_stateid *stp; 8713 struct nfs4_lockowner *lo; 8714 struct nfs4_client *clp; 8715 LIST_HEAD(reaplist); 8716 __be32 status; 8717 8718 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", 8719 clid->cl_boot, clid->cl_id); 8720 8721 status = set_client(clid, cstate, nn); 8722 if (status) 8723 return status; 8724 clp = cstate->clp; 8725 8726 spin_lock(&clp->cl_lock); 8727 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner); 8728 if (!lo) { 8729 spin_unlock(&clp->cl_lock); 8730 return nfs_ok; 8731 } 8732 8733 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { 8734 if (check_for_locks(stp->st_stid.sc_file, lo)) { 8735 spin_unlock(&clp->cl_lock); 8736 nfs4_put_stateowner(&lo->lo_owner); 8737 return nfserr_locks_held; 8738 } 8739 } 8740 unhash_lockowner_locked(lo); 8741 while (!list_empty(&lo->lo_owner.so_stateids)) { 8742 stp = list_first_entry(&lo->lo_owner.so_stateids, 8743 struct nfs4_ol_stateid, 8744 st_perstateowner); 8745 unhash_lock_stateid(stp); 8746 put_ol_stateid_locked(stp, &reaplist); 8747 } 8748 spin_unlock(&clp->cl_lock); 8749 8750 free_ol_stateid_reaplist(&reaplist); 8751 remove_blocked_locks(lo); 8752 nfs4_put_stateowner(&lo->lo_owner); 8753 return nfs_ok; 8754 } 8755 8756 static inline struct nfs4_client_reclaim * 8757 alloc_reclaim(void) 8758 { 8759 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); 8760 } 8761 8762 bool 8763 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) 8764 { 8765 struct nfs4_client_reclaim *crp; 8766 8767 crp = nfsd4_find_reclaim_client(name, nn); 8768 return (crp && crp->cr_clp); 8769 } 8770 8771 /* 8772 * failure => all reset bets are off, nfserr_no_grace... 8773 * 8774 * The caller is responsible for freeing name.data if NULL is returned (it 8775 * will be freed in nfs4_remove_reclaim_record in the normal case). 8776 */ 8777 struct nfs4_client_reclaim * 8778 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, 8779 struct nfsd_net *nn) 8780 { 8781 unsigned int strhashval; 8782 struct nfs4_client_reclaim *crp; 8783 8784 crp = alloc_reclaim(); 8785 if (crp) { 8786 strhashval = clientstr_hashval(name); 8787 INIT_LIST_HEAD(&crp->cr_strhash); 8788 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); 8789 crp->cr_name.data = name.data; 8790 crp->cr_name.len = name.len; 8791 crp->cr_princhash.data = princhash.data; 8792 crp->cr_princhash.len = princhash.len; 8793 crp->cr_clp = NULL; 8794 nn->reclaim_str_hashtbl_size++; 8795 } 8796 return crp; 8797 } 8798 8799 void 8800 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) 8801 { 8802 list_del(&crp->cr_strhash); 8803 kfree(crp->cr_name.data); 8804 kfree(crp->cr_princhash.data); 8805 kfree(crp); 8806 nn->reclaim_str_hashtbl_size--; 8807 } 8808 8809 void 8810 nfs4_release_reclaim(struct nfsd_net *nn) 8811 { 8812 struct nfs4_client_reclaim *crp = NULL; 8813 int i; 8814 8815 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 8816 while (!list_empty(&nn->reclaim_str_hashtbl[i])) { 8817 crp = list_entry(nn->reclaim_str_hashtbl[i].next, 8818 struct nfs4_client_reclaim, cr_strhash); 8819 nfs4_remove_reclaim_record(crp, nn); 8820 } 8821 } 8822 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); 8823 } 8824 8825 /* 8826 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ 8827 struct nfs4_client_reclaim * 8828 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) 8829 { 8830 unsigned int strhashval; 8831 struct nfs4_client_reclaim *crp = NULL; 8832 8833 strhashval = clientstr_hashval(name); 8834 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { 8835 if (compare_blob(&crp->cr_name, &name) == 0) { 8836 return crp; 8837 } 8838 } 8839 return NULL; 8840 } 8841 8842 __be32 8843 nfs4_check_open_reclaim(struct nfs4_client *clp) 8844 { 8845 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) 8846 return nfserr_no_grace; 8847 8848 if (nfsd4_client_record_check(clp)) 8849 return nfserr_reclaim_bad; 8850 8851 return nfs_ok; 8852 } 8853 8854 /* 8855 * Since the lifetime of a delegation isn't limited to that of an open, a 8856 * client may quite reasonably hang on to a delegation as long as it has 8857 * the inode cached. This becomes an obvious problem the first time a 8858 * client's inode cache approaches the size of the server's total memory. 8859 * 8860 * For now we avoid this problem by imposing a hard limit on the number 8861 * of delegations, which varies according to the server's memory size. 8862 */ 8863 static void 8864 set_max_delegations(void) 8865 { 8866 /* 8867 * Allow at most 4 delegations per megabyte of RAM. Quick 8868 * estimates suggest that in the worst case (where every delegation 8869 * is for a different inode), a delegation could take about 1.5K, 8870 * giving a worst case usage of about 6% of memory. 8871 */ 8872 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); 8873 } 8874 8875 static int nfs4_state_create_net(struct net *net) 8876 { 8877 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8878 int i; 8879 8880 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 8881 sizeof(struct list_head), 8882 GFP_KERNEL); 8883 if (!nn->conf_id_hashtbl) 8884 goto err; 8885 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 8886 sizeof(struct list_head), 8887 GFP_KERNEL); 8888 if (!nn->unconf_id_hashtbl) 8889 goto err_unconf_id; 8890 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, 8891 sizeof(struct list_head), 8892 GFP_KERNEL); 8893 if (!nn->sessionid_hashtbl) 8894 goto err_sessionid; 8895 8896 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 8897 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); 8898 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); 8899 } 8900 for (i = 0; i < SESSION_HASH_SIZE; i++) 8901 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); 8902 nn->conf_name_tree = RB_ROOT; 8903 nn->unconf_name_tree = RB_ROOT; 8904 nn->boot_time = ktime_get_real_seconds(); 8905 nn->grace_ended = false; 8906 nn->nfsd4_manager.block_opens = true; 8907 INIT_LIST_HEAD(&nn->nfsd4_manager.list); 8908 INIT_LIST_HEAD(&nn->client_lru); 8909 INIT_LIST_HEAD(&nn->close_lru); 8910 INIT_LIST_HEAD(&nn->del_recall_lru); 8911 spin_lock_init(&nn->client_lock); 8912 spin_lock_init(&nn->s2s_cp_lock); 8913 idr_init(&nn->s2s_cp_stateids); 8914 atomic_set(&nn->pending_async_copies, 0); 8915 8916 spin_lock_init(&nn->blocked_locks_lock); 8917 INIT_LIST_HEAD(&nn->blocked_locks_lru); 8918 8919 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); 8920 INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker); 8921 get_net(net); 8922 8923 nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client"); 8924 if (!nn->nfsd_client_shrinker) 8925 goto err_shrinker; 8926 8927 nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan; 8928 nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count; 8929 nn->nfsd_client_shrinker->private_data = nn; 8930 8931 shrinker_register(nn->nfsd_client_shrinker); 8932 8933 return 0; 8934 8935 err_shrinker: 8936 put_net(net); 8937 kfree(nn->sessionid_hashtbl); 8938 err_sessionid: 8939 kfree(nn->unconf_id_hashtbl); 8940 err_unconf_id: 8941 kfree(nn->conf_id_hashtbl); 8942 err: 8943 return -ENOMEM; 8944 } 8945 8946 static void 8947 nfs4_state_destroy_net(struct net *net) 8948 { 8949 int i; 8950 struct nfs4_client *clp = NULL; 8951 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8952 8953 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 8954 while (!list_empty(&nn->conf_id_hashtbl[i])) { 8955 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); 8956 destroy_client(clp); 8957 } 8958 } 8959 8960 WARN_ON(!list_empty(&nn->blocked_locks_lru)); 8961 8962 for (i = 0; i < CLIENT_HASH_SIZE; i++) { 8963 while (!list_empty(&nn->unconf_id_hashtbl[i])) { 8964 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); 8965 destroy_client(clp); 8966 } 8967 } 8968 8969 kfree(nn->sessionid_hashtbl); 8970 kfree(nn->unconf_id_hashtbl); 8971 kfree(nn->conf_id_hashtbl); 8972 put_net(net); 8973 } 8974 8975 int 8976 nfs4_state_start_net(struct net *net) 8977 { 8978 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8979 int ret; 8980 8981 ret = nfs4_state_create_net(net); 8982 if (ret) 8983 return ret; 8984 locks_start_grace(net, &nn->nfsd4_manager); 8985 nfsd4_client_tracking_init(net); 8986 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) 8987 goto skip_grace; 8988 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", 8989 nn->nfsd4_grace, net->ns.inum); 8990 trace_nfsd_grace_start(nn); 8991 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); 8992 return 0; 8993 8994 skip_grace: 8995 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", 8996 net->ns.inum); 8997 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); 8998 nfsd4_end_grace(nn); 8999 return 0; 9000 } 9001 9002 /* initialization to perform when the nfsd service is started: */ 9003 int 9004 nfs4_state_start(void) 9005 { 9006 int ret; 9007 9008 ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params); 9009 if (ret) 9010 return ret; 9011 9012 nfsd_slot_shrinker = shrinker_alloc(0, "nfsd-DRC-slot"); 9013 if (!nfsd_slot_shrinker) { 9014 rhltable_destroy(&nfs4_file_rhltable); 9015 return -ENOMEM; 9016 } 9017 nfsd_slot_shrinker->count_objects = nfsd_slot_count; 9018 nfsd_slot_shrinker->scan_objects = nfsd_slot_scan; 9019 shrinker_register(nfsd_slot_shrinker); 9020 9021 set_max_delegations(); 9022 return 0; 9023 } 9024 9025 void 9026 nfs4_state_shutdown_net(struct net *net) 9027 { 9028 struct nfs4_delegation *dp = NULL; 9029 struct list_head *pos, *next, reaplist; 9030 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9031 9032 shrinker_free(nn->nfsd_client_shrinker); 9033 cancel_work_sync(&nn->nfsd_shrinker_work); 9034 cancel_delayed_work_sync(&nn->laundromat_work); 9035 locks_end_grace(&nn->nfsd4_manager); 9036 9037 INIT_LIST_HEAD(&reaplist); 9038 spin_lock(&state_lock); 9039 list_for_each_safe(pos, next, &nn->del_recall_lru) { 9040 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 9041 unhash_delegation_locked(dp, SC_STATUS_CLOSED); 9042 list_add(&dp->dl_recall_lru, &reaplist); 9043 } 9044 spin_unlock(&state_lock); 9045 list_for_each_safe(pos, next, &reaplist) { 9046 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 9047 list_del_init(&dp->dl_recall_lru); 9048 destroy_unhashed_deleg(dp); 9049 } 9050 9051 nfsd4_client_tracking_exit(net); 9052 nfs4_state_destroy_net(net); 9053 #ifdef CONFIG_NFSD_V4_2_INTER_SSC 9054 nfsd4_ssc_shutdown_umount(nn); 9055 #endif 9056 } 9057 9058 void 9059 nfs4_state_shutdown(void) 9060 { 9061 rhltable_destroy(&nfs4_file_rhltable); 9062 shrinker_free(nfsd_slot_shrinker); 9063 } 9064 9065 static void 9066 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) 9067 { 9068 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && 9069 CURRENT_STATEID(stateid)) 9070 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); 9071 } 9072 9073 static void 9074 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) 9075 { 9076 if (cstate->minorversion) { 9077 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); 9078 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); 9079 } 9080 } 9081 9082 void 9083 clear_current_stateid(struct nfsd4_compound_state *cstate) 9084 { 9085 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); 9086 } 9087 9088 /* 9089 * functions to set current state id 9090 */ 9091 void 9092 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, 9093 union nfsd4_op_u *u) 9094 { 9095 put_stateid(cstate, &u->open_downgrade.od_stateid); 9096 } 9097 9098 void 9099 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, 9100 union nfsd4_op_u *u) 9101 { 9102 put_stateid(cstate, &u->open.op_stateid); 9103 } 9104 9105 void 9106 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, 9107 union nfsd4_op_u *u) 9108 { 9109 put_stateid(cstate, &u->close.cl_stateid); 9110 } 9111 9112 void 9113 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, 9114 union nfsd4_op_u *u) 9115 { 9116 put_stateid(cstate, &u->lock.lk_resp_stateid); 9117 } 9118 9119 /* 9120 * functions to consume current state id 9121 */ 9122 9123 void 9124 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, 9125 union nfsd4_op_u *u) 9126 { 9127 get_stateid(cstate, &u->open_downgrade.od_stateid); 9128 } 9129 9130 void 9131 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, 9132 union nfsd4_op_u *u) 9133 { 9134 get_stateid(cstate, &u->delegreturn.dr_stateid); 9135 } 9136 9137 void 9138 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, 9139 union nfsd4_op_u *u) 9140 { 9141 get_stateid(cstate, &u->free_stateid.fr_stateid); 9142 } 9143 9144 void 9145 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, 9146 union nfsd4_op_u *u) 9147 { 9148 get_stateid(cstate, &u->setattr.sa_stateid); 9149 } 9150 9151 void 9152 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, 9153 union nfsd4_op_u *u) 9154 { 9155 get_stateid(cstate, &u->close.cl_stateid); 9156 } 9157 9158 void 9159 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, 9160 union nfsd4_op_u *u) 9161 { 9162 get_stateid(cstate, &u->locku.lu_stateid); 9163 } 9164 9165 void 9166 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, 9167 union nfsd4_op_u *u) 9168 { 9169 get_stateid(cstate, &u->read.rd_stateid); 9170 } 9171 9172 void 9173 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, 9174 union nfsd4_op_u *u) 9175 { 9176 get_stateid(cstate, &u->write.wr_stateid); 9177 } 9178 9179 /** 9180 * nfsd4_vet_deleg_time - vet and set the timespec for a delegated timestamp update 9181 * @req: timestamp from the client 9182 * @orig: original timestamp in the inode 9183 * @now: current time 9184 * 9185 * Given a timestamp from the client response, check it against the 9186 * current timestamp in the inode and the current time. Returns true 9187 * if the inode's timestamp needs to be updated, and false otherwise. 9188 * @req may also be changed if the timestamp needs to be clamped. 9189 */ 9190 bool nfsd4_vet_deleg_time(struct timespec64 *req, const struct timespec64 *orig, 9191 const struct timespec64 *now) 9192 { 9193 9194 /* 9195 * "When the time presented is before the original time, then the 9196 * update is ignored." Also no need to update if there is no change. 9197 */ 9198 if (timespec64_compare(req, orig) <= 0) 9199 return false; 9200 9201 /* 9202 * "When the time presented is in the future, the server can either 9203 * clamp the new time to the current time, or it may 9204 * return NFS4ERR_DELAY to the client, allowing it to retry." 9205 */ 9206 if (timespec64_compare(req, now) > 0) 9207 *req = *now; 9208 9209 return true; 9210 } 9211 9212 static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation *dp) 9213 { 9214 struct inode *inode = d_inode(dentry); 9215 struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr; 9216 struct iattr attrs = { }; 9217 int ret; 9218 9219 if (deleg_attrs_deleg(dp->dl_type)) { 9220 struct timespec64 now = current_time(inode); 9221 9222 attrs.ia_atime = ncf->ncf_cb_atime; 9223 attrs.ia_mtime = ncf->ncf_cb_mtime; 9224 9225 if (nfsd4_vet_deleg_time(&attrs.ia_atime, &dp->dl_atime, &now)) 9226 attrs.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET; 9227 9228 if (nfsd4_vet_deleg_time(&attrs.ia_mtime, &dp->dl_mtime, &now)) { 9229 attrs.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET; 9230 attrs.ia_ctime = attrs.ia_mtime; 9231 if (nfsd4_vet_deleg_time(&attrs.ia_ctime, &dp->dl_ctime, &now)) 9232 attrs.ia_valid |= ATTR_CTIME | ATTR_CTIME_SET; 9233 } 9234 } else { 9235 attrs.ia_valid |= ATTR_MTIME | ATTR_CTIME; 9236 } 9237 9238 if (!attrs.ia_valid) 9239 return 0; 9240 9241 attrs.ia_valid |= ATTR_DELEG; 9242 inode_lock(inode); 9243 ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL); 9244 inode_unlock(inode); 9245 return ret; 9246 } 9247 9248 /** 9249 * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict 9250 * @rqstp: RPC transaction context 9251 * @dentry: dentry of inode to be checked for a conflict 9252 * @pdp: returned WRITE delegation, if one was found 9253 * 9254 * This function is called when there is a conflict between a write 9255 * delegation and a change/size GETATTR from another client. The server 9256 * must either use the CB_GETATTR to get the current values of the 9257 * attributes from the client that holds the delegation or recall the 9258 * delegation before replying to the GETATTR. See RFC 8881 section 9259 * 18.7.4. 9260 * 9261 * Returns 0 if there is no conflict; otherwise an nfs_stat 9262 * code is returned. If @pdp is set to a non-NULL value, then the 9263 * caller must put the reference. 9264 */ 9265 __be32 9266 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry, 9267 struct nfs4_delegation **pdp) 9268 { 9269 __be32 status; 9270 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 9271 struct file_lock_context *ctx; 9272 struct nfs4_delegation *dp = NULL; 9273 struct file_lease *fl; 9274 struct nfs4_cb_fattr *ncf; 9275 struct inode *inode = d_inode(dentry); 9276 9277 ctx = locks_inode_context(inode); 9278 if (!ctx) 9279 return nfs_ok; 9280 9281 #define NON_NFSD_LEASE ((void *)1) 9282 9283 spin_lock(&ctx->flc_lock); 9284 for_each_file_lock(fl, &ctx->flc_lease) { 9285 if (fl->c.flc_flags == FL_LAYOUT) 9286 continue; 9287 if (fl->c.flc_type == F_WRLCK) { 9288 if (fl->fl_lmops == &nfsd_lease_mng_ops) 9289 dp = fl->c.flc_owner; 9290 else 9291 dp = NON_NFSD_LEASE; 9292 } 9293 break; 9294 } 9295 if (dp == NULL || dp == NON_NFSD_LEASE || 9296 dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) { 9297 spin_unlock(&ctx->flc_lock); 9298 if (dp == NON_NFSD_LEASE) { 9299 status = nfserrno(nfsd_open_break_lease(inode, 9300 NFSD_MAY_READ)); 9301 if (status != nfserr_jukebox || 9302 !nfsd_wait_for_delegreturn(rqstp, inode)) 9303 return status; 9304 } 9305 return 0; 9306 } 9307 9308 nfsd_stats_wdeleg_getattr_inc(nn); 9309 refcount_inc(&dp->dl_stid.sc_count); 9310 ncf = &dp->dl_cb_fattr; 9311 nfs4_cb_getattr(&dp->dl_cb_fattr); 9312 spin_unlock(&ctx->flc_lock); 9313 9314 wait_on_bit_timeout(&ncf->ncf_getattr.cb_flags, NFSD4_CALLBACK_RUNNING, 9315 TASK_UNINTERRUPTIBLE, NFSD_CB_GETATTR_TIMEOUT); 9316 if (ncf->ncf_cb_status) { 9317 /* Recall delegation only if client didn't respond */ 9318 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ)); 9319 if (status != nfserr_jukebox || 9320 !nfsd_wait_for_delegreturn(rqstp, inode)) 9321 goto out_status; 9322 } 9323 if (!ncf->ncf_file_modified && 9324 (ncf->ncf_initial_cinfo != ncf->ncf_cb_change || 9325 ncf->ncf_cur_fsize != ncf->ncf_cb_fsize)) 9326 ncf->ncf_file_modified = true; 9327 if (ncf->ncf_file_modified) { 9328 int err; 9329 9330 /* 9331 * Per section 10.4.3 of RFC 8881, the server would 9332 * not update the file's metadata with the client's 9333 * modified size 9334 */ 9335 err = cb_getattr_update_times(dentry, dp); 9336 if (err) { 9337 status = nfserrno(err); 9338 goto out_status; 9339 } 9340 ncf->ncf_cur_fsize = ncf->ncf_cb_fsize; 9341 *pdp = dp; 9342 return nfs_ok; 9343 } 9344 status = nfs_ok; 9345 out_status: 9346 nfs4_put_stid(&dp->dl_stid); 9347 return status; 9348 } 9349