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