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