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