11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (c) 2001 The Regents of the University of Michigan. 31da177e4SLinus Torvalds * All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Kendrick Smith <kmsmith@umich.edu> 61da177e4SLinus Torvalds * Andy Adamson <kandros@umich.edu> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Redistribution and use in source and binary forms, with or without 91da177e4SLinus Torvalds * modification, are permitted provided that the following conditions 101da177e4SLinus Torvalds * are met: 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * 1. Redistributions of source code must retain the above copyright 131da177e4SLinus Torvalds * notice, this list of conditions and the following disclaimer. 141da177e4SLinus Torvalds * 2. Redistributions in binary form must reproduce the above copyright 151da177e4SLinus Torvalds * notice, this list of conditions and the following disclaimer in the 161da177e4SLinus Torvalds * documentation and/or other materials provided with the distribution. 171da177e4SLinus Torvalds * 3. Neither the name of the University nor the names of its 181da177e4SLinus Torvalds * contributors may be used to endorse or promote products derived 191da177e4SLinus Torvalds * from this software without specific prior written permission. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 221da177e4SLinus Torvalds * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 231da177e4SLinus Torvalds * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 241da177e4SLinus Torvalds * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251da177e4SLinus Torvalds * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 261da177e4SLinus Torvalds * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 271da177e4SLinus Torvalds * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 281da177e4SLinus Torvalds * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 291da177e4SLinus Torvalds * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 301da177e4SLinus Torvalds * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 311da177e4SLinus Torvalds * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 35aceaf78dSDave Hansen #include <linux/file.h> 36b89f4321SArnd Bergmann #include <linux/fs.h> 375a0e3ad6STejun Heo #include <linux/slab.h> 380964a3d3SNeilBrown #include <linux/namei.h> 39c2f1a551SMeelap Shah #include <linux/swap.h> 4017456804SBryan Schumaker #include <linux/pagemap.h> 417df302f7SChuck Lever #include <linux/ratelimit.h> 4268e76ad0SOlga Kornievskaia #include <linux/sunrpc/svcauth_gss.h> 435976687aSJeff Layton #include <linux/sunrpc/addr.h> 4487545899SDaniel Borkmann #include <linux/jhash.h> 459a74af21SBoaz Harrosh #include "xdr4.h" 4606b332a5SJ. Bruce Fields #include "xdr4cb.h" 470a3adadeSJ. Bruce Fields #include "vfs.h" 48bfa4b365SJ. Bruce Fields #include "current_stateid.h" 491da177e4SLinus Torvalds 505e1533c7SStanislav Kinsbursky #include "netns.h" 519cf514ccSChristoph Hellwig #include "pnfs.h" 525e1533c7SStanislav Kinsbursky 531da177e4SLinus Torvalds #define NFSDDBG_FACILITY NFSDDBG_PROC 541da177e4SLinus Torvalds 55f32f3c2dSJ. Bruce Fields #define all_ones {{~0,~0},~0} 56f32f3c2dSJ. Bruce Fields static const stateid_t one_stateid = { 57f32f3c2dSJ. Bruce Fields .si_generation = ~0, 58f32f3c2dSJ. Bruce Fields .si_opaque = all_ones, 59f32f3c2dSJ. Bruce Fields }; 60f32f3c2dSJ. Bruce Fields static const stateid_t zero_stateid = { 61f32f3c2dSJ. Bruce Fields /* all fields zero */ 62f32f3c2dSJ. Bruce Fields }; 6319ff0f28STigran Mkrtchyan static const stateid_t currentstateid = { 6419ff0f28STigran Mkrtchyan .si_generation = 1, 6519ff0f28STigran Mkrtchyan }; 66fb500a7cSTrond Myklebust static const stateid_t close_stateid = { 67fb500a7cSTrond Myklebust .si_generation = 0xffffffffU, 68fb500a7cSTrond Myklebust }; 69f32f3c2dSJ. Bruce Fields 70ec6b5d7bSAndy Adamson static u64 current_sessionid = 1; 71fd39ca9aSNeilBrown 72f32f3c2dSJ. Bruce Fields #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) 73f32f3c2dSJ. Bruce Fields #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) 7419ff0f28STigran Mkrtchyan #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) 75ae254dacSAndrew Elble #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* forward declarations */ 78f9c00c3aSJeff Layton static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); 796011695dSTrond Myklebust static void nfs4_free_ol_stateid(struct nfs4_stid *stid); 801da177e4SLinus Torvalds 818b671b80SJ. Bruce Fields /* Locking: */ 828b671b80SJ. Bruce Fields 838b671b80SJ. Bruce Fields /* 848b671b80SJ. Bruce Fields * Currently used for the del_recall_lru and file hash table. In an 858b671b80SJ. Bruce Fields * effort to decrease the scope of the client_mutex, this spinlock may 868b671b80SJ. Bruce Fields * eventually cover more: 878b671b80SJ. Bruce Fields */ 88cdc97505SBenny Halevy static DEFINE_SPINLOCK(state_lock); 898b671b80SJ. Bruce Fields 904f34bd05SAndrew Elble enum nfsd4_st_mutex_lock_subclass { 914f34bd05SAndrew Elble OPEN_STATEID_MUTEX = 0, 924f34bd05SAndrew Elble LOCK_STATEID_MUTEX = 1, 934f34bd05SAndrew Elble }; 944f34bd05SAndrew Elble 95b401be22SJeff Layton /* 96b401be22SJeff Layton * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for 97b401be22SJeff Layton * the refcount on the open stateid to drop. 98b401be22SJeff Layton */ 99b401be22SJeff Layton static DECLARE_WAIT_QUEUE_HEAD(close_wq); 100b401be22SJeff Layton 1019258a2d5SJeff Layton static struct kmem_cache *client_slab; 102abf1135bSChristoph Hellwig static struct kmem_cache *openowner_slab; 103abf1135bSChristoph Hellwig static struct kmem_cache *lockowner_slab; 104abf1135bSChristoph Hellwig static struct kmem_cache *file_slab; 105abf1135bSChristoph Hellwig static struct kmem_cache *stateid_slab; 106abf1135bSChristoph Hellwig static struct kmem_cache *deleg_slab; 1078287f009SSachin Bhamare static struct kmem_cache *odstate_slab; 108e60d4398SNeilBrown 10966b2b9b2SJ. Bruce Fields static void free_session(struct nfsd4_session *); 110508dc6e1SBenny Halevy 111c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; 11276d348faSJeff Layton static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; 1130162ac2bSChristoph Hellwig 11466b2b9b2SJ. Bruce Fields static bool is_session_dead(struct nfsd4_session *ses) 115508dc6e1SBenny Halevy { 11666b2b9b2SJ. Bruce Fields return ses->se_flags & NFS4_SESSION_DEAD; 11766b2b9b2SJ. Bruce Fields } 11866b2b9b2SJ. Bruce Fields 119f0f51f5cSJ. Bruce Fields static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) 120f0f51f5cSJ. Bruce Fields { 121f0f51f5cSJ. Bruce Fields if (atomic_read(&ses->se_ref) > ref_held_by_me) 12266b2b9b2SJ. Bruce Fields return nfserr_jukebox; 12366b2b9b2SJ. Bruce Fields ses->se_flags |= NFS4_SESSION_DEAD; 12466b2b9b2SJ. Bruce Fields return nfs_ok; 12566b2b9b2SJ. Bruce Fields } 12666b2b9b2SJ. Bruce Fields 127221a6876SJ. Bruce Fields static bool is_client_expired(struct nfs4_client *clp) 128221a6876SJ. Bruce Fields { 129221a6876SJ. Bruce Fields return clp->cl_time == 0; 130221a6876SJ. Bruce Fields } 131221a6876SJ. Bruce Fields 132221a6876SJ. Bruce Fields static __be32 get_client_locked(struct nfs4_client *clp) 133221a6876SJ. Bruce Fields { 1340a880a28STrond Myklebust struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1350a880a28STrond Myklebust 1360a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 1370a880a28STrond Myklebust 138221a6876SJ. Bruce Fields if (is_client_expired(clp)) 139221a6876SJ. Bruce Fields return nfserr_expired; 140221a6876SJ. Bruce Fields atomic_inc(&clp->cl_refcount); 141221a6876SJ. Bruce Fields return nfs_ok; 142221a6876SJ. Bruce Fields } 143221a6876SJ. Bruce Fields 144221a6876SJ. Bruce Fields /* must be called under the client_lock */ 145221a6876SJ. Bruce Fields static inline void 146221a6876SJ. Bruce Fields renew_client_locked(struct nfs4_client *clp) 147221a6876SJ. Bruce Fields { 148221a6876SJ. Bruce Fields struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 149221a6876SJ. Bruce Fields 150221a6876SJ. Bruce Fields if (is_client_expired(clp)) { 151221a6876SJ. Bruce Fields WARN_ON(1); 152221a6876SJ. Bruce Fields printk("%s: client (clientid %08x/%08x) already expired\n", 153221a6876SJ. Bruce Fields __func__, 154221a6876SJ. Bruce Fields clp->cl_clientid.cl_boot, 155221a6876SJ. Bruce Fields clp->cl_clientid.cl_id); 156221a6876SJ. Bruce Fields return; 157221a6876SJ. Bruce Fields } 158221a6876SJ. Bruce Fields 159221a6876SJ. Bruce Fields dprintk("renewing client (clientid %08x/%08x)\n", 160221a6876SJ. Bruce Fields clp->cl_clientid.cl_boot, 161221a6876SJ. Bruce Fields clp->cl_clientid.cl_id); 162221a6876SJ. Bruce Fields list_move_tail(&clp->cl_lru, &nn->client_lru); 163221a6876SJ. Bruce Fields clp->cl_time = get_seconds(); 164221a6876SJ. Bruce Fields } 165221a6876SJ. Bruce Fields 166ba138435SFengguang Wu static void put_client_renew_locked(struct nfs4_client *clp) 167221a6876SJ. Bruce Fields { 1680a880a28STrond Myklebust struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1690a880a28STrond Myklebust 1700a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 1710a880a28STrond Myklebust 172221a6876SJ. Bruce Fields if (!atomic_dec_and_test(&clp->cl_refcount)) 173221a6876SJ. Bruce Fields return; 174221a6876SJ. Bruce Fields if (!is_client_expired(clp)) 175221a6876SJ. Bruce Fields renew_client_locked(clp); 176221a6876SJ. Bruce Fields } 177221a6876SJ. Bruce Fields 1784b24ca7dSJeff Layton static void put_client_renew(struct nfs4_client *clp) 1794b24ca7dSJeff Layton { 1804b24ca7dSJeff Layton struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1814b24ca7dSJeff Layton 182d6c249b4SJeff Layton if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock)) 183d6c249b4SJeff Layton return; 184d6c249b4SJeff Layton if (!is_client_expired(clp)) 185d6c249b4SJeff Layton renew_client_locked(clp); 1864b24ca7dSJeff Layton spin_unlock(&nn->client_lock); 1874b24ca7dSJeff Layton } 1884b24ca7dSJeff Layton 189d4e19e70STrond Myklebust static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) 190d4e19e70STrond Myklebust { 191d4e19e70STrond Myklebust __be32 status; 192d4e19e70STrond Myklebust 193d4e19e70STrond Myklebust if (is_session_dead(ses)) 194d4e19e70STrond Myklebust return nfserr_badsession; 195d4e19e70STrond Myklebust status = get_client_locked(ses->se_client); 196d4e19e70STrond Myklebust if (status) 197d4e19e70STrond Myklebust return status; 198d4e19e70STrond Myklebust atomic_inc(&ses->se_ref); 199d4e19e70STrond Myklebust return nfs_ok; 200d4e19e70STrond Myklebust } 201d4e19e70STrond Myklebust 202d4e19e70STrond Myklebust static void nfsd4_put_session_locked(struct nfsd4_session *ses) 203d4e19e70STrond Myklebust { 204d4e19e70STrond Myklebust struct nfs4_client *clp = ses->se_client; 2050a880a28STrond Myklebust struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2060a880a28STrond Myklebust 2070a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 208d4e19e70STrond Myklebust 209d4e19e70STrond Myklebust if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) 210d4e19e70STrond Myklebust free_session(ses); 211d4e19e70STrond Myklebust put_client_renew_locked(clp); 212d4e19e70STrond Myklebust } 213d4e19e70STrond Myklebust 214d4e19e70STrond Myklebust static void nfsd4_put_session(struct nfsd4_session *ses) 215d4e19e70STrond Myklebust { 216d4e19e70STrond Myklebust struct nfs4_client *clp = ses->se_client; 217d4e19e70STrond Myklebust struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 218d4e19e70STrond Myklebust 219d4e19e70STrond Myklebust spin_lock(&nn->client_lock); 220d4e19e70STrond Myklebust nfsd4_put_session_locked(ses); 221d4e19e70STrond Myklebust spin_unlock(&nn->client_lock); 222d4e19e70STrond Myklebust } 223d4e19e70STrond Myklebust 22476d348faSJeff Layton static struct nfsd4_blocked_lock * 22576d348faSJeff Layton find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, 22676d348faSJeff Layton struct nfsd_net *nn) 22776d348faSJeff Layton { 22876d348faSJeff Layton struct nfsd4_blocked_lock *cur, *found = NULL; 22976d348faSJeff Layton 2300cc11a61SJeff Layton spin_lock(&nn->blocked_locks_lock); 23176d348faSJeff Layton list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { 23276d348faSJeff Layton if (fh_match(fh, &cur->nbl_fh)) { 23376d348faSJeff Layton list_del_init(&cur->nbl_list); 2347919d0a2SJeff Layton list_del_init(&cur->nbl_lru); 23576d348faSJeff Layton found = cur; 23676d348faSJeff Layton break; 23776d348faSJeff Layton } 23876d348faSJeff Layton } 2390cc11a61SJeff Layton spin_unlock(&nn->blocked_locks_lock); 24076d348faSJeff Layton if (found) 24176d348faSJeff Layton posix_unblock_lock(&found->nbl_lock); 24276d348faSJeff Layton return found; 24376d348faSJeff Layton } 24476d348faSJeff Layton 24576d348faSJeff Layton static struct nfsd4_blocked_lock * 24676d348faSJeff Layton find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, 24776d348faSJeff Layton struct nfsd_net *nn) 24876d348faSJeff Layton { 24976d348faSJeff Layton struct nfsd4_blocked_lock *nbl; 25076d348faSJeff Layton 25176d348faSJeff Layton nbl = find_blocked_lock(lo, fh, nn); 25276d348faSJeff Layton if (!nbl) { 25376d348faSJeff Layton nbl= kmalloc(sizeof(*nbl), GFP_KERNEL); 25476d348faSJeff Layton if (nbl) { 25576d348faSJeff Layton fh_copy_shallow(&nbl->nbl_fh, fh); 25676d348faSJeff Layton locks_init_lock(&nbl->nbl_lock); 25776d348faSJeff Layton nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, 25876d348faSJeff Layton &nfsd4_cb_notify_lock_ops, 25976d348faSJeff Layton NFSPROC4_CLNT_CB_NOTIFY_LOCK); 26076d348faSJeff Layton } 26176d348faSJeff Layton } 26276d348faSJeff Layton return nbl; 26376d348faSJeff Layton } 26476d348faSJeff Layton 26576d348faSJeff Layton static void 26676d348faSJeff Layton free_blocked_lock(struct nfsd4_blocked_lock *nbl) 26776d348faSJeff Layton { 26876d348faSJeff Layton locks_release_private(&nbl->nbl_lock); 26976d348faSJeff Layton kfree(nbl); 27076d348faSJeff Layton } 27176d348faSJeff Layton 27268ef3bc3SJeff Layton static void 27368ef3bc3SJeff Layton remove_blocked_locks(struct nfs4_lockowner *lo) 27468ef3bc3SJeff Layton { 27568ef3bc3SJeff Layton struct nfs4_client *clp = lo->lo_owner.so_client; 27668ef3bc3SJeff Layton struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 27768ef3bc3SJeff Layton struct nfsd4_blocked_lock *nbl; 27868ef3bc3SJeff Layton LIST_HEAD(reaplist); 27968ef3bc3SJeff Layton 28068ef3bc3SJeff Layton /* Dequeue all blocked locks */ 28168ef3bc3SJeff Layton spin_lock(&nn->blocked_locks_lock); 28268ef3bc3SJeff Layton while (!list_empty(&lo->lo_blocked)) { 28368ef3bc3SJeff Layton nbl = list_first_entry(&lo->lo_blocked, 28468ef3bc3SJeff Layton struct nfsd4_blocked_lock, 28568ef3bc3SJeff Layton nbl_list); 28668ef3bc3SJeff Layton list_del_init(&nbl->nbl_list); 28768ef3bc3SJeff Layton list_move(&nbl->nbl_lru, &reaplist); 28868ef3bc3SJeff Layton } 28968ef3bc3SJeff Layton spin_unlock(&nn->blocked_locks_lock); 29068ef3bc3SJeff Layton 29168ef3bc3SJeff Layton /* Now free them */ 29268ef3bc3SJeff Layton while (!list_empty(&reaplist)) { 29368ef3bc3SJeff Layton nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, 29468ef3bc3SJeff Layton nbl_lru); 29568ef3bc3SJeff Layton list_del_init(&nbl->nbl_lru); 29668ef3bc3SJeff Layton posix_unblock_lock(&nbl->nbl_lock); 29768ef3bc3SJeff Layton free_blocked_lock(nbl); 29868ef3bc3SJeff Layton } 29968ef3bc3SJeff Layton } 30068ef3bc3SJeff Layton 30176d348faSJeff Layton static int 30276d348faSJeff Layton nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) 30376d348faSJeff Layton { 30476d348faSJeff Layton /* 30576d348faSJeff Layton * Since this is just an optimization, we don't try very hard if it 30676d348faSJeff Layton * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and 30776d348faSJeff Layton * just quit trying on anything else. 30876d348faSJeff Layton */ 30976d348faSJeff Layton switch (task->tk_status) { 31076d348faSJeff Layton case -NFS4ERR_DELAY: 31176d348faSJeff Layton rpc_delay(task, 1 * HZ); 31276d348faSJeff Layton return 0; 31376d348faSJeff Layton default: 31476d348faSJeff Layton return 1; 31576d348faSJeff Layton } 31676d348faSJeff Layton } 31776d348faSJeff Layton 31876d348faSJeff Layton static void 31976d348faSJeff Layton nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) 32076d348faSJeff Layton { 32176d348faSJeff Layton struct nfsd4_blocked_lock *nbl = container_of(cb, 32276d348faSJeff Layton struct nfsd4_blocked_lock, nbl_cb); 32376d348faSJeff Layton 32476d348faSJeff Layton free_blocked_lock(nbl); 32576d348faSJeff Layton } 32676d348faSJeff Layton 32776d348faSJeff Layton static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { 32876d348faSJeff Layton .done = nfsd4_cb_notify_lock_done, 32976d348faSJeff Layton .release = nfsd4_cb_notify_lock_release, 33076d348faSJeff Layton }; 33176d348faSJeff Layton 332b5971afaSKinglong Mee static inline struct nfs4_stateowner * 333b5971afaSKinglong Mee nfs4_get_stateowner(struct nfs4_stateowner *sop) 334b5971afaSKinglong Mee { 335b5971afaSKinglong Mee atomic_inc(&sop->so_count); 336b5971afaSKinglong Mee return sop; 337b5971afaSKinglong Mee } 338b5971afaSKinglong Mee 3397ffb5880STrond Myklebust static int 340d4f0489fSTrond Myklebust same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) 3417ffb5880STrond Myklebust { 3427ffb5880STrond Myklebust return (sop->so_owner.len == owner->len) && 343d4f0489fSTrond Myklebust 0 == memcmp(sop->so_owner.data, owner->data, owner->len); 3447ffb5880STrond Myklebust } 3457ffb5880STrond Myklebust 3467ffb5880STrond Myklebust static struct nfs4_openowner * 3477ffb5880STrond Myklebust find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, 348d4f0489fSTrond Myklebust struct nfs4_client *clp) 3497ffb5880STrond Myklebust { 3507ffb5880STrond Myklebust struct nfs4_stateowner *so; 3517ffb5880STrond Myklebust 352d4f0489fSTrond Myklebust lockdep_assert_held(&clp->cl_lock); 3537ffb5880STrond Myklebust 354d4f0489fSTrond Myklebust list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], 355d4f0489fSTrond Myklebust so_strhash) { 3567ffb5880STrond Myklebust if (!so->so_is_open_owner) 3577ffb5880STrond Myklebust continue; 358b5971afaSKinglong Mee if (same_owner_str(so, &open->op_owner)) 359b5971afaSKinglong Mee return openowner(nfs4_get_stateowner(so)); 3607ffb5880STrond Myklebust } 3617ffb5880STrond Myklebust return NULL; 3627ffb5880STrond Myklebust } 3637ffb5880STrond Myklebust 3647ffb5880STrond Myklebust static struct nfs4_openowner * 3657ffb5880STrond Myklebust find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, 366d4f0489fSTrond Myklebust struct nfs4_client *clp) 3677ffb5880STrond Myklebust { 3687ffb5880STrond Myklebust struct nfs4_openowner *oo; 3697ffb5880STrond Myklebust 370d4f0489fSTrond Myklebust spin_lock(&clp->cl_lock); 371d4f0489fSTrond Myklebust oo = find_openstateowner_str_locked(hashval, open, clp); 372d4f0489fSTrond Myklebust spin_unlock(&clp->cl_lock); 3737ffb5880STrond Myklebust return oo; 3747ffb5880STrond Myklebust } 3757ffb5880STrond Myklebust 3761da177e4SLinus Torvalds static inline u32 3771da177e4SLinus Torvalds opaque_hashval(const void *ptr, int nbytes) 3781da177e4SLinus Torvalds { 3791da177e4SLinus Torvalds unsigned char *cptr = (unsigned char *) ptr; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds u32 x = 0; 3821da177e4SLinus Torvalds while (nbytes--) { 3831da177e4SLinus Torvalds x *= 37; 3841da177e4SLinus Torvalds x += *cptr++; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds return x; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3895b095e99SJeff Layton static void nfsd4_free_file_rcu(struct rcu_head *rcu) 39032513b40SJ. Bruce Fields { 3915b095e99SJeff Layton struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); 3925b095e99SJeff Layton 3935b095e99SJeff Layton kmem_cache_free(file_slab, fp); 39432513b40SJ. Bruce Fields } 39532513b40SJ. Bruce Fields 396e6ba76e1SChristoph Hellwig void 39713cd2184SNeilBrown put_nfs4_file(struct nfs4_file *fi) 39813cd2184SNeilBrown { 39902e1215fSJeff Layton might_lock(&state_lock); 40002e1215fSJeff Layton 401818a34ebSElena Reshetova if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) { 4025b095e99SJeff Layton hlist_del_rcu(&fi->fi_hash); 403cdc97505SBenny Halevy spin_unlock(&state_lock); 4048287f009SSachin Bhamare WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); 4055b095e99SJeff Layton WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); 4065b095e99SJeff Layton call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); 4078b671b80SJ. Bruce Fields } 40813cd2184SNeilBrown } 40913cd2184SNeilBrown 410de18643dSTrond Myklebust static struct file * 411de18643dSTrond Myklebust __nfs4_get_fd(struct nfs4_file *f, int oflag) 412de18643dSTrond Myklebust { 413de18643dSTrond Myklebust if (f->fi_fds[oflag]) 414de18643dSTrond Myklebust return get_file(f->fi_fds[oflag]); 415de18643dSTrond Myklebust return NULL; 416de18643dSTrond Myklebust } 417de18643dSTrond Myklebust 418de18643dSTrond Myklebust static struct file * 419de18643dSTrond Myklebust find_writeable_file_locked(struct nfs4_file *f) 420de18643dSTrond Myklebust { 421de18643dSTrond Myklebust struct file *ret; 422de18643dSTrond Myklebust 423de18643dSTrond Myklebust lockdep_assert_held(&f->fi_lock); 424de18643dSTrond Myklebust 425de18643dSTrond Myklebust ret = __nfs4_get_fd(f, O_WRONLY); 426de18643dSTrond Myklebust if (!ret) 427de18643dSTrond Myklebust ret = __nfs4_get_fd(f, O_RDWR); 428de18643dSTrond Myklebust return ret; 429de18643dSTrond Myklebust } 430de18643dSTrond Myklebust 431de18643dSTrond Myklebust static struct file * 432de18643dSTrond Myklebust find_writeable_file(struct nfs4_file *f) 433de18643dSTrond Myklebust { 434de18643dSTrond Myklebust struct file *ret; 435de18643dSTrond Myklebust 436de18643dSTrond Myklebust spin_lock(&f->fi_lock); 437de18643dSTrond Myklebust ret = find_writeable_file_locked(f); 438de18643dSTrond Myklebust spin_unlock(&f->fi_lock); 439de18643dSTrond Myklebust 440de18643dSTrond Myklebust return ret; 441de18643dSTrond Myklebust } 442de18643dSTrond Myklebust 443de18643dSTrond Myklebust static struct file *find_readable_file_locked(struct nfs4_file *f) 444de18643dSTrond Myklebust { 445de18643dSTrond Myklebust struct file *ret; 446de18643dSTrond Myklebust 447de18643dSTrond Myklebust lockdep_assert_held(&f->fi_lock); 448de18643dSTrond Myklebust 449de18643dSTrond Myklebust ret = __nfs4_get_fd(f, O_RDONLY); 450de18643dSTrond Myklebust if (!ret) 451de18643dSTrond Myklebust ret = __nfs4_get_fd(f, O_RDWR); 452de18643dSTrond Myklebust return ret; 453de18643dSTrond Myklebust } 454de18643dSTrond Myklebust 455de18643dSTrond Myklebust static struct file * 456de18643dSTrond Myklebust find_readable_file(struct nfs4_file *f) 457de18643dSTrond Myklebust { 458de18643dSTrond Myklebust struct file *ret; 459de18643dSTrond Myklebust 460de18643dSTrond Myklebust spin_lock(&f->fi_lock); 461de18643dSTrond Myklebust ret = find_readable_file_locked(f); 462de18643dSTrond Myklebust spin_unlock(&f->fi_lock); 463de18643dSTrond Myklebust 464de18643dSTrond Myklebust return ret; 465de18643dSTrond Myklebust } 466de18643dSTrond Myklebust 4674d227fcaSChristoph Hellwig struct file * 468de18643dSTrond Myklebust find_any_file(struct nfs4_file *f) 469de18643dSTrond Myklebust { 470de18643dSTrond Myklebust struct file *ret; 471de18643dSTrond Myklebust 472de18643dSTrond Myklebust spin_lock(&f->fi_lock); 473de18643dSTrond Myklebust ret = __nfs4_get_fd(f, O_RDWR); 474de18643dSTrond Myklebust if (!ret) { 475de18643dSTrond Myklebust ret = __nfs4_get_fd(f, O_WRONLY); 476de18643dSTrond Myklebust if (!ret) 477de18643dSTrond Myklebust ret = __nfs4_get_fd(f, O_RDONLY); 478de18643dSTrond Myklebust } 479de18643dSTrond Myklebust spin_unlock(&f->fi_lock); 480de18643dSTrond Myklebust return ret; 481de18643dSTrond Myklebust } 482de18643dSTrond Myklebust 48302a3508dSTrond Myklebust static atomic_long_t num_delegations; 484697ce9beSZhang Yanfei unsigned long max_delegations; 485ef0f3390SNeilBrown 486ef0f3390SNeilBrown /* 487ef0f3390SNeilBrown * Open owner state (share locks) 488ef0f3390SNeilBrown */ 489ef0f3390SNeilBrown 49016bfdaafSJ. Bruce Fields /* hash tables for lock and open owners */ 49116bfdaafSJ. Bruce Fields #define OWNER_HASH_BITS 8 49216bfdaafSJ. Bruce Fields #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) 49316bfdaafSJ. Bruce Fields #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) 494ef0f3390SNeilBrown 495d4f0489fSTrond Myklebust static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) 496ddc04c41SJ. Bruce Fields { 497ddc04c41SJ. Bruce Fields unsigned int ret; 498ddc04c41SJ. Bruce Fields 499ddc04c41SJ. Bruce Fields ret = opaque_hashval(ownername->data, ownername->len); 50016bfdaafSJ. Bruce Fields return ret & OWNER_HASH_MASK; 501ddc04c41SJ. Bruce Fields } 502ef0f3390SNeilBrown 503ef0f3390SNeilBrown /* hash table for nfs4_file */ 504ef0f3390SNeilBrown #define FILE_HASH_BITS 8 505ef0f3390SNeilBrown #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) 50635079582SShan Wei 507ca943217STrond Myklebust static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) 508ddc04c41SJ. Bruce Fields { 509ca943217STrond Myklebust return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); 510ca943217STrond Myklebust } 511ca943217STrond Myklebust 512ca943217STrond Myklebust static unsigned int file_hashval(struct knfsd_fh *fh) 513ca943217STrond Myklebust { 514ca943217STrond Myklebust return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); 515ca943217STrond Myklebust } 516ca943217STrond Myklebust 51789876f8cSJeff Layton static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; 518ef0f3390SNeilBrown 51912659651SJeff Layton static void 52012659651SJeff Layton __nfs4_file_get_access(struct nfs4_file *fp, u32 access) 5213477565eSJ. Bruce Fields { 5227214e860SJeff Layton lockdep_assert_held(&fp->fi_lock); 5237214e860SJeff Layton 52412659651SJeff Layton if (access & NFS4_SHARE_ACCESS_WRITE) 52512659651SJeff Layton atomic_inc(&fp->fi_access[O_WRONLY]); 52612659651SJeff Layton if (access & NFS4_SHARE_ACCESS_READ) 52712659651SJeff Layton atomic_inc(&fp->fi_access[O_RDONLY]); 5283477565eSJ. Bruce Fields } 5293477565eSJ. Bruce Fields 53012659651SJeff Layton static __be32 53112659651SJeff Layton nfs4_file_get_access(struct nfs4_file *fp, u32 access) 532998db52cSJ. Bruce Fields { 5337214e860SJeff Layton lockdep_assert_held(&fp->fi_lock); 5347214e860SJeff Layton 53512659651SJeff Layton /* Does this access mode make sense? */ 53612659651SJeff Layton if (access & ~NFS4_SHARE_ACCESS_BOTH) 53712659651SJeff Layton return nfserr_inval; 53812659651SJeff Layton 539baeb4ff0SJeff Layton /* Does it conflict with a deny mode already set? */ 540baeb4ff0SJeff Layton if ((access & fp->fi_share_deny) != 0) 541baeb4ff0SJeff Layton return nfserr_share_denied; 542baeb4ff0SJeff Layton 54312659651SJeff Layton __nfs4_file_get_access(fp, access); 54412659651SJeff Layton return nfs_ok; 545998db52cSJ. Bruce Fields } 546998db52cSJ. Bruce Fields 547baeb4ff0SJeff Layton static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) 548baeb4ff0SJeff Layton { 549baeb4ff0SJeff Layton /* Common case is that there is no deny mode. */ 550baeb4ff0SJeff Layton if (deny) { 551baeb4ff0SJeff Layton /* Does this deny mode make sense? */ 552baeb4ff0SJeff Layton if (deny & ~NFS4_SHARE_DENY_BOTH) 553baeb4ff0SJeff Layton return nfserr_inval; 554baeb4ff0SJeff Layton 555baeb4ff0SJeff Layton if ((deny & NFS4_SHARE_DENY_READ) && 556baeb4ff0SJeff Layton atomic_read(&fp->fi_access[O_RDONLY])) 557baeb4ff0SJeff Layton return nfserr_share_denied; 558baeb4ff0SJeff Layton 559baeb4ff0SJeff Layton if ((deny & NFS4_SHARE_DENY_WRITE) && 560baeb4ff0SJeff Layton atomic_read(&fp->fi_access[O_WRONLY])) 561baeb4ff0SJeff Layton return nfserr_share_denied; 562baeb4ff0SJeff Layton } 563baeb4ff0SJeff Layton return nfs_ok; 564baeb4ff0SJeff Layton } 565baeb4ff0SJeff Layton 566998db52cSJ. Bruce Fields static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) 567f9d7562fSJ. Bruce Fields { 568de18643dSTrond Myklebust might_lock(&fp->fi_lock); 569de18643dSTrond Myklebust 570de18643dSTrond Myklebust if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { 571de18643dSTrond Myklebust struct file *f1 = NULL; 572de18643dSTrond Myklebust struct file *f2 = NULL; 573de18643dSTrond Myklebust 5746d338b51SJeff Layton swap(f1, fp->fi_fds[oflag]); 5750c7c3e67SJ. Bruce Fields if (atomic_read(&fp->fi_access[1 - oflag]) == 0) 5766d338b51SJeff Layton swap(f2, fp->fi_fds[O_RDWR]); 577de18643dSTrond Myklebust spin_unlock(&fp->fi_lock); 578de18643dSTrond Myklebust if (f1) 579de18643dSTrond Myklebust fput(f1); 580de18643dSTrond Myklebust if (f2) 581de18643dSTrond Myklebust fput(f2); 582f9d7562fSJ. Bruce Fields } 583f9d7562fSJ. Bruce Fields } 584f9d7562fSJ. Bruce Fields 58512659651SJeff Layton static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) 586998db52cSJ. Bruce Fields { 58712659651SJeff Layton WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); 58812659651SJeff Layton 58912659651SJeff Layton if (access & NFS4_SHARE_ACCESS_WRITE) 590998db52cSJ. Bruce Fields __nfs4_file_put_access(fp, O_WRONLY); 59112659651SJeff Layton if (access & NFS4_SHARE_ACCESS_READ) 59212659651SJeff Layton __nfs4_file_put_access(fp, O_RDONLY); 593998db52cSJ. Bruce Fields } 594998db52cSJ. Bruce Fields 5958287f009SSachin Bhamare /* 5968287f009SSachin Bhamare * Allocate a new open/delegation state counter. This is needed for 5978287f009SSachin Bhamare * pNFS for proper return on close semantics. 5988287f009SSachin Bhamare * 5998287f009SSachin Bhamare * Note that we only allocate it for pNFS-enabled exports, otherwise 6008287f009SSachin Bhamare * all pointers to struct nfs4_clnt_odstate are always NULL. 6018287f009SSachin Bhamare */ 6028287f009SSachin Bhamare static struct nfs4_clnt_odstate * 6038287f009SSachin Bhamare alloc_clnt_odstate(struct nfs4_client *clp) 6048287f009SSachin Bhamare { 6058287f009SSachin Bhamare struct nfs4_clnt_odstate *co; 6068287f009SSachin Bhamare 6078287f009SSachin Bhamare co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); 6088287f009SSachin Bhamare if (co) { 6098287f009SSachin Bhamare co->co_client = clp; 610cff7cb2eSElena Reshetova refcount_set(&co->co_odcount, 1); 6118287f009SSachin Bhamare } 6128287f009SSachin Bhamare return co; 6138287f009SSachin Bhamare } 6148287f009SSachin Bhamare 6158287f009SSachin Bhamare static void 6168287f009SSachin Bhamare hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) 6178287f009SSachin Bhamare { 6188287f009SSachin Bhamare struct nfs4_file *fp = co->co_file; 6198287f009SSachin Bhamare 6208287f009SSachin Bhamare lockdep_assert_held(&fp->fi_lock); 6218287f009SSachin Bhamare list_add(&co->co_perfile, &fp->fi_clnt_odstate); 6228287f009SSachin Bhamare } 6238287f009SSachin Bhamare 6248287f009SSachin Bhamare static inline void 6258287f009SSachin Bhamare get_clnt_odstate(struct nfs4_clnt_odstate *co) 6268287f009SSachin Bhamare { 6278287f009SSachin Bhamare if (co) 628cff7cb2eSElena Reshetova refcount_inc(&co->co_odcount); 6298287f009SSachin Bhamare } 6308287f009SSachin Bhamare 6318287f009SSachin Bhamare static void 6328287f009SSachin Bhamare put_clnt_odstate(struct nfs4_clnt_odstate *co) 6338287f009SSachin Bhamare { 6348287f009SSachin Bhamare struct nfs4_file *fp; 6358287f009SSachin Bhamare 6368287f009SSachin Bhamare if (!co) 6378287f009SSachin Bhamare return; 6388287f009SSachin Bhamare 6398287f009SSachin Bhamare fp = co->co_file; 640cff7cb2eSElena Reshetova if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { 6418287f009SSachin Bhamare list_del(&co->co_perfile); 6428287f009SSachin Bhamare spin_unlock(&fp->fi_lock); 6438287f009SSachin Bhamare 6448287f009SSachin Bhamare nfsd4_return_all_file_layouts(co->co_client, fp); 6458287f009SSachin Bhamare kmem_cache_free(odstate_slab, co); 6468287f009SSachin Bhamare } 6478287f009SSachin Bhamare } 6488287f009SSachin Bhamare 6498287f009SSachin Bhamare static struct nfs4_clnt_odstate * 6508287f009SSachin Bhamare find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) 6518287f009SSachin Bhamare { 6528287f009SSachin Bhamare struct nfs4_clnt_odstate *co; 6538287f009SSachin Bhamare struct nfs4_client *cl; 6548287f009SSachin Bhamare 6558287f009SSachin Bhamare if (!new) 6568287f009SSachin Bhamare return NULL; 6578287f009SSachin Bhamare 6588287f009SSachin Bhamare cl = new->co_client; 6598287f009SSachin Bhamare 6608287f009SSachin Bhamare spin_lock(&fp->fi_lock); 6618287f009SSachin Bhamare list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { 6628287f009SSachin Bhamare if (co->co_client == cl) { 6638287f009SSachin Bhamare get_clnt_odstate(co); 6648287f009SSachin Bhamare goto out; 6658287f009SSachin Bhamare } 6668287f009SSachin Bhamare } 6678287f009SSachin Bhamare co = new; 6688287f009SSachin Bhamare co->co_file = fp; 6698287f009SSachin Bhamare hash_clnt_odstate_locked(new); 6708287f009SSachin Bhamare out: 6718287f009SSachin Bhamare spin_unlock(&fp->fi_lock); 6728287f009SSachin Bhamare return co; 6738287f009SSachin Bhamare } 6748287f009SSachin Bhamare 675d19fb70dSKinglong Mee struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, 676d19fb70dSKinglong Mee void (*sc_free)(struct nfs4_stid *)) 677996e0938SJ. Bruce Fields { 6783abdb607SJ. Bruce Fields struct nfs4_stid *stid; 6793abdb607SJ. Bruce Fields int new_id; 6803abdb607SJ. Bruce Fields 681f8338834STrond Myklebust stid = kmem_cache_zalloc(slab, GFP_KERNEL); 6823abdb607SJ. Bruce Fields if (!stid) 6833abdb607SJ. Bruce Fields return NULL; 684996e0938SJ. Bruce Fields 6854770d722SJeff Layton idr_preload(GFP_KERNEL); 6864770d722SJeff Layton spin_lock(&cl->cl_lock); 6874770d722SJeff Layton new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT); 6884770d722SJeff Layton spin_unlock(&cl->cl_lock); 6894770d722SJeff Layton idr_preload_end(); 690ebd6c707STejun Heo if (new_id < 0) 6913abdb607SJ. Bruce Fields goto out_free; 692d19fb70dSKinglong Mee 693d19fb70dSKinglong Mee stid->sc_free = sc_free; 6943abdb607SJ. Bruce Fields stid->sc_client = cl; 6953abdb607SJ. Bruce Fields stid->sc_stateid.si_opaque.so_id = new_id; 6963abdb607SJ. Bruce Fields stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; 6973abdb607SJ. Bruce Fields /* Will be incremented before return to client: */ 698a15dfcd5SElena Reshetova refcount_set(&stid->sc_count, 1); 6999767feb2SJeff Layton spin_lock_init(&stid->sc_lock); 7003abdb607SJ. Bruce Fields 701996e0938SJ. Bruce Fields /* 7023abdb607SJ. Bruce Fields * It shouldn't be a problem to reuse an opaque stateid value. 7033abdb607SJ. Bruce Fields * I don't think it is for 4.1. But with 4.0 I worry that, for 7043abdb607SJ. Bruce Fields * example, a stray write retransmission could be accepted by 7053abdb607SJ. Bruce Fields * the server when it should have been rejected. Therefore, 7063abdb607SJ. Bruce Fields * adopt a trick from the sctp code to attempt to maximize the 7073abdb607SJ. Bruce Fields * amount of time until an id is reused, by ensuring they always 7083abdb607SJ. Bruce Fields * "increase" (mod INT_MAX): 709996e0938SJ. Bruce Fields */ 7103abdb607SJ. Bruce Fields return stid; 7113abdb607SJ. Bruce Fields out_free: 7122c44a234SWei Yongjun kmem_cache_free(slab, stid); 7133abdb607SJ. Bruce Fields return NULL; 7142a74aba7SJ. Bruce Fields } 7152a74aba7SJ. Bruce Fields 716b49e084dSJeff Layton static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) 7174cdc951bSJ. Bruce Fields { 7186011695dSTrond Myklebust struct nfs4_stid *stid; 7196011695dSTrond Myklebust 720d19fb70dSKinglong Mee stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); 7216011695dSTrond Myklebust if (!stid) 7226011695dSTrond Myklebust return NULL; 7236011695dSTrond Myklebust 724d19fb70dSKinglong Mee return openlockstateid(stid); 7256011695dSTrond Myklebust } 7266011695dSTrond Myklebust 7276011695dSTrond Myklebust static void nfs4_free_deleg(struct nfs4_stid *stid) 7286011695dSTrond Myklebust { 7296011695dSTrond Myklebust kmem_cache_free(deleg_slab, stid); 7306011695dSTrond Myklebust atomic_long_dec(&num_delegations); 7314cdc951bSJ. Bruce Fields } 7324cdc951bSJ. Bruce Fields 7336282cd56SNeilBrown /* 7346282cd56SNeilBrown * When we recall a delegation, we should be careful not to hand it 7356282cd56SNeilBrown * out again straight away. 7366282cd56SNeilBrown * To ensure this we keep a pair of bloom filters ('new' and 'old') 7376282cd56SNeilBrown * in which the filehandles of recalled delegations are "stored". 7386282cd56SNeilBrown * If a filehandle appear in either filter, a delegation is blocked. 7396282cd56SNeilBrown * When a delegation is recalled, the filehandle is stored in the "new" 7406282cd56SNeilBrown * filter. 7416282cd56SNeilBrown * Every 30 seconds we swap the filters and clear the "new" one, 7426282cd56SNeilBrown * unless both are empty of course. 7436282cd56SNeilBrown * 7446282cd56SNeilBrown * Each filter is 256 bits. We hash the filehandle to 32bit and use the 7456282cd56SNeilBrown * low 3 bytes as hash-table indices. 7466282cd56SNeilBrown * 747f54fe962SJeff Layton * 'blocked_delegations_lock', which is always taken in block_delegations(), 7486282cd56SNeilBrown * is used to manage concurrent access. Testing does not need the lock 7496282cd56SNeilBrown * except when swapping the two filters. 7506282cd56SNeilBrown */ 751f54fe962SJeff Layton static DEFINE_SPINLOCK(blocked_delegations_lock); 7526282cd56SNeilBrown static struct bloom_pair { 7536282cd56SNeilBrown int entries, old_entries; 7546282cd56SNeilBrown time_t swap_time; 7556282cd56SNeilBrown int new; /* index into 'set' */ 7566282cd56SNeilBrown DECLARE_BITMAP(set[2], 256); 7576282cd56SNeilBrown } blocked_delegations; 7586282cd56SNeilBrown 7596282cd56SNeilBrown static int delegation_blocked(struct knfsd_fh *fh) 7606282cd56SNeilBrown { 7616282cd56SNeilBrown u32 hash; 7626282cd56SNeilBrown struct bloom_pair *bd = &blocked_delegations; 7636282cd56SNeilBrown 7646282cd56SNeilBrown if (bd->entries == 0) 7656282cd56SNeilBrown return 0; 7666282cd56SNeilBrown if (seconds_since_boot() - bd->swap_time > 30) { 767f54fe962SJeff Layton spin_lock(&blocked_delegations_lock); 7686282cd56SNeilBrown if (seconds_since_boot() - bd->swap_time > 30) { 7696282cd56SNeilBrown bd->entries -= bd->old_entries; 7706282cd56SNeilBrown bd->old_entries = bd->entries; 7716282cd56SNeilBrown memset(bd->set[bd->new], 0, 7726282cd56SNeilBrown sizeof(bd->set[0])); 7736282cd56SNeilBrown bd->new = 1-bd->new; 7746282cd56SNeilBrown bd->swap_time = seconds_since_boot(); 7756282cd56SNeilBrown } 776f54fe962SJeff Layton spin_unlock(&blocked_delegations_lock); 7776282cd56SNeilBrown } 77887545899SDaniel Borkmann hash = jhash(&fh->fh_base, fh->fh_size, 0); 7796282cd56SNeilBrown if (test_bit(hash&255, bd->set[0]) && 7806282cd56SNeilBrown test_bit((hash>>8)&255, bd->set[0]) && 7816282cd56SNeilBrown test_bit((hash>>16)&255, bd->set[0])) 7826282cd56SNeilBrown return 1; 7836282cd56SNeilBrown 7846282cd56SNeilBrown if (test_bit(hash&255, bd->set[1]) && 7856282cd56SNeilBrown test_bit((hash>>8)&255, bd->set[1]) && 7866282cd56SNeilBrown test_bit((hash>>16)&255, bd->set[1])) 7876282cd56SNeilBrown return 1; 7886282cd56SNeilBrown 7896282cd56SNeilBrown return 0; 7906282cd56SNeilBrown } 7916282cd56SNeilBrown 7926282cd56SNeilBrown static void block_delegations(struct knfsd_fh *fh) 7936282cd56SNeilBrown { 7946282cd56SNeilBrown u32 hash; 7956282cd56SNeilBrown struct bloom_pair *bd = &blocked_delegations; 7966282cd56SNeilBrown 79787545899SDaniel Borkmann hash = jhash(&fh->fh_base, fh->fh_size, 0); 7986282cd56SNeilBrown 799f54fe962SJeff Layton spin_lock(&blocked_delegations_lock); 8006282cd56SNeilBrown __set_bit(hash&255, bd->set[bd->new]); 8016282cd56SNeilBrown __set_bit((hash>>8)&255, bd->set[bd->new]); 8026282cd56SNeilBrown __set_bit((hash>>16)&255, bd->set[bd->new]); 8036282cd56SNeilBrown if (bd->entries == 0) 8046282cd56SNeilBrown bd->swap_time = seconds_since_boot(); 8056282cd56SNeilBrown bd->entries += 1; 806f54fe962SJeff Layton spin_unlock(&blocked_delegations_lock); 8076282cd56SNeilBrown } 8086282cd56SNeilBrown 8091da177e4SLinus Torvalds static struct nfs4_delegation * 81086d29b10SJ. Bruce Fields alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, 81186d29b10SJ. Bruce Fields struct svc_fh *current_fh, 8128287f009SSachin Bhamare struct nfs4_clnt_odstate *odstate) 8131da177e4SLinus Torvalds { 8141da177e4SLinus Torvalds struct nfs4_delegation *dp; 81502a3508dSTrond Myklebust long n; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds dprintk("NFSD alloc_init_deleg\n"); 81802a3508dSTrond Myklebust n = atomic_long_inc_return(&num_delegations); 81902a3508dSTrond Myklebust if (n < 0 || n > max_delegations) 82002a3508dSTrond Myklebust goto out_dec; 8216282cd56SNeilBrown if (delegation_blocked(¤t_fh->fh_handle)) 82202a3508dSTrond Myklebust goto out_dec; 823d19fb70dSKinglong Mee dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); 8245b2d21c1SNeilBrown if (dp == NULL) 82502a3508dSTrond Myklebust goto out_dec; 8266011695dSTrond Myklebust 8272a74aba7SJ. Bruce Fields /* 8282a74aba7SJ. Bruce Fields * delegation seqid's are never incremented. The 4.1 special 8296136d2b4SJ. Bruce Fields * meaning of seqid 0 isn't meaningful, really, but let's avoid 8306136d2b4SJ. Bruce Fields * 0 anyway just for consistency and use 1: 8312a74aba7SJ. Bruce Fields */ 8322a74aba7SJ. Bruce Fields dp->dl_stid.sc_stateid.si_generation = 1; 833ea1da636SNeilBrown INIT_LIST_HEAD(&dp->dl_perfile); 834ea1da636SNeilBrown INIT_LIST_HEAD(&dp->dl_perclnt); 8351da177e4SLinus Torvalds INIT_LIST_HEAD(&dp->dl_recall_lru); 8368287f009SSachin Bhamare dp->dl_clnt_odstate = odstate; 8378287f009SSachin Bhamare get_clnt_odstate(odstate); 83899c41515SJ. Bruce Fields dp->dl_type = NFS4_OPEN_DELEGATE_READ; 839f0b5de1bSChristoph Hellwig dp->dl_retries = 1; 840f0b5de1bSChristoph Hellwig nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, 8410162ac2bSChristoph Hellwig &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); 84286d29b10SJ. Bruce Fields get_nfs4_file(fp); 84386d29b10SJ. Bruce Fields dp->dl_stid.sc_file = fp; 8441da177e4SLinus Torvalds return dp; 84502a3508dSTrond Myklebust out_dec: 84602a3508dSTrond Myklebust atomic_long_dec(&num_delegations); 84702a3508dSTrond Myklebust return NULL; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds void 8516011695dSTrond Myklebust nfs4_put_stid(struct nfs4_stid *s) 8521da177e4SLinus Torvalds { 85311b9164aSTrond Myklebust struct nfs4_file *fp = s->sc_file; 8546011695dSTrond Myklebust struct nfs4_client *clp = s->sc_client; 8556011695dSTrond Myklebust 8564770d722SJeff Layton might_lock(&clp->cl_lock); 8574770d722SJeff Layton 858a15dfcd5SElena Reshetova if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { 859b401be22SJeff Layton wake_up_all(&close_wq); 8606011695dSTrond Myklebust return; 861b401be22SJeff Layton } 8626011695dSTrond Myklebust idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); 8634770d722SJeff Layton spin_unlock(&clp->cl_lock); 8646011695dSTrond Myklebust s->sc_free(s); 86511b9164aSTrond Myklebust if (fp) 86611b9164aSTrond Myklebust put_nfs4_file(fp); 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8699767feb2SJeff Layton void 8709767feb2SJeff Layton nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) 8719767feb2SJeff Layton { 8729767feb2SJeff Layton stateid_t *src = &stid->sc_stateid; 8739767feb2SJeff Layton 8749767feb2SJeff Layton spin_lock(&stid->sc_lock); 8759767feb2SJeff Layton if (unlikely(++src->si_generation == 0)) 8769767feb2SJeff Layton src->si_generation = 1; 8779767feb2SJeff Layton memcpy(dst, src, sizeof(*dst)); 8789767feb2SJeff Layton spin_unlock(&stid->sc_lock); 8799767feb2SJeff Layton } 8809767feb2SJeff Layton 881353601e7SJ. Bruce Fields static void put_deleg_file(struct nfs4_file *fp) 8821da177e4SLinus Torvalds { 8836bcc034eSJeff Layton struct file *filp = NULL; 884353601e7SJ. Bruce Fields 885353601e7SJ. Bruce Fields spin_lock(&fp->fi_lock); 886353601e7SJ. Bruce Fields if (--fp->fi_delegees == 0) 887353601e7SJ. Bruce Fields swap(filp, fp->fi_deleg_file); 888353601e7SJ. Bruce Fields spin_unlock(&fp->fi_lock); 889353601e7SJ. Bruce Fields 890353601e7SJ. Bruce Fields if (filp) 891353601e7SJ. Bruce Fields fput(filp); 892353601e7SJ. Bruce Fields } 893353601e7SJ. Bruce Fields 894353601e7SJ. Bruce Fields static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) 895353601e7SJ. Bruce Fields { 896cba7b3d1SJ. Bruce Fields struct nfs4_file *fp = dp->dl_stid.sc_file; 897353601e7SJ. Bruce Fields struct file *filp = fp->fi_deleg_file; 898417c6629SJeff Layton 899b8232d33SJ. Bruce Fields WARN_ON_ONCE(!fp->fi_delegees); 900b8232d33SJ. Bruce Fields 901653e514eSJ. Bruce Fields vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp); 902353601e7SJ. Bruce Fields put_deleg_file(fp); 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 9050af6e690SJ. Bruce Fields static void destroy_unhashed_deleg(struct nfs4_delegation *dp) 9060af6e690SJ. Bruce Fields { 9070af6e690SJ. Bruce Fields put_clnt_odstate(dp->dl_clnt_odstate); 908353601e7SJ. Bruce Fields nfs4_unlock_deleg_lease(dp); 9090af6e690SJ. Bruce Fields nfs4_put_stid(&dp->dl_stid); 9100af6e690SJ. Bruce Fields } 9110af6e690SJ. Bruce Fields 912cd61c522SChristoph Hellwig void nfs4_unhash_stid(struct nfs4_stid *s) 9136136d2b4SJ. Bruce Fields { 9143abdb607SJ. Bruce Fields s->sc_type = 0; 9156136d2b4SJ. Bruce Fields } 9166136d2b4SJ. Bruce Fields 91734ed9872SAndrew Elble /** 91868b18f52SJ. Bruce Fields * nfs4_delegation_exists - Discover if this delegation already exists 91934ed9872SAndrew Elble * @clp: a pointer to the nfs4_client we're granting a delegation to 92034ed9872SAndrew Elble * @fp: a pointer to the nfs4_file we're granting a delegation on 92134ed9872SAndrew Elble * 92234ed9872SAndrew Elble * Return: 92368b18f52SJ. Bruce Fields * On success: true iff an existing delegation is found 92434ed9872SAndrew Elble */ 92534ed9872SAndrew Elble 92668b18f52SJ. Bruce Fields static bool 92768b18f52SJ. Bruce Fields nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) 928931ee56cSBenny Halevy { 92934ed9872SAndrew Elble struct nfs4_delegation *searchdp = NULL; 93034ed9872SAndrew Elble struct nfs4_client *searchclp = NULL; 93134ed9872SAndrew Elble 932cdc97505SBenny Halevy lockdep_assert_held(&state_lock); 933417c6629SJeff Layton lockdep_assert_held(&fp->fi_lock); 934931ee56cSBenny Halevy 93534ed9872SAndrew Elble list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { 93634ed9872SAndrew Elble searchclp = searchdp->dl_stid.sc_client; 93734ed9872SAndrew Elble if (clp == searchclp) { 93851d87bc2SFengguang Wu return true; 93934ed9872SAndrew Elble } 94034ed9872SAndrew Elble } 94151d87bc2SFengguang Wu return false; 94234ed9872SAndrew Elble } 94334ed9872SAndrew Elble 94434ed9872SAndrew Elble /** 94534ed9872SAndrew Elble * hash_delegation_locked - Add a delegation to the appropriate lists 94634ed9872SAndrew Elble * @dp: a pointer to the nfs4_delegation we are adding. 94734ed9872SAndrew Elble * @fp: a pointer to the nfs4_file we're granting a delegation on 94834ed9872SAndrew Elble * 94934ed9872SAndrew Elble * Return: 95034ed9872SAndrew Elble * On success: NULL if the delegation was successfully hashed. 95134ed9872SAndrew Elble * 95234ed9872SAndrew Elble * On error: -EAGAIN if one was previously granted to this 95334ed9872SAndrew Elble * nfs4_client for this nfs4_file. Delegation is not hashed. 95434ed9872SAndrew Elble * 95534ed9872SAndrew Elble */ 95634ed9872SAndrew Elble 95734ed9872SAndrew Elble static int 95834ed9872SAndrew Elble hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) 95934ed9872SAndrew Elble { 96034ed9872SAndrew Elble struct nfs4_client *clp = dp->dl_stid.sc_client; 96134ed9872SAndrew Elble 96234ed9872SAndrew Elble lockdep_assert_held(&state_lock); 96334ed9872SAndrew Elble lockdep_assert_held(&fp->fi_lock); 96434ed9872SAndrew Elble 96568b18f52SJ. Bruce Fields if (nfs4_delegation_exists(clp, fp)) 96668b18f52SJ. Bruce Fields return -EAGAIN; 967a15dfcd5SElena Reshetova refcount_inc(&dp->dl_stid.sc_count); 9683fb87d13SBenny Halevy dp->dl_stid.sc_type = NFS4_DELEG_STID; 969931ee56cSBenny Halevy list_add(&dp->dl_perfile, &fp->fi_delegations); 97034ed9872SAndrew Elble list_add(&dp->dl_perclnt, &clp->cl_delegations); 97134ed9872SAndrew Elble return 0; 972931ee56cSBenny Halevy } 973931ee56cSBenny Halevy 9743fcbbd24SJeff Layton static bool 97542690676SJeff Layton unhash_delegation_locked(struct nfs4_delegation *dp) 9761da177e4SLinus Torvalds { 97711b9164aSTrond Myklebust struct nfs4_file *fp = dp->dl_stid.sc_file; 97802e1215fSJeff Layton 97942690676SJeff Layton lockdep_assert_held(&state_lock); 98042690676SJeff Layton 9813fcbbd24SJeff Layton if (list_empty(&dp->dl_perfile)) 9823fcbbd24SJeff Layton return false; 9833fcbbd24SJeff Layton 984b0fc29d6STrond Myklebust dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; 985d55a166cSJeff Layton /* Ensure that deleg break won't try to requeue it */ 986d55a166cSJeff Layton ++dp->dl_time; 987417c6629SJeff Layton spin_lock(&fp->fi_lock); 988931ee56cSBenny Halevy list_del_init(&dp->dl_perclnt); 9891da177e4SLinus Torvalds list_del_init(&dp->dl_recall_lru); 99002e1215fSJeff Layton list_del_init(&dp->dl_perfile); 99102e1215fSJeff Layton spin_unlock(&fp->fi_lock); 9923fcbbd24SJeff Layton return true; 993cbf7a75bSJ. Bruce Fields } 9943bd64a5bSJ. Bruce Fields 9953bd64a5bSJ. Bruce Fields static void destroy_delegation(struct nfs4_delegation *dp) 9963bd64a5bSJ. Bruce Fields { 9973fcbbd24SJeff Layton bool unhashed; 9983fcbbd24SJeff Layton 99942690676SJeff Layton spin_lock(&state_lock); 10003fcbbd24SJeff Layton unhashed = unhash_delegation_locked(dp); 100142690676SJeff Layton spin_unlock(&state_lock); 10020af6e690SJ. Bruce Fields if (unhashed) 10030af6e690SJ. Bruce Fields destroy_unhashed_deleg(dp); 10043fcbbd24SJeff Layton } 10053bd64a5bSJ. Bruce Fields 10063bd64a5bSJ. Bruce Fields static void revoke_delegation(struct nfs4_delegation *dp) 10073bd64a5bSJ. Bruce Fields { 10083bd64a5bSJ. Bruce Fields struct nfs4_client *clp = dp->dl_stid.sc_client; 10093bd64a5bSJ. Bruce Fields 10102d4a532dSJeff Layton WARN_ON(!list_empty(&dp->dl_recall_lru)); 10112d4a532dSJeff Layton 10120af6e690SJ. Bruce Fields if (clp->cl_minorversion) { 10133bd64a5bSJ. Bruce Fields dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; 10140af6e690SJ. Bruce Fields refcount_inc(&dp->dl_stid.sc_count); 10152d4a532dSJeff Layton spin_lock(&clp->cl_lock); 10162d4a532dSJeff Layton list_add(&dp->dl_recall_lru, &clp->cl_revoked); 10172d4a532dSJeff Layton spin_unlock(&clp->cl_lock); 10183bd64a5bSJ. Bruce Fields } 10190af6e690SJ. Bruce Fields destroy_unhashed_deleg(dp); 10203bd64a5bSJ. Bruce Fields } 10213bd64a5bSJ. Bruce Fields 10221da177e4SLinus Torvalds /* 10231da177e4SLinus Torvalds * SETCLIENTID state 10241da177e4SLinus Torvalds */ 10251da177e4SLinus Torvalds 1026ddc04c41SJ. Bruce Fields static unsigned int clientid_hashval(u32 id) 1027ddc04c41SJ. Bruce Fields { 1028ddc04c41SJ. Bruce Fields return id & CLIENT_HASH_MASK; 1029ddc04c41SJ. Bruce Fields } 1030ddc04c41SJ. Bruce Fields 1031ddc04c41SJ. Bruce Fields static unsigned int clientstr_hashval(const char *name) 1032ddc04c41SJ. Bruce Fields { 1033ddc04c41SJ. Bruce Fields return opaque_hashval(name, 8) & CLIENT_HASH_MASK; 1034ddc04c41SJ. Bruce Fields } 1035ddc04c41SJ. Bruce Fields 10361da177e4SLinus Torvalds /* 1037f9d7562fSJ. Bruce Fields * We store the NONE, READ, WRITE, and BOTH bits separately in the 1038f9d7562fSJ. Bruce Fields * st_{access,deny}_bmap field of the stateid, in order to track not 1039f9d7562fSJ. Bruce Fields * only what share bits are currently in force, but also what 1040f9d7562fSJ. Bruce Fields * combinations of share bits previous opens have used. This allows us 1041f9d7562fSJ. Bruce Fields * to enforce the recommendation of rfc 3530 14.2.19 that the server 1042f9d7562fSJ. Bruce Fields * return an error if the client attempt to downgrade to a combination 1043f9d7562fSJ. Bruce Fields * of share bits not explicable by closing some of its previous opens. 1044f9d7562fSJ. Bruce Fields * 1045f9d7562fSJ. Bruce Fields * XXX: This enforcement is actually incomplete, since we don't keep 1046f9d7562fSJ. Bruce Fields * track of access/deny bit combinations; so, e.g., we allow: 1047f9d7562fSJ. Bruce Fields * 1048f9d7562fSJ. Bruce Fields * OPEN allow read, deny write 1049f9d7562fSJ. Bruce Fields * OPEN allow both, deny none 1050f9d7562fSJ. Bruce Fields * DOWNGRADE allow read, deny none 1051f9d7562fSJ. Bruce Fields * 1052f9d7562fSJ. Bruce Fields * which we should reject. 1053f9d7562fSJ. Bruce Fields */ 10545ae037e5SJeff Layton static unsigned int 10555ae037e5SJeff Layton bmap_to_share_mode(unsigned long bmap) { 1056f9d7562fSJ. Bruce Fields int i; 10575ae037e5SJeff Layton unsigned int access = 0; 1058f9d7562fSJ. Bruce Fields 1059f9d7562fSJ. Bruce Fields for (i = 1; i < 4; i++) { 1060f9d7562fSJ. Bruce Fields if (test_bit(i, &bmap)) 10615ae037e5SJeff Layton access |= i; 1062f9d7562fSJ. Bruce Fields } 10635ae037e5SJeff Layton return access; 1064f9d7562fSJ. Bruce Fields } 1065f9d7562fSJ. Bruce Fields 106682c5ff1bSJeff Layton /* set share access for a given stateid */ 106782c5ff1bSJeff Layton static inline void 106882c5ff1bSJeff Layton set_access(u32 access, struct nfs4_ol_stateid *stp) 106982c5ff1bSJeff Layton { 1070c11c591fSJeff Layton unsigned char mask = 1 << access; 1071c11c591fSJeff Layton 1072c11c591fSJeff Layton WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); 1073c11c591fSJeff Layton stp->st_access_bmap |= mask; 107482c5ff1bSJeff Layton } 107582c5ff1bSJeff Layton 107682c5ff1bSJeff Layton /* clear share access for a given stateid */ 107782c5ff1bSJeff Layton static inline void 107882c5ff1bSJeff Layton clear_access(u32 access, struct nfs4_ol_stateid *stp) 107982c5ff1bSJeff Layton { 1080c11c591fSJeff Layton unsigned char mask = 1 << access; 1081c11c591fSJeff Layton 1082c11c591fSJeff Layton WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); 1083c11c591fSJeff Layton stp->st_access_bmap &= ~mask; 108482c5ff1bSJeff Layton } 108582c5ff1bSJeff Layton 108682c5ff1bSJeff Layton /* test whether a given stateid has access */ 108782c5ff1bSJeff Layton static inline bool 108882c5ff1bSJeff Layton test_access(u32 access, struct nfs4_ol_stateid *stp) 108982c5ff1bSJeff Layton { 1090c11c591fSJeff Layton unsigned char mask = 1 << access; 1091c11c591fSJeff Layton 1092c11c591fSJeff Layton return (bool)(stp->st_access_bmap & mask); 109382c5ff1bSJeff Layton } 109482c5ff1bSJeff Layton 1095ce0fc43cSJeff Layton /* set share deny for a given stateid */ 1096ce0fc43cSJeff Layton static inline void 1097c11c591fSJeff Layton set_deny(u32 deny, struct nfs4_ol_stateid *stp) 1098ce0fc43cSJeff Layton { 1099c11c591fSJeff Layton unsigned char mask = 1 << deny; 1100c11c591fSJeff Layton 1101c11c591fSJeff Layton WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); 1102c11c591fSJeff Layton stp->st_deny_bmap |= mask; 1103ce0fc43cSJeff Layton } 1104ce0fc43cSJeff Layton 1105ce0fc43cSJeff Layton /* clear share deny for a given stateid */ 1106ce0fc43cSJeff Layton static inline void 1107c11c591fSJeff Layton clear_deny(u32 deny, struct nfs4_ol_stateid *stp) 1108ce0fc43cSJeff Layton { 1109c11c591fSJeff Layton unsigned char mask = 1 << deny; 1110c11c591fSJeff Layton 1111c11c591fSJeff Layton WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); 1112c11c591fSJeff Layton stp->st_deny_bmap &= ~mask; 1113ce0fc43cSJeff Layton } 1114ce0fc43cSJeff Layton 1115ce0fc43cSJeff Layton /* test whether a given stateid is denying specific access */ 1116ce0fc43cSJeff Layton static inline bool 1117c11c591fSJeff Layton test_deny(u32 deny, struct nfs4_ol_stateid *stp) 1118ce0fc43cSJeff Layton { 1119c11c591fSJeff Layton unsigned char mask = 1 << deny; 1120c11c591fSJeff Layton 1121c11c591fSJeff Layton return (bool)(stp->st_deny_bmap & mask); 1122f9d7562fSJ. Bruce Fields } 1123f9d7562fSJ. Bruce Fields 1124f9d7562fSJ. Bruce Fields static int nfs4_access_to_omode(u32 access) 1125f9d7562fSJ. Bruce Fields { 11268f34a430SJ. Bruce Fields switch (access & NFS4_SHARE_ACCESS_BOTH) { 1127f9d7562fSJ. Bruce Fields case NFS4_SHARE_ACCESS_READ: 1128f9d7562fSJ. Bruce Fields return O_RDONLY; 1129f9d7562fSJ. Bruce Fields case NFS4_SHARE_ACCESS_WRITE: 1130f9d7562fSJ. Bruce Fields return O_WRONLY; 1131f9d7562fSJ. Bruce Fields case NFS4_SHARE_ACCESS_BOTH: 1132f9d7562fSJ. Bruce Fields return O_RDWR; 1133f9d7562fSJ. Bruce Fields } 1134063b0fb9SJ. Bruce Fields WARN_ON_ONCE(1); 1135063b0fb9SJ. Bruce Fields return O_RDONLY; 1136f9d7562fSJ. Bruce Fields } 1137f9d7562fSJ. Bruce Fields 1138baeb4ff0SJeff Layton /* 1139baeb4ff0SJeff Layton * A stateid that had a deny mode associated with it is being released 1140baeb4ff0SJeff Layton * or downgraded. Recalculate the deny mode on the file. 1141baeb4ff0SJeff Layton */ 1142baeb4ff0SJeff Layton static void 1143baeb4ff0SJeff Layton recalculate_deny_mode(struct nfs4_file *fp) 1144baeb4ff0SJeff Layton { 1145baeb4ff0SJeff Layton struct nfs4_ol_stateid *stp; 1146baeb4ff0SJeff Layton 1147baeb4ff0SJeff Layton spin_lock(&fp->fi_lock); 1148baeb4ff0SJeff Layton fp->fi_share_deny = 0; 1149baeb4ff0SJeff Layton list_for_each_entry(stp, &fp->fi_stateids, st_perfile) 1150baeb4ff0SJeff Layton fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); 1151baeb4ff0SJeff Layton spin_unlock(&fp->fi_lock); 1152baeb4ff0SJeff Layton } 1153baeb4ff0SJeff Layton 1154baeb4ff0SJeff Layton static void 1155baeb4ff0SJeff Layton reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) 1156baeb4ff0SJeff Layton { 1157baeb4ff0SJeff Layton int i; 1158baeb4ff0SJeff Layton bool change = false; 1159baeb4ff0SJeff Layton 1160baeb4ff0SJeff Layton for (i = 1; i < 4; i++) { 1161baeb4ff0SJeff Layton if ((i & deny) != i) { 1162baeb4ff0SJeff Layton change = true; 1163baeb4ff0SJeff Layton clear_deny(i, stp); 1164baeb4ff0SJeff Layton } 1165baeb4ff0SJeff Layton } 1166baeb4ff0SJeff Layton 1167baeb4ff0SJeff Layton /* Recalculate per-file deny mode if there was a change */ 1168baeb4ff0SJeff Layton if (change) 116911b9164aSTrond Myklebust recalculate_deny_mode(stp->st_stid.sc_file); 1170baeb4ff0SJeff Layton } 1171baeb4ff0SJeff Layton 117282c5ff1bSJeff Layton /* release all access and file references for a given stateid */ 117382c5ff1bSJeff Layton static void 117482c5ff1bSJeff Layton release_all_access(struct nfs4_ol_stateid *stp) 117582c5ff1bSJeff Layton { 117682c5ff1bSJeff Layton int i; 117711b9164aSTrond Myklebust struct nfs4_file *fp = stp->st_stid.sc_file; 1178baeb4ff0SJeff Layton 1179baeb4ff0SJeff Layton if (fp && stp->st_deny_bmap != 0) 1180baeb4ff0SJeff Layton recalculate_deny_mode(fp); 118182c5ff1bSJeff Layton 118282c5ff1bSJeff Layton for (i = 1; i < 4; i++) { 118382c5ff1bSJeff Layton if (test_access(i, stp)) 118411b9164aSTrond Myklebust nfs4_file_put_access(stp->st_stid.sc_file, i); 118582c5ff1bSJeff Layton clear_access(i, stp); 118682c5ff1bSJeff Layton } 118782c5ff1bSJeff Layton } 118882c5ff1bSJeff Layton 1189d50ffdedSKinglong Mee static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) 1190d50ffdedSKinglong Mee { 1191d50ffdedSKinglong Mee kfree(sop->so_owner.data); 1192d50ffdedSKinglong Mee sop->so_ops->so_free(sop); 1193d50ffdedSKinglong Mee } 1194d50ffdedSKinglong Mee 11956b180f0bSJeff Layton static void nfs4_put_stateowner(struct nfs4_stateowner *sop) 11966b180f0bSJeff Layton { 1197a819ecc1SJeff Layton struct nfs4_client *clp = sop->so_client; 1198a819ecc1SJeff Layton 1199a819ecc1SJeff Layton might_lock(&clp->cl_lock); 1200a819ecc1SJeff Layton 1201a819ecc1SJeff Layton if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) 12026b180f0bSJeff Layton return; 12038f4b54c5SJeff Layton sop->so_ops->so_unhash(sop); 1204a819ecc1SJeff Layton spin_unlock(&clp->cl_lock); 1205d50ffdedSKinglong Mee nfs4_free_stateowner(sop); 12066b180f0bSJeff Layton } 12076b180f0bSJeff Layton 1208e8568739SJeff Layton static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) 1209529d7b2aSJ. Bruce Fields { 121011b9164aSTrond Myklebust struct nfs4_file *fp = stp->st_stid.sc_file; 12111d31a253STrond Myklebust 12121c755dc1SJeff Layton lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); 12131c755dc1SJeff Layton 1214e8568739SJeff Layton if (list_empty(&stp->st_perfile)) 1215e8568739SJeff Layton return false; 1216e8568739SJeff Layton 12171d31a253STrond Myklebust spin_lock(&fp->fi_lock); 1218e8568739SJeff Layton list_del_init(&stp->st_perfile); 12191d31a253STrond Myklebust spin_unlock(&fp->fi_lock); 1220529d7b2aSJ. Bruce Fields list_del(&stp->st_perstateowner); 1221e8568739SJeff Layton return true; 1222529d7b2aSJ. Bruce Fields } 1223529d7b2aSJ. Bruce Fields 12246011695dSTrond Myklebust static void nfs4_free_ol_stateid(struct nfs4_stid *stid) 1225529d7b2aSJ. Bruce Fields { 12266011695dSTrond Myklebust struct nfs4_ol_stateid *stp = openlockstateid(stid); 12274665e2baSJ. Bruce Fields 12288287f009SSachin Bhamare put_clnt_odstate(stp->st_clnt_odstate); 12296011695dSTrond Myklebust release_all_access(stp); 1230d3134b10SJeff Layton if (stp->st_stateowner) 1231d3134b10SJeff Layton nfs4_put_stateowner(stp->st_stateowner); 12326011695dSTrond Myklebust kmem_cache_free(stateid_slab, stid); 1233529d7b2aSJ. Bruce Fields } 1234529d7b2aSJ. Bruce Fields 1235b49e084dSJeff Layton static void nfs4_free_lock_stateid(struct nfs4_stid *stid) 1236529d7b2aSJ. Bruce Fields { 1237b49e084dSJeff Layton struct nfs4_ol_stateid *stp = openlockstateid(stid); 1238b49e084dSJeff Layton struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); 1239529d7b2aSJ. Bruce Fields struct file *file; 1240529d7b2aSJ. Bruce Fields 1241b49e084dSJeff Layton file = find_any_file(stp->st_stid.sc_file); 1242b49e084dSJeff Layton if (file) 1243b49e084dSJeff Layton filp_close(file, (fl_owner_t)lo); 1244b49e084dSJeff Layton nfs4_free_ol_stateid(stid); 1245b49e084dSJeff Layton } 1246b49e084dSJeff Layton 12472c41beb0SJeff Layton /* 12482c41beb0SJeff Layton * Put the persistent reference to an already unhashed generic stateid, while 12492c41beb0SJeff Layton * holding the cl_lock. If it's the last reference, then put it onto the 12502c41beb0SJeff Layton * reaplist for later destruction. 12512c41beb0SJeff Layton */ 12522c41beb0SJeff Layton static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, 12532c41beb0SJeff Layton struct list_head *reaplist) 12542c41beb0SJeff Layton { 12552c41beb0SJeff Layton struct nfs4_stid *s = &stp->st_stid; 12562c41beb0SJeff Layton struct nfs4_client *clp = s->sc_client; 12572c41beb0SJeff Layton 12582c41beb0SJeff Layton lockdep_assert_held(&clp->cl_lock); 12592c41beb0SJeff Layton 12602c41beb0SJeff Layton WARN_ON_ONCE(!list_empty(&stp->st_locks)); 12612c41beb0SJeff Layton 1262a15dfcd5SElena Reshetova if (!refcount_dec_and_test(&s->sc_count)) { 12632c41beb0SJeff Layton wake_up_all(&close_wq); 12642c41beb0SJeff Layton return; 12652c41beb0SJeff Layton } 12662c41beb0SJeff Layton 12672c41beb0SJeff Layton idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); 12682c41beb0SJeff Layton list_add(&stp->st_locks, reaplist); 12692c41beb0SJeff Layton } 12702c41beb0SJeff Layton 1271e8568739SJeff Layton static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) 12723c1c995cSJeff Layton { 1273f46c445bSChuck Lever lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); 12743c1c995cSJeff Layton 12753c1c995cSJeff Layton list_del_init(&stp->st_locks); 1276cd61c522SChristoph Hellwig nfs4_unhash_stid(&stp->st_stid); 1277e8568739SJeff Layton return unhash_ol_stateid(stp); 12783c1c995cSJeff Layton } 12793c1c995cSJeff Layton 12805adfd885SJeff Layton static void release_lock_stateid(struct nfs4_ol_stateid *stp) 1281b49e084dSJeff Layton { 1282f46c445bSChuck Lever struct nfs4_client *clp = stp->st_stid.sc_client; 1283e8568739SJeff Layton bool unhashed; 12841c755dc1SJeff Layton 1285f46c445bSChuck Lever spin_lock(&clp->cl_lock); 1286e8568739SJeff Layton unhashed = unhash_lock_stateid(stp); 1287f46c445bSChuck Lever spin_unlock(&clp->cl_lock); 1288e8568739SJeff Layton if (unhashed) 12896011695dSTrond Myklebust nfs4_put_stid(&stp->st_stid); 1290529d7b2aSJ. Bruce Fields } 1291529d7b2aSJ. Bruce Fields 1292c58c6610STrond Myklebust static void unhash_lockowner_locked(struct nfs4_lockowner *lo) 1293529d7b2aSJ. Bruce Fields { 1294d4f0489fSTrond Myklebust struct nfs4_client *clp = lo->lo_owner.so_client; 1295c58c6610STrond Myklebust 1296d4f0489fSTrond Myklebust lockdep_assert_held(&clp->cl_lock); 1297c58c6610STrond Myklebust 12988f4b54c5SJeff Layton list_del_init(&lo->lo_owner.so_strhash); 12998f4b54c5SJeff Layton } 13008f4b54c5SJeff Layton 13012c41beb0SJeff Layton /* 13022c41beb0SJeff Layton * Free a list of generic stateids that were collected earlier after being 13032c41beb0SJeff Layton * fully unhashed. 13042c41beb0SJeff Layton */ 13052c41beb0SJeff Layton static void 13062c41beb0SJeff Layton free_ol_stateid_reaplist(struct list_head *reaplist) 13072c41beb0SJeff Layton { 13082c41beb0SJeff Layton struct nfs4_ol_stateid *stp; 1309fb94d766SKinglong Mee struct nfs4_file *fp; 13102c41beb0SJeff Layton 13112c41beb0SJeff Layton might_sleep(); 13122c41beb0SJeff Layton 13132c41beb0SJeff Layton while (!list_empty(reaplist)) { 13142c41beb0SJeff Layton stp = list_first_entry(reaplist, struct nfs4_ol_stateid, 13152c41beb0SJeff Layton st_locks); 13162c41beb0SJeff Layton list_del(&stp->st_locks); 1317fb94d766SKinglong Mee fp = stp->st_stid.sc_file; 13182c41beb0SJeff Layton stp->st_stid.sc_free(&stp->st_stid); 1319fb94d766SKinglong Mee if (fp) 1320fb94d766SKinglong Mee put_nfs4_file(fp); 13212c41beb0SJeff Layton } 13222c41beb0SJeff Layton } 13232c41beb0SJeff Layton 1324d83017f9SJeff Layton static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, 1325d83017f9SJeff Layton struct list_head *reaplist) 13263c87b9b7STrond Myklebust { 13273c87b9b7STrond Myklebust struct nfs4_ol_stateid *stp; 13283c87b9b7STrond Myklebust 1329e8568739SJeff Layton lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); 1330e8568739SJeff Layton 13313c87b9b7STrond Myklebust while (!list_empty(&open_stp->st_locks)) { 13323c87b9b7STrond Myklebust stp = list_entry(open_stp->st_locks.next, 13333c87b9b7STrond Myklebust struct nfs4_ol_stateid, st_locks); 1334e8568739SJeff Layton WARN_ON(!unhash_lock_stateid(stp)); 1335d83017f9SJeff Layton put_ol_stateid_locked(stp, reaplist); 1336529d7b2aSJ. Bruce Fields } 1337529d7b2aSJ. Bruce Fields } 1338529d7b2aSJ. Bruce Fields 1339e8568739SJeff Layton static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, 1340d83017f9SJeff Layton struct list_head *reaplist) 13412283963fSJ. Bruce Fields { 1342e8568739SJeff Layton bool unhashed; 1343e8568739SJeff Layton 13442c41beb0SJeff Layton lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); 13452c41beb0SJeff Layton 1346e8568739SJeff Layton unhashed = unhash_ol_stateid(stp); 1347d83017f9SJeff Layton release_open_stateid_locks(stp, reaplist); 1348e8568739SJeff Layton return unhashed; 134938c387b5SJ. Bruce Fields } 135038c387b5SJ. Bruce Fields 135138c387b5SJ. Bruce Fields static void release_open_stateid(struct nfs4_ol_stateid *stp) 135238c387b5SJ. Bruce Fields { 13532c41beb0SJeff Layton LIST_HEAD(reaplist); 13542c41beb0SJeff Layton 13552c41beb0SJeff Layton spin_lock(&stp->st_stid.sc_client->cl_lock); 1356e8568739SJeff Layton if (unhash_open_stateid(stp, &reaplist)) 13572c41beb0SJeff Layton put_ol_stateid_locked(stp, &reaplist); 13582c41beb0SJeff Layton spin_unlock(&stp->st_stid.sc_client->cl_lock); 13592c41beb0SJeff Layton free_ol_stateid_reaplist(&reaplist); 13602283963fSJ. Bruce Fields } 13612283963fSJ. Bruce Fields 13627ffb5880STrond Myklebust static void unhash_openowner_locked(struct nfs4_openowner *oo) 1363f1d110caSJ. Bruce Fields { 1364d4f0489fSTrond Myklebust struct nfs4_client *clp = oo->oo_owner.so_client; 13657ffb5880STrond Myklebust 1366d4f0489fSTrond Myklebust lockdep_assert_held(&clp->cl_lock); 13677ffb5880STrond Myklebust 13688f4b54c5SJeff Layton list_del_init(&oo->oo_owner.so_strhash); 13698f4b54c5SJeff Layton list_del_init(&oo->oo_perclient); 1370f1d110caSJ. Bruce Fields } 1371f1d110caSJ. Bruce Fields 1372f7a4d872SJ. Bruce Fields static void release_last_closed_stateid(struct nfs4_openowner *oo) 1373f7a4d872SJ. Bruce Fields { 1374217526e7SJeff Layton struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, 1375217526e7SJeff Layton nfsd_net_id); 1376217526e7SJeff Layton struct nfs4_ol_stateid *s; 1377f7a4d872SJ. Bruce Fields 1378217526e7SJeff Layton spin_lock(&nn->client_lock); 1379217526e7SJeff Layton s = oo->oo_last_closed_stid; 1380f7a4d872SJ. Bruce Fields if (s) { 1381d3134b10SJeff Layton list_del_init(&oo->oo_close_lru); 1382f7a4d872SJ. Bruce Fields oo->oo_last_closed_stid = NULL; 1383f7a4d872SJ. Bruce Fields } 1384217526e7SJeff Layton spin_unlock(&nn->client_lock); 1385217526e7SJeff Layton if (s) 1386217526e7SJeff Layton nfs4_put_stid(&s->st_stid); 1387f7a4d872SJ. Bruce Fields } 1388f7a4d872SJ. Bruce Fields 13892c41beb0SJeff Layton static void release_openowner(struct nfs4_openowner *oo) 13908f4b54c5SJeff Layton { 13918f4b54c5SJeff Layton struct nfs4_ol_stateid *stp; 1392d4f0489fSTrond Myklebust struct nfs4_client *clp = oo->oo_owner.so_client; 13932c41beb0SJeff Layton struct list_head reaplist; 13947ffb5880STrond Myklebust 13952c41beb0SJeff Layton INIT_LIST_HEAD(&reaplist); 13967ffb5880STrond Myklebust 1397d4f0489fSTrond Myklebust spin_lock(&clp->cl_lock); 13987ffb5880STrond Myklebust unhash_openowner_locked(oo); 13992c41beb0SJeff Layton while (!list_empty(&oo->oo_owner.so_stateids)) { 14002c41beb0SJeff Layton stp = list_first_entry(&oo->oo_owner.so_stateids, 14012c41beb0SJeff Layton struct nfs4_ol_stateid, st_perstateowner); 1402e8568739SJeff Layton if (unhash_open_stateid(stp, &reaplist)) 14032c41beb0SJeff Layton put_ol_stateid_locked(stp, &reaplist); 14042c41beb0SJeff Layton } 1405d4f0489fSTrond Myklebust spin_unlock(&clp->cl_lock); 14062c41beb0SJeff Layton free_ol_stateid_reaplist(&reaplist); 1407f7a4d872SJ. Bruce Fields release_last_closed_stateid(oo); 14086b180f0bSJeff Layton nfs4_put_stateowner(&oo->oo_owner); 1409f1d110caSJ. Bruce Fields } 1410f1d110caSJ. Bruce Fields 14115282fd72SMarc Eshel static inline int 14125282fd72SMarc Eshel hash_sessionid(struct nfs4_sessionid *sessionid) 14135282fd72SMarc Eshel { 14145282fd72SMarc Eshel struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; 14155282fd72SMarc Eshel 14165282fd72SMarc Eshel return sid->sequence % SESSION_HASH_SIZE; 14175282fd72SMarc Eshel } 14185282fd72SMarc Eshel 1419135dd002SMark Salter #ifdef CONFIG_SUNRPC_DEBUG 14205282fd72SMarc Eshel static inline void 14215282fd72SMarc Eshel dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) 14225282fd72SMarc Eshel { 14235282fd72SMarc Eshel u32 *ptr = (u32 *)(&sessionid->data[0]); 14245282fd72SMarc Eshel dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); 14255282fd72SMarc Eshel } 14268f199b82STrond Myklebust #else 14278f199b82STrond Myklebust static inline void 14288f199b82STrond Myklebust dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) 14298f199b82STrond Myklebust { 14308f199b82STrond Myklebust } 14318f199b82STrond Myklebust #endif 14328f199b82STrond Myklebust 14339411b1d4SJ. Bruce Fields /* 14349411b1d4SJ. Bruce Fields * Bump the seqid on cstate->replay_owner, and clear replay_owner if it 14359411b1d4SJ. Bruce Fields * won't be used for replay. 14369411b1d4SJ. Bruce Fields */ 14379411b1d4SJ. Bruce Fields void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) 14389411b1d4SJ. Bruce Fields { 14399411b1d4SJ. Bruce Fields struct nfs4_stateowner *so = cstate->replay_owner; 14409411b1d4SJ. Bruce Fields 14419411b1d4SJ. Bruce Fields if (nfserr == nfserr_replay_me) 14429411b1d4SJ. Bruce Fields return; 14439411b1d4SJ. Bruce Fields 14449411b1d4SJ. Bruce Fields if (!seqid_mutating_err(ntohl(nfserr))) { 144558fb12e6SJeff Layton nfsd4_cstate_clear_replay(cstate); 14469411b1d4SJ. Bruce Fields return; 14479411b1d4SJ. Bruce Fields } 14489411b1d4SJ. Bruce Fields if (!so) 14499411b1d4SJ. Bruce Fields return; 14509411b1d4SJ. Bruce Fields if (so->so_is_open_owner) 14519411b1d4SJ. Bruce Fields release_last_closed_stateid(openowner(so)); 14529411b1d4SJ. Bruce Fields so->so_seqid++; 14539411b1d4SJ. Bruce Fields return; 14549411b1d4SJ. Bruce Fields } 14555282fd72SMarc Eshel 1456ec6b5d7bSAndy Adamson static void 1457ec6b5d7bSAndy Adamson gen_sessionid(struct nfsd4_session *ses) 1458ec6b5d7bSAndy Adamson { 1459ec6b5d7bSAndy Adamson struct nfs4_client *clp = ses->se_client; 1460ec6b5d7bSAndy Adamson struct nfsd4_sessionid *sid; 1461ec6b5d7bSAndy Adamson 1462ec6b5d7bSAndy Adamson sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; 1463ec6b5d7bSAndy Adamson sid->clientid = clp->cl_clientid; 1464ec6b5d7bSAndy Adamson sid->sequence = current_sessionid++; 1465ec6b5d7bSAndy Adamson sid->reserved = 0; 1466ec6b5d7bSAndy Adamson } 1467ec6b5d7bSAndy Adamson 1468ec6b5d7bSAndy Adamson /* 1469a649637cSAndy Adamson * The protocol defines ca_maxresponssize_cached to include the size of 1470a649637cSAndy Adamson * the rpc header, but all we need to cache is the data starting after 1471a649637cSAndy Adamson * the end of the initial SEQUENCE operation--the rest we regenerate 1472a649637cSAndy Adamson * each time. Therefore we can advertise a ca_maxresponssize_cached 1473a649637cSAndy Adamson * value that is the number of bytes in our cache plus a few additional 1474a649637cSAndy Adamson * bytes. In order to stay on the safe side, and not promise more than 1475a649637cSAndy Adamson * we can cache, those additional bytes must be the minimum possible: 24 1476a649637cSAndy Adamson * bytes of rpc header (xid through accept state, with AUTH_NULL 1477a649637cSAndy Adamson * verifier), 12 for the compound header (with zero-length tag), and 44 1478a649637cSAndy Adamson * for the SEQUENCE op response: 1479ec6b5d7bSAndy Adamson */ 1480a649637cSAndy Adamson #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) 1481a649637cSAndy Adamson 1482557ce264SAndy Adamson static void 1483557ce264SAndy Adamson free_session_slots(struct nfsd4_session *ses) 1484557ce264SAndy Adamson { 1485557ce264SAndy Adamson int i; 1486557ce264SAndy Adamson 148753da6a53SJ. Bruce Fields for (i = 0; i < ses->se_fchannel.maxreqs; i++) { 148853da6a53SJ. Bruce Fields free_svc_cred(&ses->se_slots[i]->sl_cred); 1489557ce264SAndy Adamson kfree(ses->se_slots[i]); 1490557ce264SAndy Adamson } 149153da6a53SJ. Bruce Fields } 1492557ce264SAndy Adamson 1493efe0cb6dSJ. Bruce Fields /* 1494efe0cb6dSJ. Bruce Fields * We don't actually need to cache the rpc and session headers, so we 1495efe0cb6dSJ. Bruce Fields * can allocate a little less for each slot: 1496efe0cb6dSJ. Bruce Fields */ 149755c760cfSJ. Bruce Fields static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) 1498efe0cb6dSJ. Bruce Fields { 149955c760cfSJ. Bruce Fields u32 size; 1500efe0cb6dSJ. Bruce Fields 150155c760cfSJ. Bruce Fields if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) 150255c760cfSJ. Bruce Fields size = 0; 150355c760cfSJ. Bruce Fields else 150455c760cfSJ. Bruce Fields size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; 150555c760cfSJ. Bruce Fields return size + sizeof(struct nfsd4_slot); 1506557ce264SAndy Adamson } 1507557ce264SAndy Adamson 15085b6feee9SJ. Bruce Fields /* 15095b6feee9SJ. Bruce Fields * XXX: If we run out of reserved DRC memory we could (up to a point) 15105b6feee9SJ. Bruce Fields * re-negotiate active sessions and reduce their slot usage to make 151142b2aa86SJustin P. Mattock * room for new connections. For now we just fail the create session. 15125b6feee9SJ. Bruce Fields */ 151355c760cfSJ. Bruce Fields static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) 15145b6feee9SJ. Bruce Fields { 151555c760cfSJ. Bruce Fields u32 slotsize = slot_bytes(ca); 151655c760cfSJ. Bruce Fields u32 num = ca->maxreqs; 15175b6feee9SJ. Bruce Fields int avail; 15185b6feee9SJ. Bruce Fields 15195b6feee9SJ. Bruce Fields spin_lock(&nfsd_drc_lock); 1520697ce9beSZhang Yanfei avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, 15215b6feee9SJ. Bruce Fields nfsd_drc_max_mem - nfsd_drc_mem_used); 1522de766e57SJ. Bruce Fields /* 1523de766e57SJ. Bruce Fields * Never use more than a third of the remaining memory, 1524de766e57SJ. Bruce Fields * unless it's the only way to give this client a slot: 1525de766e57SJ. Bruce Fields */ 1526de766e57SJ. Bruce Fields avail = clamp_t(int, avail, slotsize, avail/3); 15275b6feee9SJ. Bruce Fields num = min_t(int, num, avail / slotsize); 15285b6feee9SJ. Bruce Fields nfsd_drc_mem_used += num * slotsize; 15295b6feee9SJ. Bruce Fields spin_unlock(&nfsd_drc_lock); 15305b6feee9SJ. Bruce Fields 15315b6feee9SJ. Bruce Fields return num; 15325b6feee9SJ. Bruce Fields } 15335b6feee9SJ. Bruce Fields 153455c760cfSJ. Bruce Fields static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) 15355b6feee9SJ. Bruce Fields { 153655c760cfSJ. Bruce Fields int slotsize = slot_bytes(ca); 153755c760cfSJ. Bruce Fields 15385b6feee9SJ. Bruce Fields spin_lock(&nfsd_drc_lock); 153955c760cfSJ. Bruce Fields nfsd_drc_mem_used -= slotsize * ca->maxreqs; 15405b6feee9SJ. Bruce Fields spin_unlock(&nfsd_drc_lock); 15415b6feee9SJ. Bruce Fields } 15425b6feee9SJ. Bruce Fields 154360810e54SKinglong Mee static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, 154460810e54SKinglong Mee struct nfsd4_channel_attrs *battrs) 15455b6feee9SJ. Bruce Fields { 154660810e54SKinglong Mee int numslots = fattrs->maxreqs; 154760810e54SKinglong Mee int slotsize = slot_bytes(fattrs); 15485b6feee9SJ. Bruce Fields struct nfsd4_session *new; 15495b6feee9SJ. Bruce Fields int mem, i; 1550ec6b5d7bSAndy Adamson 1551c23753daSJ. Bruce Fields BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *) 1552ec6b5d7bSAndy Adamson + sizeof(struct nfsd4_session) > PAGE_SIZE); 15535b6feee9SJ. Bruce Fields mem = numslots * sizeof(struct nfsd4_slot *); 1554ec6b5d7bSAndy Adamson 15555b6feee9SJ. Bruce Fields new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); 15566c18ba9fSAlexandros Batsakis if (!new) 15575b6feee9SJ. Bruce Fields return NULL; 1558ec6b5d7bSAndy Adamson /* allocate each struct nfsd4_slot and data cache in one piece */ 15595b6feee9SJ. Bruce Fields for (i = 0; i < numslots; i++) { 156055c760cfSJ. Bruce Fields new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); 15615b6feee9SJ. Bruce Fields if (!new->se_slots[i]) 1562ec6b5d7bSAndy Adamson goto out_free; 1563ec6b5d7bSAndy Adamson } 156460810e54SKinglong Mee 156560810e54SKinglong Mee memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); 156660810e54SKinglong Mee memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); 156760810e54SKinglong Mee 15685b6feee9SJ. Bruce Fields return new; 15695b6feee9SJ. Bruce Fields out_free: 15705b6feee9SJ. Bruce Fields while (i--) 15715b6feee9SJ. Bruce Fields kfree(new->se_slots[i]); 15725b6feee9SJ. Bruce Fields kfree(new); 15735b6feee9SJ. Bruce Fields return NULL; 15745b6feee9SJ. Bruce Fields } 15755b6feee9SJ. Bruce Fields 157619cf5c02SJ. Bruce Fields static void free_conn(struct nfsd4_conn *c) 157719cf5c02SJ. Bruce Fields { 157819cf5c02SJ. Bruce Fields svc_xprt_put(c->cn_xprt); 157919cf5c02SJ. Bruce Fields kfree(c); 158019cf5c02SJ. Bruce Fields } 158119cf5c02SJ. Bruce Fields 158219cf5c02SJ. Bruce Fields static void nfsd4_conn_lost(struct svc_xpt_user *u) 158319cf5c02SJ. Bruce Fields { 158419cf5c02SJ. Bruce Fields struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); 158519cf5c02SJ. Bruce Fields struct nfs4_client *clp = c->cn_session->se_client; 158619cf5c02SJ. Bruce Fields 158719cf5c02SJ. Bruce Fields spin_lock(&clp->cl_lock); 158819cf5c02SJ. Bruce Fields if (!list_empty(&c->cn_persession)) { 158919cf5c02SJ. Bruce Fields list_del(&c->cn_persession); 159019cf5c02SJ. Bruce Fields free_conn(c); 159119cf5c02SJ. Bruce Fields } 1592eea49806SJ. Bruce Fields nfsd4_probe_callback(clp); 15932e4b7239SJ. Bruce Fields spin_unlock(&clp->cl_lock); 159419cf5c02SJ. Bruce Fields } 159519cf5c02SJ. Bruce Fields 1596d29c374cSJ. Bruce Fields static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) 1597c7662518SJ. Bruce Fields { 1598c7662518SJ. Bruce Fields struct nfsd4_conn *conn; 1599c7662518SJ. Bruce Fields 1600c7662518SJ. Bruce Fields conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); 1601c7662518SJ. Bruce Fields if (!conn) 1602db90681dSJ. Bruce Fields return NULL; 1603c7662518SJ. Bruce Fields svc_xprt_get(rqstp->rq_xprt); 1604c7662518SJ. Bruce Fields conn->cn_xprt = rqstp->rq_xprt; 1605d29c374cSJ. Bruce Fields conn->cn_flags = flags; 1606db90681dSJ. Bruce Fields INIT_LIST_HEAD(&conn->cn_xpt_user.list); 1607db90681dSJ. Bruce Fields return conn; 1608db90681dSJ. Bruce Fields } 1609db90681dSJ. Bruce Fields 1610328ead28SJ. Bruce Fields static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) 1611328ead28SJ. Bruce Fields { 1612328ead28SJ. Bruce Fields conn->cn_session = ses; 1613328ead28SJ. Bruce Fields list_add(&conn->cn_persession, &ses->se_conns); 1614328ead28SJ. Bruce Fields } 1615328ead28SJ. Bruce Fields 1616db90681dSJ. Bruce Fields static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) 1617db90681dSJ. Bruce Fields { 1618db90681dSJ. Bruce Fields struct nfs4_client *clp = ses->se_client; 1619c7662518SJ. Bruce Fields 1620c7662518SJ. Bruce Fields spin_lock(&clp->cl_lock); 1621328ead28SJ. Bruce Fields __nfsd4_hash_conn(conn, ses); 1622c7662518SJ. Bruce Fields spin_unlock(&clp->cl_lock); 1623db90681dSJ. Bruce Fields } 1624c7662518SJ. Bruce Fields 162521b75b01SJ. Bruce Fields static int nfsd4_register_conn(struct nfsd4_conn *conn) 1626db90681dSJ. Bruce Fields { 162719cf5c02SJ. Bruce Fields conn->cn_xpt_user.callback = nfsd4_conn_lost; 162821b75b01SJ. Bruce Fields return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); 1629db90681dSJ. Bruce Fields } 1630db90681dSJ. Bruce Fields 1631e1ff371fSJ. Bruce Fields static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) 1632db90681dSJ. Bruce Fields { 163321b75b01SJ. Bruce Fields int ret; 1634db90681dSJ. Bruce Fields 1635db90681dSJ. Bruce Fields nfsd4_hash_conn(conn, ses); 163621b75b01SJ. Bruce Fields ret = nfsd4_register_conn(conn); 163721b75b01SJ. Bruce Fields if (ret) 163821b75b01SJ. Bruce Fields /* oops; xprt is already down: */ 163921b75b01SJ. Bruce Fields nfsd4_conn_lost(&conn->cn_xpt_user); 164057a37144SJ. Bruce Fields /* We may have gained or lost a callback channel: */ 164157a37144SJ. Bruce Fields nfsd4_probe_callback_sync(ses->se_client); 1642c7662518SJ. Bruce Fields } 1643c7662518SJ. Bruce Fields 1644e1ff371fSJ. Bruce Fields static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) 16451d1bc8f2SJ. Bruce Fields { 16461d1bc8f2SJ. Bruce Fields u32 dir = NFS4_CDFC4_FORE; 16471d1bc8f2SJ. Bruce Fields 1648e1ff371fSJ. Bruce Fields if (cses->flags & SESSION4_BACK_CHAN) 16491d1bc8f2SJ. Bruce Fields dir |= NFS4_CDFC4_BACK; 1650e1ff371fSJ. Bruce Fields return alloc_conn(rqstp, dir); 16511d1bc8f2SJ. Bruce Fields } 16521d1bc8f2SJ. Bruce Fields 16531d1bc8f2SJ. Bruce Fields /* must be called under client_lock */ 165419cf5c02SJ. Bruce Fields static void nfsd4_del_conns(struct nfsd4_session *s) 1655c7662518SJ. Bruce Fields { 165619cf5c02SJ. Bruce Fields struct nfs4_client *clp = s->se_client; 165719cf5c02SJ. Bruce Fields struct nfsd4_conn *c; 165819cf5c02SJ. Bruce Fields 165919cf5c02SJ. Bruce Fields spin_lock(&clp->cl_lock); 166019cf5c02SJ. Bruce Fields while (!list_empty(&s->se_conns)) { 166119cf5c02SJ. Bruce Fields c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); 166219cf5c02SJ. Bruce Fields list_del_init(&c->cn_persession); 166319cf5c02SJ. Bruce Fields spin_unlock(&clp->cl_lock); 166419cf5c02SJ. Bruce Fields 166519cf5c02SJ. Bruce Fields unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); 166619cf5c02SJ. Bruce Fields free_conn(c); 166719cf5c02SJ. Bruce Fields 166819cf5c02SJ. Bruce Fields spin_lock(&clp->cl_lock); 166919cf5c02SJ. Bruce Fields } 167019cf5c02SJ. Bruce Fields spin_unlock(&clp->cl_lock); 1671c7662518SJ. Bruce Fields } 1672c7662518SJ. Bruce Fields 16731377b69eSJ. Bruce Fields static void __free_session(struct nfsd4_session *ses) 16741377b69eSJ. Bruce Fields { 16751377b69eSJ. Bruce Fields free_session_slots(ses); 16761377b69eSJ. Bruce Fields kfree(ses); 16771377b69eSJ. Bruce Fields } 16781377b69eSJ. Bruce Fields 167966b2b9b2SJ. Bruce Fields static void free_session(struct nfsd4_session *ses) 1680508dc6e1SBenny Halevy { 1681c7662518SJ. Bruce Fields nfsd4_del_conns(ses); 168255c760cfSJ. Bruce Fields nfsd4_put_drc_mem(&ses->se_fchannel); 1683c7662518SJ. Bruce Fields __free_session(ses); 1684a827bcb2SJ. Bruce Fields } 1685ec6b5d7bSAndy Adamson 1686135ae827SFengguang Wu static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) 1687a827bcb2SJ. Bruce Fields { 1688a827bcb2SJ. Bruce Fields int idx; 16891872de0eSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 1690a827bcb2SJ. Bruce Fields 1691ec6b5d7bSAndy Adamson new->se_client = clp; 1692ec6b5d7bSAndy Adamson gen_sessionid(new); 1693ec6b5d7bSAndy Adamson 1694c7662518SJ. Bruce Fields INIT_LIST_HEAD(&new->se_conns); 1695c7662518SJ. Bruce Fields 1696ac7c46f2SJ. Bruce Fields new->se_cb_seq_nr = 1; 1697ec6b5d7bSAndy Adamson new->se_flags = cses->flags; 16988b5ce5cdSJ. Bruce Fields new->se_cb_prog = cses->callback_prog; 1699c6bb3ca2SJ. Bruce Fields new->se_cb_sec = cses->cb_sec; 170066b2b9b2SJ. Bruce Fields atomic_set(&new->se_ref, 0); 17015b6feee9SJ. Bruce Fields idx = hash_sessionid(&new->se_sessionid); 17021872de0eSStanislav Kinsbursky list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); 17034c649378SJ. Bruce Fields spin_lock(&clp->cl_lock); 1704ec6b5d7bSAndy Adamson list_add(&new->se_perclnt, &clp->cl_sessions); 17054c649378SJ. Bruce Fields spin_unlock(&clp->cl_lock); 170660810e54SKinglong Mee 1707b0d2e42cSChuck Lever { 1708edd76786SJ. Bruce Fields struct sockaddr *sa = svc_addr(rqstp); 1709dcbeaa68SJ. Bruce Fields /* 1710dcbeaa68SJ. Bruce Fields * This is a little silly; with sessions there's no real 1711dcbeaa68SJ. Bruce Fields * use for the callback address. Use the peer address 1712dcbeaa68SJ. Bruce Fields * as a reasonable default for now, but consider fixing 1713dcbeaa68SJ. Bruce Fields * the rpc client not to require an address in the 1714dcbeaa68SJ. Bruce Fields * future: 1715dcbeaa68SJ. Bruce Fields */ 1716edd76786SJ. Bruce Fields rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); 1717edd76786SJ. Bruce Fields clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); 1718edd76786SJ. Bruce Fields } 1719ec6b5d7bSAndy Adamson } 1720ec6b5d7bSAndy Adamson 17219089f1b4SBenny Halevy /* caller must hold client_lock */ 17225282fd72SMarc Eshel static struct nfsd4_session * 1723d4e19e70STrond Myklebust __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) 17245282fd72SMarc Eshel { 17255282fd72SMarc Eshel struct nfsd4_session *elem; 17265282fd72SMarc Eshel int idx; 17271872de0eSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 17285282fd72SMarc Eshel 17290a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 17300a880a28STrond Myklebust 17315282fd72SMarc Eshel dump_sessionid(__func__, sessionid); 17325282fd72SMarc Eshel idx = hash_sessionid(sessionid); 17335282fd72SMarc Eshel /* Search in the appropriate list */ 17341872de0eSStanislav Kinsbursky list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { 17355282fd72SMarc Eshel if (!memcmp(elem->se_sessionid.data, sessionid->data, 17365282fd72SMarc Eshel NFS4_MAX_SESSIONID_LEN)) { 17375282fd72SMarc Eshel return elem; 17385282fd72SMarc Eshel } 17395282fd72SMarc Eshel } 17405282fd72SMarc Eshel 17415282fd72SMarc Eshel dprintk("%s: session not found\n", __func__); 17425282fd72SMarc Eshel return NULL; 17435282fd72SMarc Eshel } 17445282fd72SMarc Eshel 1745d4e19e70STrond Myklebust static struct nfsd4_session * 1746d4e19e70STrond Myklebust find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, 1747d4e19e70STrond Myklebust __be32 *ret) 1748d4e19e70STrond Myklebust { 1749d4e19e70STrond Myklebust struct nfsd4_session *session; 1750d4e19e70STrond Myklebust __be32 status = nfserr_badsession; 1751d4e19e70STrond Myklebust 1752d4e19e70STrond Myklebust session = __find_in_sessionid_hashtbl(sessionid, net); 1753d4e19e70STrond Myklebust if (!session) 1754d4e19e70STrond Myklebust goto out; 1755d4e19e70STrond Myklebust status = nfsd4_get_session_locked(session); 1756d4e19e70STrond Myklebust if (status) 1757d4e19e70STrond Myklebust session = NULL; 1758d4e19e70STrond Myklebust out: 1759d4e19e70STrond Myklebust *ret = status; 1760d4e19e70STrond Myklebust return session; 1761d4e19e70STrond Myklebust } 1762d4e19e70STrond Myklebust 17639089f1b4SBenny Halevy /* caller must hold client_lock */ 17647116ed6bSAndy Adamson static void 17655282fd72SMarc Eshel unhash_session(struct nfsd4_session *ses) 17667116ed6bSAndy Adamson { 17670a880a28STrond Myklebust struct nfs4_client *clp = ses->se_client; 17680a880a28STrond Myklebust struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 17690a880a28STrond Myklebust 17700a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 17710a880a28STrond Myklebust 17727116ed6bSAndy Adamson list_del(&ses->se_hash); 17734c649378SJ. Bruce Fields spin_lock(&ses->se_client->cl_lock); 17747116ed6bSAndy Adamson list_del(&ses->se_perclnt); 17754c649378SJ. Bruce Fields spin_unlock(&ses->se_client->cl_lock); 17765282fd72SMarc Eshel } 17775282fd72SMarc Eshel 17781da177e4SLinus Torvalds /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ 17791da177e4SLinus Torvalds static int 17802c142baaSStanislav Kinsbursky STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) 17811da177e4SLinus Torvalds { 1782bbc7f33aSJ. Bruce Fields /* 1783bbc7f33aSJ. Bruce Fields * We're assuming the clid was not given out from a boot 1784bbc7f33aSJ. Bruce Fields * precisely 2^32 (about 136 years) before this one. That seems 1785bbc7f33aSJ. Bruce Fields * a safe assumption: 1786bbc7f33aSJ. Bruce Fields */ 1787bbc7f33aSJ. Bruce Fields if (clid->cl_boot == (u32)nn->boot_time) 17881da177e4SLinus Torvalds return 0; 178960adfc50SAndy Adamson dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", 17902c142baaSStanislav Kinsbursky clid->cl_boot, clid->cl_id, nn->boot_time); 17911da177e4SLinus Torvalds return 1; 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds /* 17951da177e4SLinus Torvalds * XXX Should we use a slab cache ? 17961da177e4SLinus Torvalds * This type of memory management is somewhat inefficient, but we use it 17971da177e4SLinus Torvalds * anyway since SETCLIENTID is not a common operation. 17981da177e4SLinus Torvalds */ 179935bba9a3SJ. Bruce Fields static struct nfs4_client *alloc_client(struct xdr_netobj name) 18001da177e4SLinus Torvalds { 18011da177e4SLinus Torvalds struct nfs4_client *clp; 1802d4f0489fSTrond Myklebust int i; 18031da177e4SLinus Torvalds 18049258a2d5SJeff Layton clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); 180535bba9a3SJ. Bruce Fields if (clp == NULL) 180635bba9a3SJ. Bruce Fields return NULL; 180767114fe6SThomas Meyer clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); 1808d4f0489fSTrond Myklebust if (clp->cl_name.data == NULL) 1809d4f0489fSTrond Myklebust goto err_no_name; 18106da2ec56SKees Cook clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, 18116da2ec56SKees Cook sizeof(struct list_head), 18126da2ec56SKees Cook GFP_KERNEL); 1813d4f0489fSTrond Myklebust if (!clp->cl_ownerstr_hashtbl) 1814d4f0489fSTrond Myklebust goto err_no_hashtbl; 1815d4f0489fSTrond Myklebust for (i = 0; i < OWNER_HASH_SIZE; i++) 1816d4f0489fSTrond Myklebust INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); 18171da177e4SLinus Torvalds clp->cl_name.len = name.len; 18185694c93eSTrond Myklebust INIT_LIST_HEAD(&clp->cl_sessions); 18195694c93eSTrond Myklebust idr_init(&clp->cl_stateids); 18205694c93eSTrond Myklebust atomic_set(&clp->cl_refcount, 0); 18215694c93eSTrond Myklebust clp->cl_cb_state = NFSD4_CB_UNKNOWN; 18225694c93eSTrond Myklebust INIT_LIST_HEAD(&clp->cl_idhash); 18235694c93eSTrond Myklebust INIT_LIST_HEAD(&clp->cl_openowners); 18245694c93eSTrond Myklebust INIT_LIST_HEAD(&clp->cl_delegations); 18255694c93eSTrond Myklebust INIT_LIST_HEAD(&clp->cl_lru); 18265694c93eSTrond Myklebust INIT_LIST_HEAD(&clp->cl_revoked); 18279cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS 18289cf514ccSChristoph Hellwig INIT_LIST_HEAD(&clp->cl_lo_states); 18299cf514ccSChristoph Hellwig #endif 18305694c93eSTrond Myklebust spin_lock_init(&clp->cl_lock); 18315694c93eSTrond Myklebust rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); 18321da177e4SLinus Torvalds return clp; 1833d4f0489fSTrond Myklebust err_no_hashtbl: 1834d4f0489fSTrond Myklebust kfree(clp->cl_name.data); 1835d4f0489fSTrond Myklebust err_no_name: 18369258a2d5SJeff Layton kmem_cache_free(client_slab, clp); 1837d4f0489fSTrond Myklebust return NULL; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 18404dd86e15STrond Myklebust static void 18411da177e4SLinus Torvalds free_client(struct nfs4_client *clp) 18421da177e4SLinus Torvalds { 1843792c95ddSJ. Bruce Fields while (!list_empty(&clp->cl_sessions)) { 1844792c95ddSJ. Bruce Fields struct nfsd4_session *ses; 1845792c95ddSJ. Bruce Fields ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, 1846792c95ddSJ. Bruce Fields se_perclnt); 1847792c95ddSJ. Bruce Fields list_del(&ses->se_perclnt); 184866b2b9b2SJ. Bruce Fields WARN_ON_ONCE(atomic_read(&ses->se_ref)); 184966b2b9b2SJ. Bruce Fields free_session(ses); 1850792c95ddSJ. Bruce Fields } 18514cb57e30STrond Myklebust rpc_destroy_wait_queue(&clp->cl_cb_waitq); 185203a4e1f6SJ. Bruce Fields free_svc_cred(&clp->cl_cred); 1853d4f0489fSTrond Myklebust kfree(clp->cl_ownerstr_hashtbl); 18541da177e4SLinus Torvalds kfree(clp->cl_name.data); 18552d32b29aSmajianpeng idr_destroy(&clp->cl_stateids); 18569258a2d5SJeff Layton kmem_cache_free(client_slab, clp); 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 185984d38ac9SBenny Halevy /* must be called under the client_lock */ 18604beb345bSTrond Myklebust static void 186184d38ac9SBenny Halevy unhash_client_locked(struct nfs4_client *clp) 186284d38ac9SBenny Halevy { 18634beb345bSTrond Myklebust struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1864792c95ddSJ. Bruce Fields struct nfsd4_session *ses; 1865792c95ddSJ. Bruce Fields 18660a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 18670a880a28STrond Myklebust 18684beb345bSTrond Myklebust /* Mark the client as expired! */ 18694beb345bSTrond Myklebust clp->cl_time = 0; 18704beb345bSTrond Myklebust /* Make it invisible */ 18714beb345bSTrond Myklebust if (!list_empty(&clp->cl_idhash)) { 18724beb345bSTrond Myklebust list_del_init(&clp->cl_idhash); 18734beb345bSTrond Myklebust if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) 18744beb345bSTrond Myklebust rb_erase(&clp->cl_namenode, &nn->conf_name_tree); 18754beb345bSTrond Myklebust else 18764beb345bSTrond Myklebust rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); 18774beb345bSTrond Myklebust } 18784beb345bSTrond Myklebust list_del_init(&clp->cl_lru); 18794c649378SJ. Bruce Fields spin_lock(&clp->cl_lock); 1880792c95ddSJ. Bruce Fields list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) 1881792c95ddSJ. Bruce Fields list_del_init(&ses->se_hash); 18824c649378SJ. Bruce Fields spin_unlock(&clp->cl_lock); 188384d38ac9SBenny Halevy } 188484d38ac9SBenny Halevy 18851da177e4SLinus Torvalds static void 18864beb345bSTrond Myklebust unhash_client(struct nfs4_client *clp) 18874beb345bSTrond Myklebust { 18884beb345bSTrond Myklebust struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 18894beb345bSTrond Myklebust 18904beb345bSTrond Myklebust spin_lock(&nn->client_lock); 18914beb345bSTrond Myklebust unhash_client_locked(clp); 18924beb345bSTrond Myklebust spin_unlock(&nn->client_lock); 18934beb345bSTrond Myklebust } 18944beb345bSTrond Myklebust 189597403d95SJeff Layton static __be32 mark_client_expired_locked(struct nfs4_client *clp) 189697403d95SJeff Layton { 189797403d95SJeff Layton if (atomic_read(&clp->cl_refcount)) 189897403d95SJeff Layton return nfserr_jukebox; 189997403d95SJeff Layton unhash_client_locked(clp); 190097403d95SJeff Layton return nfs_ok; 190197403d95SJeff Layton } 190297403d95SJeff Layton 19034beb345bSTrond Myklebust static void 19044beb345bSTrond Myklebust __destroy_client(struct nfs4_client *clp) 19051da177e4SLinus Torvalds { 190668ef3bc3SJeff Layton int i; 1907fe0750e5SJ. Bruce Fields struct nfs4_openowner *oo; 19081da177e4SLinus Torvalds struct nfs4_delegation *dp; 19091da177e4SLinus Torvalds struct list_head reaplist; 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds INIT_LIST_HEAD(&reaplist); 1912cdc97505SBenny Halevy spin_lock(&state_lock); 1913ea1da636SNeilBrown while (!list_empty(&clp->cl_delegations)) { 1914ea1da636SNeilBrown dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); 19153fcbbd24SJeff Layton WARN_ON(!unhash_delegation_locked(dp)); 191642690676SJeff Layton list_add(&dp->dl_recall_lru, &reaplist); 19171da177e4SLinus Torvalds } 1918cdc97505SBenny Halevy spin_unlock(&state_lock); 19191da177e4SLinus Torvalds while (!list_empty(&reaplist)) { 19201da177e4SLinus Torvalds dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); 192142690676SJeff Layton list_del_init(&dp->dl_recall_lru); 19220af6e690SJ. Bruce Fields destroy_unhashed_deleg(dp); 19231da177e4SLinus Torvalds } 19242d4a532dSJeff Layton while (!list_empty(&clp->cl_revoked)) { 1925c876486bSAndrew Elble dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); 19262d4a532dSJeff Layton list_del_init(&dp->dl_recall_lru); 19276011695dSTrond Myklebust nfs4_put_stid(&dp->dl_stid); 1928956c4feeSBenny Halevy } 1929ea1da636SNeilBrown while (!list_empty(&clp->cl_openowners)) { 1930fe0750e5SJ. Bruce Fields oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); 1931b5971afaSKinglong Mee nfs4_get_stateowner(&oo->oo_owner); 1932fe0750e5SJ. Bruce Fields release_openowner(oo); 19331da177e4SLinus Torvalds } 193468ef3bc3SJeff Layton for (i = 0; i < OWNER_HASH_SIZE; i++) { 193568ef3bc3SJeff Layton struct nfs4_stateowner *so, *tmp; 193668ef3bc3SJeff Layton 193768ef3bc3SJeff Layton list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], 193868ef3bc3SJeff Layton so_strhash) { 193968ef3bc3SJeff Layton /* Should be no openowners at this point */ 194068ef3bc3SJeff Layton WARN_ON_ONCE(so->so_is_open_owner); 194168ef3bc3SJeff Layton remove_blocked_locks(lockowner(so)); 194268ef3bc3SJeff Layton } 194368ef3bc3SJeff Layton } 19449cf514ccSChristoph Hellwig nfsd4_return_all_client_layouts(clp); 19456ff8da08SJ. Bruce Fields nfsd4_shutdown_callback(clp); 19462bf23875SJ. Bruce Fields if (clp->cl_cb_conn.cb_xprt) 19472bf23875SJ. Bruce Fields svc_xprt_put(clp->cl_cb_conn.cb_xprt); 1948b12a05cbSJ. Bruce Fields free_client(clp); 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 19514beb345bSTrond Myklebust static void 19524beb345bSTrond Myklebust destroy_client(struct nfs4_client *clp) 19534beb345bSTrond Myklebust { 19544beb345bSTrond Myklebust unhash_client(clp); 19554beb345bSTrond Myklebust __destroy_client(clp); 19564beb345bSTrond Myklebust } 19574beb345bSTrond Myklebust 19580d22f68fSJ. Bruce Fields static void expire_client(struct nfs4_client *clp) 19590d22f68fSJ. Bruce Fields { 19604beb345bSTrond Myklebust unhash_client(clp); 19610d22f68fSJ. Bruce Fields nfsd4_client_record_remove(clp); 19624beb345bSTrond Myklebust __destroy_client(clp); 19630d22f68fSJ. Bruce Fields } 19640d22f68fSJ. Bruce Fields 196535bba9a3SJ. Bruce Fields static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) 196635bba9a3SJ. Bruce Fields { 196735bba9a3SJ. Bruce Fields memcpy(target->cl_verifier.data, source->data, 196835bba9a3SJ. Bruce Fields sizeof(target->cl_verifier.data)); 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds 197135bba9a3SJ. Bruce Fields static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) 197235bba9a3SJ. Bruce Fields { 19731da177e4SLinus Torvalds target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 19741da177e4SLinus Torvalds target->cl_clientid.cl_id = source->cl_clientid.cl_id; 19751da177e4SLinus Torvalds } 19761da177e4SLinus Torvalds 197750043859SJ. Bruce Fields static int copy_cred(struct svc_cred *target, struct svc_cred *source) 197850043859SJ. Bruce Fields { 19792f10fdcbSNeilBrown target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); 19802f10fdcbSNeilBrown target->cr_raw_principal = kstrdup(source->cr_raw_principal, 19812f10fdcbSNeilBrown GFP_KERNEL); 19829abdda5dSChuck Lever target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); 19832f10fdcbSNeilBrown if ((source->cr_principal && !target->cr_principal) || 19849abdda5dSChuck Lever (source->cr_raw_principal && !target->cr_raw_principal) || 19859abdda5dSChuck Lever (source->cr_targ_princ && !target->cr_targ_princ)) 19862f10fdcbSNeilBrown return -ENOMEM; 198750043859SJ. Bruce Fields 1988d5497fc6SJ. Bruce Fields target->cr_flavor = source->cr_flavor; 19891da177e4SLinus Torvalds target->cr_uid = source->cr_uid; 19901da177e4SLinus Torvalds target->cr_gid = source->cr_gid; 19911da177e4SLinus Torvalds target->cr_group_info = source->cr_group_info; 19921da177e4SLinus Torvalds get_group_info(target->cr_group_info); 19930dc1531aSJ. Bruce Fields target->cr_gss_mech = source->cr_gss_mech; 19940dc1531aSJ. Bruce Fields if (source->cr_gss_mech) 19950dc1531aSJ. Bruce Fields gss_mech_get(source->cr_gss_mech); 199603a4e1f6SJ. Bruce Fields return 0; 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 1999ef17af2aSRasmus Villemoes static int 2000ac55fdc4SJeff Layton compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) 2001ac55fdc4SJeff Layton { 2002ef17af2aSRasmus Villemoes if (o1->len < o2->len) 2003ef17af2aSRasmus Villemoes return -1; 2004ef17af2aSRasmus Villemoes if (o1->len > o2->len) 2005ef17af2aSRasmus Villemoes return 1; 2006ef17af2aSRasmus Villemoes return memcmp(o1->data, o2->data, o1->len); 2007ac55fdc4SJeff Layton } 2008ac55fdc4SJeff Layton 200935bba9a3SJ. Bruce Fields static int same_name(const char *n1, const char *n2) 2010599e0a22SJ. Bruce Fields { 2011a55370a3SNeilBrown return 0 == memcmp(n1, n2, HEXDIR_LEN); 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds static int 2015599e0a22SJ. Bruce Fields same_verf(nfs4_verifier *v1, nfs4_verifier *v2) 2016599e0a22SJ. Bruce Fields { 2017599e0a22SJ. Bruce Fields return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 20201da177e4SLinus Torvalds static int 2021599e0a22SJ. Bruce Fields same_clid(clientid_t *cl1, clientid_t *cl2) 2022599e0a22SJ. Bruce Fields { 2023599e0a22SJ. Bruce Fields return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds 20268fbba96eSJ. Bruce Fields static bool groups_equal(struct group_info *g1, struct group_info *g2) 20278fbba96eSJ. Bruce Fields { 20288fbba96eSJ. Bruce Fields int i; 20298fbba96eSJ. Bruce Fields 20308fbba96eSJ. Bruce Fields if (g1->ngroups != g2->ngroups) 20318fbba96eSJ. Bruce Fields return false; 20328fbba96eSJ. Bruce Fields for (i=0; i<g1->ngroups; i++) 203381243eacSAlexey Dobriyan if (!gid_eq(g1->gid[i], g2->gid[i])) 20348fbba96eSJ. Bruce Fields return false; 20358fbba96eSJ. Bruce Fields return true; 20368fbba96eSJ. Bruce Fields } 20378fbba96eSJ. Bruce Fields 203868eb3508SJ. Bruce Fields /* 203968eb3508SJ. Bruce Fields * RFC 3530 language requires clid_inuse be returned when the 204068eb3508SJ. Bruce Fields * "principal" associated with a requests differs from that previously 204168eb3508SJ. Bruce Fields * used. We use uid, gid's, and gss principal string as our best 204268eb3508SJ. Bruce Fields * approximation. We also don't want to allow non-gss use of a client 204368eb3508SJ. Bruce Fields * established using gss: in theory cr_principal should catch that 204468eb3508SJ. Bruce Fields * change, but in practice cr_principal can be null even in the gss case 204568eb3508SJ. Bruce Fields * since gssd doesn't always pass down a principal string. 204668eb3508SJ. Bruce Fields */ 204768eb3508SJ. Bruce Fields static bool is_gss_cred(struct svc_cred *cr) 204868eb3508SJ. Bruce Fields { 204968eb3508SJ. Bruce Fields /* Is cr_flavor one of the gss "pseudoflavors"?: */ 205068eb3508SJ. Bruce Fields return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); 205168eb3508SJ. Bruce Fields } 205268eb3508SJ. Bruce Fields 205368eb3508SJ. Bruce Fields 20545559b50aSVivek Trivedi static bool 2055599e0a22SJ. Bruce Fields same_creds(struct svc_cred *cr1, struct svc_cred *cr2) 2056599e0a22SJ. Bruce Fields { 205768eb3508SJ. Bruce Fields if ((is_gss_cred(cr1) != is_gss_cred(cr2)) 20586fab8779SEric W. Biederman || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) 20596fab8779SEric W. Biederman || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) 20608fbba96eSJ. Bruce Fields || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) 20618fbba96eSJ. Bruce Fields return false; 20629abdda5dSChuck Lever /* XXX: check that cr_targ_princ fields match ? */ 20638fbba96eSJ. Bruce Fields if (cr1->cr_principal == cr2->cr_principal) 20648fbba96eSJ. Bruce Fields return true; 20658fbba96eSJ. Bruce Fields if (!cr1->cr_principal || !cr2->cr_principal) 20668fbba96eSJ. Bruce Fields return false; 20675559b50aSVivek Trivedi return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 207057266a6eSJ. Bruce Fields static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) 207157266a6eSJ. Bruce Fields { 207257266a6eSJ. Bruce Fields struct svc_cred *cr = &rqstp->rq_cred; 207357266a6eSJ. Bruce Fields u32 service; 207457266a6eSJ. Bruce Fields 2075c4720591SJ. Bruce Fields if (!cr->cr_gss_mech) 2076c4720591SJ. Bruce Fields return false; 207757266a6eSJ. Bruce Fields service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); 207857266a6eSJ. Bruce Fields return service == RPC_GSS_SVC_INTEGRITY || 207957266a6eSJ. Bruce Fields service == RPC_GSS_SVC_PRIVACY; 208057266a6eSJ. Bruce Fields } 208157266a6eSJ. Bruce Fields 2082dedeb13fSAndrew Elble bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) 208357266a6eSJ. Bruce Fields { 208457266a6eSJ. Bruce Fields struct svc_cred *cr = &rqstp->rq_cred; 208557266a6eSJ. Bruce Fields 208657266a6eSJ. Bruce Fields if (!cl->cl_mach_cred) 208757266a6eSJ. Bruce Fields return true; 208857266a6eSJ. Bruce Fields if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) 208957266a6eSJ. Bruce Fields return false; 209057266a6eSJ. Bruce Fields if (!svc_rqst_integrity_protected(rqstp)) 209157266a6eSJ. Bruce Fields return false; 2092414ca017SJ. Bruce Fields if (cl->cl_cred.cr_raw_principal) 2093414ca017SJ. Bruce Fields return 0 == strcmp(cl->cl_cred.cr_raw_principal, 2094414ca017SJ. Bruce Fields cr->cr_raw_principal); 209557266a6eSJ. Bruce Fields if (!cr->cr_principal) 209657266a6eSJ. Bruce Fields return false; 209757266a6eSJ. Bruce Fields return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); 209857266a6eSJ. Bruce Fields } 209957266a6eSJ. Bruce Fields 2100294ac32eSJeff Layton static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) 2101deda2faaSJ. Bruce Fields { 2102ab4684d1SChuck Lever __be32 verf[2]; 21031da177e4SLinus Torvalds 2104f419992cSJeff Layton /* 2105f419992cSJeff Layton * This is opaque to client, so no need to byte-swap. Use 2106f419992cSJeff Layton * __force to keep sparse happy 2107f419992cSJeff Layton */ 2108f419992cSJeff Layton verf[0] = (__force __be32)get_seconds(); 210919311aa8SKinglong Mee verf[1] = (__force __be32)nn->clverifier_counter++; 2110ab4684d1SChuck Lever memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds 2113294ac32eSJeff Layton static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) 2114294ac32eSJeff Layton { 2115294ac32eSJeff Layton clp->cl_clientid.cl_boot = nn->boot_time; 2116294ac32eSJeff Layton clp->cl_clientid.cl_id = nn->clientid_counter++; 2117294ac32eSJeff Layton gen_confirm(clp, nn); 2118294ac32eSJeff Layton } 2119294ac32eSJeff Layton 21204770d722SJeff Layton static struct nfs4_stid * 21214770d722SJeff Layton find_stateid_locked(struct nfs4_client *cl, stateid_t *t) 21224581d140SJ. Bruce Fields { 21233abdb607SJ. Bruce Fields struct nfs4_stid *ret; 21243abdb607SJ. Bruce Fields 21253abdb607SJ. Bruce Fields ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); 21263abdb607SJ. Bruce Fields if (!ret || !ret->sc_type) 21273abdb607SJ. Bruce Fields return NULL; 21283abdb607SJ. Bruce Fields return ret; 21294581d140SJ. Bruce Fields } 21304d71ab87SJ. Bruce Fields 21314770d722SJeff Layton static struct nfs4_stid * 21324770d722SJeff Layton find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask) 2133f459e453SJ. Bruce Fields { 2134f459e453SJ. Bruce Fields struct nfs4_stid *s; 2135f459e453SJ. Bruce Fields 21364770d722SJeff Layton spin_lock(&cl->cl_lock); 21374770d722SJeff Layton s = find_stateid_locked(cl, t); 21382d3f9668STrond Myklebust if (s != NULL) { 21392d3f9668STrond Myklebust if (typemask & s->sc_type) 2140a15dfcd5SElena Reshetova refcount_inc(&s->sc_count); 21412d3f9668STrond Myklebust else 21424770d722SJeff Layton s = NULL; 21432d3f9668STrond Myklebust } 21444770d722SJeff Layton spin_unlock(&cl->cl_lock); 21454d71ab87SJ. Bruce Fields return s; 21464581d140SJ. Bruce Fields } 21474581d140SJ. Bruce Fields 21482216d449SJeff Layton static struct nfs4_client *create_client(struct xdr_netobj name, 2149b09333c4SRicardo Labiaga struct svc_rqst *rqstp, nfs4_verifier *verf) 2150b09333c4SRicardo Labiaga { 2151b09333c4SRicardo Labiaga struct nfs4_client *clp; 2152b09333c4SRicardo Labiaga struct sockaddr *sa = svc_addr(rqstp); 215303a4e1f6SJ. Bruce Fields int ret; 2154c212cecfSStanislav Kinsbursky struct net *net = SVC_NET(rqstp); 2155b09333c4SRicardo Labiaga 2156b09333c4SRicardo Labiaga clp = alloc_client(name); 2157b09333c4SRicardo Labiaga if (clp == NULL) 2158b09333c4SRicardo Labiaga return NULL; 2159b09333c4SRicardo Labiaga 216003a4e1f6SJ. Bruce Fields ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); 216103a4e1f6SJ. Bruce Fields if (ret) { 2162b09333c4SRicardo Labiaga free_client(clp); 2163b09333c4SRicardo Labiaga return NULL; 2164b09333c4SRicardo Labiaga } 21650162ac2bSChristoph Hellwig nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); 216607cd4909SBenny Halevy clp->cl_time = get_seconds(); 2167b09333c4SRicardo Labiaga clear_bit(0, &clp->cl_cb_slot_busy); 2168b09333c4SRicardo Labiaga copy_verf(clp, verf); 2169b09333c4SRicardo Labiaga rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); 2170edd76786SJ. Bruce Fields clp->cl_cb_session = NULL; 2171c212cecfSStanislav Kinsbursky clp->net = net; 2172b09333c4SRicardo Labiaga return clp; 2173b09333c4SRicardo Labiaga } 2174b09333c4SRicardo Labiaga 2175fd39ca9aSNeilBrown static void 2176ac55fdc4SJeff Layton add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) 2177ac55fdc4SJeff Layton { 2178ac55fdc4SJeff Layton struct rb_node **new = &(root->rb_node), *parent = NULL; 2179ac55fdc4SJeff Layton struct nfs4_client *clp; 2180ac55fdc4SJeff Layton 2181ac55fdc4SJeff Layton while (*new) { 2182ac55fdc4SJeff Layton clp = rb_entry(*new, struct nfs4_client, cl_namenode); 2183ac55fdc4SJeff Layton parent = *new; 2184ac55fdc4SJeff Layton 2185ac55fdc4SJeff Layton if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) 2186ac55fdc4SJeff Layton new = &((*new)->rb_left); 2187ac55fdc4SJeff Layton else 2188ac55fdc4SJeff Layton new = &((*new)->rb_right); 2189ac55fdc4SJeff Layton } 2190ac55fdc4SJeff Layton 2191ac55fdc4SJeff Layton rb_link_node(&new_clp->cl_namenode, parent, new); 2192ac55fdc4SJeff Layton rb_insert_color(&new_clp->cl_namenode, root); 2193ac55fdc4SJeff Layton } 2194ac55fdc4SJeff Layton 2195ac55fdc4SJeff Layton static struct nfs4_client * 2196ac55fdc4SJeff Layton find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) 2197ac55fdc4SJeff Layton { 2198ef17af2aSRasmus Villemoes int cmp; 2199ac55fdc4SJeff Layton struct rb_node *node = root->rb_node; 2200ac55fdc4SJeff Layton struct nfs4_client *clp; 2201ac55fdc4SJeff Layton 2202ac55fdc4SJeff Layton while (node) { 2203ac55fdc4SJeff Layton clp = rb_entry(node, struct nfs4_client, cl_namenode); 2204ac55fdc4SJeff Layton cmp = compare_blob(&clp->cl_name, name); 2205ac55fdc4SJeff Layton if (cmp > 0) 2206ac55fdc4SJeff Layton node = node->rb_left; 2207ac55fdc4SJeff Layton else if (cmp < 0) 2208ac55fdc4SJeff Layton node = node->rb_right; 2209ac55fdc4SJeff Layton else 2210ac55fdc4SJeff Layton return clp; 2211ac55fdc4SJeff Layton } 2212ac55fdc4SJeff Layton return NULL; 2213ac55fdc4SJeff Layton } 2214ac55fdc4SJeff Layton 2215ac55fdc4SJeff Layton static void 2216ac55fdc4SJeff Layton add_to_unconfirmed(struct nfs4_client *clp) 22171da177e4SLinus Torvalds { 22181da177e4SLinus Torvalds unsigned int idhashval; 22190a7ec377SStanislav Kinsbursky struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 22201da177e4SLinus Torvalds 22210a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 22220a880a28STrond Myklebust 2223ac55fdc4SJeff Layton clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); 2224a99454aaSStanislav Kinsbursky add_clp_to_name_tree(clp, &nn->unconf_name_tree); 22251da177e4SLinus Torvalds idhashval = clientid_hashval(clp->cl_clientid.cl_id); 22260a7ec377SStanislav Kinsbursky list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); 22273dbacee6STrond Myklebust renew_client_locked(clp); 22281da177e4SLinus Torvalds } 22291da177e4SLinus Torvalds 2230fd39ca9aSNeilBrown static void 22311da177e4SLinus Torvalds move_to_confirmed(struct nfs4_client *clp) 22321da177e4SLinus Torvalds { 22331da177e4SLinus Torvalds unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); 22348daae4dcSStanislav Kinsbursky struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 22351da177e4SLinus Torvalds 22360a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 22370a880a28STrond Myklebust 22381da177e4SLinus Torvalds dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); 22398daae4dcSStanislav Kinsbursky list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); 2240a99454aaSStanislav Kinsbursky rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); 2241382a62e7SStanislav Kinsbursky add_clp_to_name_tree(clp, &nn->conf_name_tree); 2242ac55fdc4SJeff Layton set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); 22433dbacee6STrond Myklebust renew_client_locked(clp); 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds static struct nfs4_client * 2247bfa85e83SJ. Bruce Fields find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) 22481da177e4SLinus Torvalds { 22491da177e4SLinus Torvalds struct nfs4_client *clp; 22501da177e4SLinus Torvalds unsigned int idhashval = clientid_hashval(clid->cl_id); 22511da177e4SLinus Torvalds 2252bfa85e83SJ. Bruce Fields list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { 2253a50d2ad1SJ. Bruce Fields if (same_clid(&clp->cl_clientid, clid)) { 2254d15c077eSJ. Bruce Fields if ((bool)clp->cl_minorversion != sessions) 2255d15c077eSJ. Bruce Fields return NULL; 22563dbacee6STrond Myklebust renew_client_locked(clp); 22571da177e4SLinus Torvalds return clp; 22581da177e4SLinus Torvalds } 2259a50d2ad1SJ. Bruce Fields } 22601da177e4SLinus Torvalds return NULL; 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds static struct nfs4_client * 2264bfa85e83SJ. Bruce Fields find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) 2265bfa85e83SJ. Bruce Fields { 2266bfa85e83SJ. Bruce Fields struct list_head *tbl = nn->conf_id_hashtbl; 2267bfa85e83SJ. Bruce Fields 22680a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 2269bfa85e83SJ. Bruce Fields return find_client_in_id_table(tbl, clid, sessions); 2270bfa85e83SJ. Bruce Fields } 2271bfa85e83SJ. Bruce Fields 2272bfa85e83SJ. Bruce Fields static struct nfs4_client * 22730a7ec377SStanislav Kinsbursky find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) 22741da177e4SLinus Torvalds { 2275bfa85e83SJ. Bruce Fields struct list_head *tbl = nn->unconf_id_hashtbl; 22761da177e4SLinus Torvalds 22770a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 2278bfa85e83SJ. Bruce Fields return find_client_in_id_table(tbl, clid, sessions); 22791da177e4SLinus Torvalds } 22801da177e4SLinus Torvalds 22816e5f15c9SJ. Bruce Fields static bool clp_used_exchangeid(struct nfs4_client *clp) 2282a1bcecd2SAndy Adamson { 22836e5f15c9SJ. Bruce Fields return clp->cl_exchange_flags != 0; 2284a1bcecd2SAndy Adamson } 2285a1bcecd2SAndy Adamson 228628ce6054SNeilBrown static struct nfs4_client * 2287382a62e7SStanislav Kinsbursky find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) 228828ce6054SNeilBrown { 22890a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 2290382a62e7SStanislav Kinsbursky return find_clp_in_name_tree(name, &nn->conf_name_tree); 229128ce6054SNeilBrown } 229228ce6054SNeilBrown 229328ce6054SNeilBrown static struct nfs4_client * 2294a99454aaSStanislav Kinsbursky find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) 229528ce6054SNeilBrown { 22960a880a28STrond Myklebust lockdep_assert_held(&nn->client_lock); 2297a99454aaSStanislav Kinsbursky return find_clp_in_name_tree(name, &nn->unconf_name_tree); 229828ce6054SNeilBrown } 229928ce6054SNeilBrown 2300fd39ca9aSNeilBrown static void 23016f3d772fSTakuma Umeya gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) 23021da177e4SLinus Torvalds { 230307263f1eSJ. Bruce Fields struct nfs4_cb_conn *conn = &clp->cl_cb_conn; 23046f3d772fSTakuma Umeya struct sockaddr *sa = svc_addr(rqstp); 23056f3d772fSTakuma Umeya u32 scopeid = rpc_get_scope_id(sa); 23067077ecbaSJeff Layton unsigned short expected_family; 23071da177e4SLinus Torvalds 23087077ecbaSJeff Layton /* Currently, we only support tcp and tcp6 for the callback channel */ 23097077ecbaSJeff Layton if (se->se_callback_netid_len == 3 && 23107077ecbaSJeff Layton !memcmp(se->se_callback_netid_val, "tcp", 3)) 23117077ecbaSJeff Layton expected_family = AF_INET; 23127077ecbaSJeff Layton else if (se->se_callback_netid_len == 4 && 23137077ecbaSJeff Layton !memcmp(se->se_callback_netid_val, "tcp6", 4)) 23147077ecbaSJeff Layton expected_family = AF_INET6; 23157077ecbaSJeff Layton else 23161da177e4SLinus Torvalds goto out_err; 23171da177e4SLinus Torvalds 2318c212cecfSStanislav Kinsbursky conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, 2319aa9a4ec7SJeff Layton se->se_callback_addr_len, 232007263f1eSJ. Bruce Fields (struct sockaddr *)&conn->cb_addr, 232107263f1eSJ. Bruce Fields sizeof(conn->cb_addr)); 2322aa9a4ec7SJeff Layton 232307263f1eSJ. Bruce Fields if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) 23241da177e4SLinus Torvalds goto out_err; 2325aa9a4ec7SJeff Layton 232607263f1eSJ. Bruce Fields if (conn->cb_addr.ss_family == AF_INET6) 232707263f1eSJ. Bruce Fields ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; 2328fbf4665fSJeff Layton 232907263f1eSJ. Bruce Fields conn->cb_prog = se->se_callback_prog; 233007263f1eSJ. Bruce Fields conn->cb_ident = se->se_callback_ident; 2331849a1cf1SMi Jinlong memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); 23321da177e4SLinus Torvalds return; 23331da177e4SLinus Torvalds out_err: 233407263f1eSJ. Bruce Fields conn->cb_addr.ss_family = AF_UNSPEC; 233507263f1eSJ. Bruce Fields conn->cb_addrlen = 0; 23364ab495bfSRasmus Villemoes dprintk("NFSD: this client (clientid %08x/%08x) " 23371da177e4SLinus Torvalds "will not receive delegations\n", 23381da177e4SLinus Torvalds clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds return; 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 2343074fe897SAndy Adamson /* 2344067e1aceSJ. Bruce Fields * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. 2345074fe897SAndy Adamson */ 2346b607664eSTrond Myklebust static void 2347074fe897SAndy Adamson nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) 2348074fe897SAndy Adamson { 2349f5236013SJ. Bruce Fields struct xdr_buf *buf = resp->xdr.buf; 2350557ce264SAndy Adamson struct nfsd4_slot *slot = resp->cstate.slot; 2351557ce264SAndy Adamson unsigned int base; 2352074fe897SAndy Adamson 2353557ce264SAndy Adamson dprintk("--> %s slot %p\n", __func__, slot); 2354074fe897SAndy Adamson 2355085def3aSJ. Bruce Fields slot->sl_flags |= NFSD4_SLOT_INITIALIZED; 2356557ce264SAndy Adamson slot->sl_opcnt = resp->opcnt; 2357557ce264SAndy Adamson slot->sl_status = resp->cstate.status; 235853da6a53SJ. Bruce Fields free_svc_cred(&slot->sl_cred); 235953da6a53SJ. Bruce Fields copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); 2360bf864a31SAndy Adamson 2361085def3aSJ. Bruce Fields if (!nfsd4_cache_this(resp)) { 2362085def3aSJ. Bruce Fields slot->sl_flags &= ~NFSD4_SLOT_CACHED; 2363bf864a31SAndy Adamson return; 2364bf864a31SAndy Adamson } 2365085def3aSJ. Bruce Fields slot->sl_flags |= NFSD4_SLOT_CACHED; 2366085def3aSJ. Bruce Fields 2367f5236013SJ. Bruce Fields base = resp->cstate.data_offset; 2368f5236013SJ. Bruce Fields slot->sl_datalen = buf->len - base; 2369f5236013SJ. Bruce Fields if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) 2370d3f03403SDan Carpenter WARN(1, "%s: sessions DRC could not cache compound\n", 2371d3f03403SDan Carpenter __func__); 2372557ce264SAndy Adamson return; 2373074fe897SAndy Adamson } 2374074fe897SAndy Adamson 2375074fe897SAndy Adamson /* 2376abfabf8cSAndy Adamson * Encode the replay sequence operation from the slot values. 2377abfabf8cSAndy Adamson * If cachethis is FALSE encode the uncached rep error on the next 2378abfabf8cSAndy Adamson * operation which sets resp->p and increments resp->opcnt for 2379abfabf8cSAndy Adamson * nfs4svc_encode_compoundres. 2380abfabf8cSAndy Adamson * 2381074fe897SAndy Adamson */ 2382abfabf8cSAndy Adamson static __be32 2383abfabf8cSAndy Adamson nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, 2384abfabf8cSAndy Adamson struct nfsd4_compoundres *resp) 2385074fe897SAndy Adamson { 2386abfabf8cSAndy Adamson struct nfsd4_op *op; 2387abfabf8cSAndy Adamson struct nfsd4_slot *slot = resp->cstate.slot; 2388074fe897SAndy Adamson 2389abfabf8cSAndy Adamson /* Encode the replayed sequence operation */ 2390abfabf8cSAndy Adamson op = &args->ops[resp->opcnt - 1]; 2391abfabf8cSAndy Adamson nfsd4_encode_operation(resp, op); 2392abfabf8cSAndy Adamson 2393085def3aSJ. Bruce Fields if (slot->sl_flags & NFSD4_SLOT_CACHED) 2394085def3aSJ. Bruce Fields return op->status; 2395085def3aSJ. Bruce Fields if (args->opcnt == 1) { 2396085def3aSJ. Bruce Fields /* 2397085def3aSJ. Bruce Fields * The original operation wasn't a solo sequence--we 2398085def3aSJ. Bruce Fields * always cache those--so this retry must not match the 2399085def3aSJ. Bruce Fields * original: 2400085def3aSJ. Bruce Fields */ 2401085def3aSJ. Bruce Fields op->status = nfserr_seq_false_retry; 2402085def3aSJ. Bruce Fields } else { 2403abfabf8cSAndy Adamson op = &args->ops[resp->opcnt++]; 2404abfabf8cSAndy Adamson op->status = nfserr_retry_uncached_rep; 2405abfabf8cSAndy Adamson nfsd4_encode_operation(resp, op); 2406074fe897SAndy Adamson } 2407abfabf8cSAndy Adamson return op->status; 2408074fe897SAndy Adamson } 2409074fe897SAndy Adamson 2410074fe897SAndy Adamson /* 2411557ce264SAndy Adamson * The sequence operation is not cached because we can use the slot and 2412557ce264SAndy Adamson * session values. 2413074fe897SAndy Adamson */ 24143ca2eb98SJ. Bruce Fields static __be32 2415bf864a31SAndy Adamson nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, 2416bf864a31SAndy Adamson struct nfsd4_sequence *seq) 2417074fe897SAndy Adamson { 2418557ce264SAndy Adamson struct nfsd4_slot *slot = resp->cstate.slot; 2419f5236013SJ. Bruce Fields struct xdr_stream *xdr = &resp->xdr; 2420f5236013SJ. Bruce Fields __be32 *p; 2421074fe897SAndy Adamson __be32 status; 2422074fe897SAndy Adamson 2423557ce264SAndy Adamson dprintk("--> %s slot %p\n", __func__, slot); 2424074fe897SAndy Adamson 2425abfabf8cSAndy Adamson status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); 24260da7b19cSJ. Bruce Fields if (status) 2427abfabf8cSAndy Adamson return status; 2428074fe897SAndy Adamson 2429f5236013SJ. Bruce Fields p = xdr_reserve_space(xdr, slot->sl_datalen); 2430f5236013SJ. Bruce Fields if (!p) { 2431f5236013SJ. Bruce Fields WARN_ON_ONCE(1); 2432f5236013SJ. Bruce Fields return nfserr_serverfault; 2433f5236013SJ. Bruce Fields } 2434f5236013SJ. Bruce Fields xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); 2435f5236013SJ. Bruce Fields xdr_commit_encode(xdr); 2436074fe897SAndy Adamson 2437557ce264SAndy Adamson resp->opcnt = slot->sl_opcnt; 2438f5236013SJ. Bruce Fields return slot->sl_status; 2439074fe897SAndy Adamson } 2440074fe897SAndy Adamson 24410733d213SAndy Adamson /* 24420733d213SAndy Adamson * Set the exchange_id flags returned by the server. 24430733d213SAndy Adamson */ 24440733d213SAndy Adamson static void 24450733d213SAndy Adamson nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) 24460733d213SAndy Adamson { 24479cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS 24489cf514ccSChristoph Hellwig new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; 24499cf514ccSChristoph Hellwig #else 24500733d213SAndy Adamson new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; 24519cf514ccSChristoph Hellwig #endif 24520733d213SAndy Adamson 24530733d213SAndy Adamson /* Referrals are supported, Migration is not. */ 24540733d213SAndy Adamson new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; 24550733d213SAndy Adamson 24560733d213SAndy Adamson /* set the wire flags to return to client. */ 24570733d213SAndy Adamson clid->flags = new->cl_exchange_flags; 24580733d213SAndy Adamson } 24590733d213SAndy Adamson 24604eaea134SJ. Bruce Fields static bool client_has_openowners(struct nfs4_client *clp) 24614eaea134SJ. Bruce Fields { 24624eaea134SJ. Bruce Fields struct nfs4_openowner *oo; 24634eaea134SJ. Bruce Fields 24644eaea134SJ. Bruce Fields list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { 24654eaea134SJ. Bruce Fields if (!list_empty(&oo->oo_owner.so_stateids)) 24664eaea134SJ. Bruce Fields return true; 24674eaea134SJ. Bruce Fields } 24684eaea134SJ. Bruce Fields return false; 24694eaea134SJ. Bruce Fields } 24704eaea134SJ. Bruce Fields 2471631fc9eaSJ. Bruce Fields static bool client_has_state(struct nfs4_client *clp) 2472631fc9eaSJ. Bruce Fields { 24734eaea134SJ. Bruce Fields return client_has_openowners(clp) 247447e970beSKinglong Mee #ifdef CONFIG_NFSD_PNFS 247547e970beSKinglong Mee || !list_empty(&clp->cl_lo_states) 247647e970beSKinglong Mee #endif 24776eccece9SJ. Bruce Fields || !list_empty(&clp->cl_delegations) 24786eccece9SJ. Bruce Fields || !list_empty(&clp->cl_sessions); 2479631fc9eaSJ. Bruce Fields } 2480631fc9eaSJ. Bruce Fields 2481b37ad28bSAl Viro __be32 2482eb69853dSChristoph Hellwig nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 2483eb69853dSChristoph Hellwig union nfsd4_op_u *u) 2484069b6ad4SAndy Adamson { 2485eb69853dSChristoph Hellwig struct nfsd4_exchange_id *exid = &u->exchange_id; 24863dbacee6STrond Myklebust struct nfs4_client *conf, *new; 24873dbacee6STrond Myklebust struct nfs4_client *unconf = NULL; 248857b7b43bSJ. Bruce Fields __be32 status; 2489363168b4SJeff Layton char addr_str[INET6_ADDRSTRLEN]; 24900733d213SAndy Adamson nfs4_verifier verf = exid->verifier; 2491363168b4SJeff Layton struct sockaddr *sa = svc_addr(rqstp); 249283e08fd4SJ. Bruce Fields bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; 2493c212cecfSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 24940733d213SAndy Adamson 2495363168b4SJeff Layton rpc_ntop(sa, addr_str, sizeof(addr_str)); 24960733d213SAndy Adamson dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " 2497363168b4SJeff Layton "ip_addr=%s flags %x, spa_how %d\n", 24980733d213SAndy Adamson __func__, rqstp, exid, exid->clname.len, exid->clname.data, 2499363168b4SJeff Layton addr_str, exid->flags, exid->spa_how); 25000733d213SAndy Adamson 2501a084daf5SJ. Bruce Fields if (exid->flags & ~EXCHGID4_FLAG_MASK_A) 25020733d213SAndy Adamson return nfserr_inval; 25030733d213SAndy Adamson 250450c7b948SJ. Bruce Fields new = create_client(exid->clname, rqstp, &verf); 250550c7b948SJ. Bruce Fields if (new == NULL) 250650c7b948SJ. Bruce Fields return nfserr_jukebox; 250750c7b948SJ. Bruce Fields 25080733d213SAndy Adamson switch (exid->spa_how) { 250957266a6eSJ. Bruce Fields case SP4_MACH_CRED: 2510ed941643SAndrew Elble exid->spo_must_enforce[0] = 0; 2511ed941643SAndrew Elble exid->spo_must_enforce[1] = ( 2512ed941643SAndrew Elble 1 << (OP_BIND_CONN_TO_SESSION - 32) | 2513ed941643SAndrew Elble 1 << (OP_EXCHANGE_ID - 32) | 2514ed941643SAndrew Elble 1 << (OP_CREATE_SESSION - 32) | 2515ed941643SAndrew Elble 1 << (OP_DESTROY_SESSION - 32) | 2516ed941643SAndrew Elble 1 << (OP_DESTROY_CLIENTID - 32)); 2517ed941643SAndrew Elble 2518ed941643SAndrew Elble exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | 2519ed941643SAndrew Elble 1 << (OP_OPEN_DOWNGRADE) | 2520ed941643SAndrew Elble 1 << (OP_LOCKU) | 2521ed941643SAndrew Elble 1 << (OP_DELEGRETURN)); 2522ed941643SAndrew Elble 2523ed941643SAndrew Elble exid->spo_must_allow[1] &= ( 2524ed941643SAndrew Elble 1 << (OP_TEST_STATEID - 32) | 2525ed941643SAndrew Elble 1 << (OP_FREE_STATEID - 32)); 252650c7b948SJ. Bruce Fields if (!svc_rqst_integrity_protected(rqstp)) { 252750c7b948SJ. Bruce Fields status = nfserr_inval; 252850c7b948SJ. Bruce Fields goto out_nolock; 252950c7b948SJ. Bruce Fields } 2530920dd9bbSJ. Bruce Fields /* 2531920dd9bbSJ. Bruce Fields * Sometimes userspace doesn't give us a principal. 2532920dd9bbSJ. Bruce Fields * Which is a bug, really. Anyway, we can't enforce 2533920dd9bbSJ. Bruce Fields * MACH_CRED in that case, better to give up now: 2534920dd9bbSJ. Bruce Fields */ 2535414ca017SJ. Bruce Fields if (!new->cl_cred.cr_principal && 2536414ca017SJ. Bruce Fields !new->cl_cred.cr_raw_principal) { 2537920dd9bbSJ. Bruce Fields status = nfserr_serverfault; 2538920dd9bbSJ. Bruce Fields goto out_nolock; 2539920dd9bbSJ. Bruce Fields } 254050c7b948SJ. Bruce Fields new->cl_mach_cred = true; 25410733d213SAndy Adamson case SP4_NONE: 25420733d213SAndy Adamson break; 2543063b0fb9SJ. Bruce Fields default: /* checked by xdr code */ 2544063b0fb9SJ. Bruce Fields WARN_ON_ONCE(1); 25450733d213SAndy Adamson case SP4_SSV: 25468edf4b02SKinglong Mee status = nfserr_encr_alg_unsupp; 25478edf4b02SKinglong Mee goto out_nolock; 25480733d213SAndy Adamson } 25490733d213SAndy Adamson 25502dbb269dSJ. Bruce Fields /* Cases below refer to rfc 5661 section 18.35.4: */ 25513dbacee6STrond Myklebust spin_lock(&nn->client_lock); 2552382a62e7SStanislav Kinsbursky conf = find_confirmed_client_by_name(&exid->clname, nn); 25530733d213SAndy Adamson if (conf) { 255483e08fd4SJ. Bruce Fields bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); 255583e08fd4SJ. Bruce Fields bool verfs_match = same_verf(&verf, &conf->cl_verifier); 255683e08fd4SJ. Bruce Fields 2557136e658dSJ. Bruce Fields if (update) { 2558136e658dSJ. Bruce Fields if (!clp_used_exchangeid(conf)) { /* buggy client */ 25592dbb269dSJ. Bruce Fields status = nfserr_inval; 2560e203d506SJ. Bruce Fields goto out; 2561e203d506SJ. Bruce Fields } 2562dedeb13fSAndrew Elble if (!nfsd4_mach_creds_match(conf, rqstp)) { 256357266a6eSJ. Bruce Fields status = nfserr_wrong_cred; 256457266a6eSJ. Bruce Fields goto out; 256557266a6eSJ. Bruce Fields } 25662dbb269dSJ. Bruce Fields if (!creds_match) { /* case 9 */ 25670733d213SAndy Adamson status = nfserr_perm; 25680733d213SAndy Adamson goto out; 25690733d213SAndy Adamson } 25702dbb269dSJ. Bruce Fields if (!verfs_match) { /* case 8 */ 25710733d213SAndy Adamson status = nfserr_not_same; 25720733d213SAndy Adamson goto out; 25730733d213SAndy Adamson } 2574136e658dSJ. Bruce Fields /* case 6 */ 25750733d213SAndy Adamson exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; 25760733d213SAndy Adamson goto out_copy; 25776ddbbbfeSMike Sager } 2578136e658dSJ. Bruce Fields if (!creds_match) { /* case 3 */ 2579631fc9eaSJ. Bruce Fields if (client_has_state(conf)) { 2580136e658dSJ. Bruce Fields status = nfserr_clid_inuse; 2581136e658dSJ. Bruce Fields goto out; 2582136e658dSJ. Bruce Fields } 2583b9831b59SJ. Bruce Fields goto out_new; 2584631fc9eaSJ. Bruce Fields } 2585136e658dSJ. Bruce Fields if (verfs_match) { /* case 2 */ 25860f1ba0efSJ. Bruce Fields conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; 2587136e658dSJ. Bruce Fields goto out_copy; 2588136e658dSJ. Bruce Fields } 25892dbb269dSJ. Bruce Fields /* case 5, client reboot */ 25903dbacee6STrond Myklebust conf = NULL; 25910733d213SAndy Adamson goto out_new; 25920733d213SAndy Adamson } 25936ddbbbfeSMike Sager 25942dbb269dSJ. Bruce Fields if (update) { /* case 7 */ 25950733d213SAndy Adamson status = nfserr_noent; 25960733d213SAndy Adamson goto out; 25970733d213SAndy Adamson } 25980733d213SAndy Adamson 2599a99454aaSStanislav Kinsbursky unconf = find_unconfirmed_client_by_name(&exid->clname, nn); 26002dbb269dSJ. Bruce Fields if (unconf) /* case 4, possible retry or client restart */ 26013dbacee6STrond Myklebust unhash_client_locked(unconf); 26020733d213SAndy Adamson 26032dbb269dSJ. Bruce Fields /* case 1 (normal case) */ 26040733d213SAndy Adamson out_new: 2605fd699b8aSJeff Layton if (conf) { 2606fd699b8aSJeff Layton status = mark_client_expired_locked(conf); 2607fd699b8aSJeff Layton if (status) 2608fd699b8aSJeff Layton goto out; 2609fd699b8aSJeff Layton } 26104f540e29SJ. Bruce Fields new->cl_minorversion = cstate->minorversion; 2611ed941643SAndrew Elble new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; 2612ed941643SAndrew Elble new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; 26130733d213SAndy Adamson 2614c212cecfSStanislav Kinsbursky gen_clid(new, nn); 2615ac55fdc4SJeff Layton add_to_unconfirmed(new); 26163dbacee6STrond Myklebust swap(new, conf); 26170733d213SAndy Adamson out_copy: 26185cc40fd7STrond Myklebust exid->clientid.cl_boot = conf->cl_clientid.cl_boot; 26195cc40fd7STrond Myklebust exid->clientid.cl_id = conf->cl_clientid.cl_id; 26200733d213SAndy Adamson 26215cc40fd7STrond Myklebust exid->seqid = conf->cl_cs_slot.sl_seqid + 1; 26225cc40fd7STrond Myklebust nfsd4_set_ex_flags(conf, exid); 26230733d213SAndy Adamson 26240733d213SAndy Adamson dprintk("nfsd4_exchange_id seqid %d flags %x\n", 26255cc40fd7STrond Myklebust conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); 26260733d213SAndy Adamson status = nfs_ok; 26270733d213SAndy Adamson 26280733d213SAndy Adamson out: 26293dbacee6STrond Myklebust spin_unlock(&nn->client_lock); 263050c7b948SJ. Bruce Fields out_nolock: 26315cc40fd7STrond Myklebust if (new) 26323dbacee6STrond Myklebust expire_client(new); 26333dbacee6STrond Myklebust if (unconf) 26343dbacee6STrond Myklebust expire_client(unconf); 26350733d213SAndy Adamson return status; 2636069b6ad4SAndy Adamson } 2637069b6ad4SAndy Adamson 263857b7b43bSJ. Bruce Fields static __be32 263988e588d5SAndy Adamson check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) 2640b85d4c01SBenny Halevy { 264188e588d5SAndy Adamson dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, 264288e588d5SAndy Adamson slot_seqid); 2643b85d4c01SBenny Halevy 2644b85d4c01SBenny Halevy /* The slot is in use, and no response has been sent. */ 264588e588d5SAndy Adamson if (slot_inuse) { 264688e588d5SAndy Adamson if (seqid == slot_seqid) 2647b85d4c01SBenny Halevy return nfserr_jukebox; 2648b85d4c01SBenny Halevy else 2649b85d4c01SBenny Halevy return nfserr_seq_misordered; 2650b85d4c01SBenny Halevy } 2651f6d82485SJ. Bruce Fields /* Note unsigned 32-bit arithmetic handles wraparound: */ 265288e588d5SAndy Adamson if (likely(seqid == slot_seqid + 1)) 2653b85d4c01SBenny Halevy return nfs_ok; 265488e588d5SAndy Adamson if (seqid == slot_seqid) 2655b85d4c01SBenny Halevy return nfserr_replay_cache; 2656b85d4c01SBenny Halevy return nfserr_seq_misordered; 2657b85d4c01SBenny Halevy } 2658b85d4c01SBenny Halevy 265949557cc7SAndy Adamson /* 266049557cc7SAndy Adamson * Cache the create session result into the create session single DRC 266149557cc7SAndy Adamson * slot cache by saving the xdr structure. sl_seqid has been set. 266249557cc7SAndy Adamson * Do this for solo or embedded create session operations. 266349557cc7SAndy Adamson */ 266449557cc7SAndy Adamson static void 266549557cc7SAndy Adamson nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, 266657b7b43bSJ. Bruce Fields struct nfsd4_clid_slot *slot, __be32 nfserr) 266749557cc7SAndy Adamson { 266849557cc7SAndy Adamson slot->sl_status = nfserr; 266949557cc7SAndy Adamson memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); 267049557cc7SAndy Adamson } 267149557cc7SAndy Adamson 267249557cc7SAndy Adamson static __be32 267349557cc7SAndy Adamson nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, 267449557cc7SAndy Adamson struct nfsd4_clid_slot *slot) 267549557cc7SAndy Adamson { 267649557cc7SAndy Adamson memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); 267749557cc7SAndy Adamson return slot->sl_status; 267849557cc7SAndy Adamson } 267949557cc7SAndy Adamson 26801b74c25bSMi Jinlong #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ 26811b74c25bSMi Jinlong 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ 26821b74c25bSMi Jinlong 1 + /* MIN tag is length with zero, only length */ \ 26831b74c25bSMi Jinlong 3 + /* version, opcount, opcode */ \ 26841b74c25bSMi Jinlong XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ 26851b74c25bSMi Jinlong /* seqid, slotID, slotID, cache */ \ 26861b74c25bSMi Jinlong 4 ) * sizeof(__be32)) 26871b74c25bSMi Jinlong 26881b74c25bSMi Jinlong #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ 26891b74c25bSMi Jinlong 2 + /* verifier: AUTH_NULL, length 0 */\ 26901b74c25bSMi Jinlong 1 + /* status */ \ 26911b74c25bSMi Jinlong 1 + /* MIN tag is length with zero, only length */ \ 26921b74c25bSMi Jinlong 3 + /* opcount, opcode, opstatus*/ \ 26931b74c25bSMi Jinlong XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ 26941b74c25bSMi Jinlong /* seqid, slotID, slotID, slotID, status */ \ 26951b74c25bSMi Jinlong 5 ) * sizeof(__be32)) 26961b74c25bSMi Jinlong 269755c760cfSJ. Bruce Fields static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) 26981b74c25bSMi Jinlong { 269955c760cfSJ. Bruce Fields u32 maxrpc = nn->nfsd_serv->sv_max_mesg; 270055c760cfSJ. Bruce Fields 2701373cd409SJ. Bruce Fields if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) 2702373cd409SJ. Bruce Fields return nfserr_toosmall; 2703373cd409SJ. Bruce Fields if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) 2704373cd409SJ. Bruce Fields return nfserr_toosmall; 270555c760cfSJ. Bruce Fields ca->headerpadsz = 0; 270655c760cfSJ. Bruce Fields ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); 270755c760cfSJ. Bruce Fields ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); 270855c760cfSJ. Bruce Fields ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); 270955c760cfSJ. Bruce Fields ca->maxresp_cached = min_t(u32, ca->maxresp_cached, 271055c760cfSJ. Bruce Fields NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); 271155c760cfSJ. Bruce Fields ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); 271255c760cfSJ. Bruce Fields /* 271355c760cfSJ. Bruce Fields * Note decreasing slot size below client's request may make it 271455c760cfSJ. Bruce Fields * difficult for client to function correctly, whereas 271555c760cfSJ. Bruce Fields * decreasing the number of slots will (just?) affect 271655c760cfSJ. Bruce Fields * performance. When short on memory we therefore prefer to 271755c760cfSJ. Bruce Fields * decrease number of slots instead of their size. Clients that 271855c760cfSJ. Bruce Fields * request larger slots than they need will get poor results: 271955c760cfSJ. Bruce Fields */ 272055c760cfSJ. Bruce Fields ca->maxreqs = nfsd4_get_drc_mem(ca); 272155c760cfSJ. Bruce Fields if (!ca->maxreqs) 272255c760cfSJ. Bruce Fields return nfserr_jukebox; 272355c760cfSJ. Bruce Fields 2724373cd409SJ. Bruce Fields return nfs_ok; 27251b74c25bSMi Jinlong } 27261b74c25bSMi Jinlong 27274500632fSChuck Lever /* 27284500632fSChuck Lever * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. 27294500632fSChuck Lever * These are based on similar macros in linux/sunrpc/msg_prot.h . 27304500632fSChuck Lever */ 27314500632fSChuck Lever #define RPC_MAX_HEADER_WITH_AUTH_SYS \ 27324500632fSChuck Lever (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) 27334500632fSChuck Lever 27344500632fSChuck Lever #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ 27354500632fSChuck Lever (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) 27364500632fSChuck Lever 27378a891633SKinglong Mee #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ 27384500632fSChuck Lever RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) 27398a891633SKinglong Mee #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ 27404500632fSChuck Lever RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ 27414500632fSChuck Lever sizeof(__be32)) 27428a891633SKinglong Mee 274306b332a5SJ. Bruce Fields static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) 274406b332a5SJ. Bruce Fields { 274506b332a5SJ. Bruce Fields ca->headerpadsz = 0; 274606b332a5SJ. Bruce Fields 27478a891633SKinglong Mee if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) 274806b332a5SJ. Bruce Fields return nfserr_toosmall; 27498a891633SKinglong Mee if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) 275006b332a5SJ. Bruce Fields return nfserr_toosmall; 275106b332a5SJ. Bruce Fields ca->maxresp_cached = 0; 275206b332a5SJ. Bruce Fields if (ca->maxops < 2) 275306b332a5SJ. Bruce Fields return nfserr_toosmall; 275406b332a5SJ. Bruce Fields 275506b332a5SJ. Bruce Fields return nfs_ok; 2756069b6ad4SAndy Adamson } 2757069b6ad4SAndy Adamson 2758b78724b7SJ. Bruce Fields static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) 2759b78724b7SJ. Bruce Fields { 2760b78724b7SJ. Bruce Fields switch (cbs->flavor) { 2761b78724b7SJ. Bruce Fields case RPC_AUTH_NULL: 2762b78724b7SJ. Bruce Fields case RPC_AUTH_UNIX: 2763b78724b7SJ. Bruce Fields return nfs_ok; 2764b78724b7SJ. Bruce Fields default: 2765b78724b7SJ. Bruce Fields /* 2766b78724b7SJ. Bruce Fields * GSS case: the spec doesn't allow us to return this 2767b78724b7SJ. Bruce Fields * error. But it also doesn't allow us not to support 2768b78724b7SJ. Bruce Fields * GSS. 2769b78724b7SJ. Bruce Fields * I'd rather this fail hard than return some error the 2770b78724b7SJ. Bruce Fields * client might think it can already handle: 2771b78724b7SJ. Bruce Fields */ 2772b78724b7SJ. Bruce Fields return nfserr_encr_alg_unsupp; 2773b78724b7SJ. Bruce Fields } 2774b78724b7SJ. Bruce Fields } 2775b78724b7SJ. Bruce Fields 2776069b6ad4SAndy Adamson __be32 2777069b6ad4SAndy Adamson nfsd4_create_session(struct svc_rqst *rqstp, 2778eb69853dSChristoph Hellwig struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 2779069b6ad4SAndy Adamson { 2780eb69853dSChristoph Hellwig struct nfsd4_create_session *cr_ses = &u->create_session; 2781363168b4SJeff Layton struct sockaddr *sa = svc_addr(rqstp); 2782ec6b5d7bSAndy Adamson struct nfs4_client *conf, *unconf; 2783d20c11d8SJeff Layton struct nfs4_client *old = NULL; 2784ac7c46f2SJ. Bruce Fields struct nfsd4_session *new; 278581f0b2a4SJ. Bruce Fields struct nfsd4_conn *conn; 278649557cc7SAndy Adamson struct nfsd4_clid_slot *cs_slot = NULL; 278757b7b43bSJ. Bruce Fields __be32 status = 0; 27888daae4dcSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 2789ec6b5d7bSAndy Adamson 2790a62573dcSMi Jinlong if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) 2791a62573dcSMi Jinlong return nfserr_inval; 2792b78724b7SJ. Bruce Fields status = nfsd4_check_cb_sec(&cr_ses->cb_sec); 2793b78724b7SJ. Bruce Fields if (status) 2794b78724b7SJ. Bruce Fields return status; 279555c760cfSJ. Bruce Fields status = check_forechannel_attrs(&cr_ses->fore_channel, nn); 2796373cd409SJ. Bruce Fields if (status) 2797373cd409SJ. Bruce Fields return status; 279806b332a5SJ. Bruce Fields status = check_backchannel_attrs(&cr_ses->back_channel); 279906b332a5SJ. Bruce Fields if (status) 2800f403e450SKinglong Mee goto out_release_drc_mem; 280181f0b2a4SJ. Bruce Fields status = nfserr_jukebox; 280260810e54SKinglong Mee new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); 280355c760cfSJ. Bruce Fields if (!new) 280455c760cfSJ. Bruce Fields goto out_release_drc_mem; 280581f0b2a4SJ. Bruce Fields conn = alloc_conn_from_crses(rqstp, cr_ses); 280681f0b2a4SJ. Bruce Fields if (!conn) 280781f0b2a4SJ. Bruce Fields goto out_free_session; 2808a62573dcSMi Jinlong 2809d20c11d8SJeff Layton spin_lock(&nn->client_lock); 28100a7ec377SStanislav Kinsbursky unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); 28118daae4dcSStanislav Kinsbursky conf = find_confirmed_client(&cr_ses->clientid, true, nn); 281278389046SJ. Bruce Fields WARN_ON_ONCE(conf && unconf); 2813ec6b5d7bSAndy Adamson 2814ec6b5d7bSAndy Adamson if (conf) { 281557266a6eSJ. Bruce Fields status = nfserr_wrong_cred; 2816dedeb13fSAndrew Elble if (!nfsd4_mach_creds_match(conf, rqstp)) 281757266a6eSJ. Bruce Fields goto out_free_conn; 281849557cc7SAndy Adamson cs_slot = &conf->cl_cs_slot; 281949557cc7SAndy Adamson status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); 2820f5e22bb6SKinglong Mee if (status) { 2821f5e22bb6SKinglong Mee if (status == nfserr_replay_cache) 282249557cc7SAndy Adamson status = nfsd4_replay_create_session(cr_ses, cs_slot); 282381f0b2a4SJ. Bruce Fields goto out_free_conn; 2824ec6b5d7bSAndy Adamson } 2825ec6b5d7bSAndy Adamson } else if (unconf) { 2826ec6b5d7bSAndy Adamson if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || 2827363168b4SJeff Layton !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { 2828ec6b5d7bSAndy Adamson status = nfserr_clid_inuse; 282981f0b2a4SJ. Bruce Fields goto out_free_conn; 2830ec6b5d7bSAndy Adamson } 283157266a6eSJ. Bruce Fields status = nfserr_wrong_cred; 2832dedeb13fSAndrew Elble if (!nfsd4_mach_creds_match(unconf, rqstp)) 283357266a6eSJ. Bruce Fields goto out_free_conn; 283449557cc7SAndy Adamson cs_slot = &unconf->cl_cs_slot; 283549557cc7SAndy Adamson status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); 283638eb76a5SAndy Adamson if (status) { 283738eb76a5SAndy Adamson /* an unconfirmed replay returns misordered */ 2838ec6b5d7bSAndy Adamson status = nfserr_seq_misordered; 283981f0b2a4SJ. Bruce Fields goto out_free_conn; 2840ec6b5d7bSAndy Adamson } 2841382a62e7SStanislav Kinsbursky old = find_confirmed_client_by_name(&unconf->cl_name, nn); 2842221a6876SJ. Bruce Fields if (old) { 2843d20c11d8SJeff Layton status = mark_client_expired_locked(old); 28447abea1e8SJeff Layton if (status) { 28457abea1e8SJeff Layton old = NULL; 2846221a6876SJ. Bruce Fields goto out_free_conn; 2847221a6876SJ. Bruce Fields } 28487abea1e8SJeff Layton } 28498f9d3d3bSJ. Bruce Fields move_to_confirmed(unconf); 2850ec6b5d7bSAndy Adamson conf = unconf; 2851ec6b5d7bSAndy Adamson } else { 2852ec6b5d7bSAndy Adamson status = nfserr_stale_clientid; 285381f0b2a4SJ. Bruce Fields goto out_free_conn; 2854ec6b5d7bSAndy Adamson } 285581f0b2a4SJ. Bruce Fields status = nfs_ok; 28564ce85c8cSChuck Lever /* Persistent sessions are not supported */ 2857408b79bcSJ. Bruce Fields cr_ses->flags &= ~SESSION4_PERSIST; 28584ce85c8cSChuck Lever /* Upshifting from TCP to RDMA is not supported */ 2859408b79bcSJ. Bruce Fields cr_ses->flags &= ~SESSION4_RDMA; 2860408b79bcSJ. Bruce Fields 286181f0b2a4SJ. Bruce Fields init_session(rqstp, new, conf, cr_ses); 2862d20c11d8SJeff Layton nfsd4_get_session_locked(new); 286381f0b2a4SJ. Bruce Fields 2864ac7c46f2SJ. Bruce Fields memcpy(cr_ses->sessionid.data, new->se_sessionid.data, 2865ec6b5d7bSAndy Adamson NFS4_MAX_SESSIONID_LEN); 286686c3e16cSJ. Bruce Fields cs_slot->sl_seqid++; 286749557cc7SAndy Adamson cr_ses->seqid = cs_slot->sl_seqid; 2868ec6b5d7bSAndy Adamson 2869d20c11d8SJeff Layton /* cache solo and embedded create sessions under the client_lock */ 287049557cc7SAndy Adamson nfsd4_cache_create_session(cr_ses, cs_slot, status); 2871d20c11d8SJeff Layton spin_unlock(&nn->client_lock); 2872d20c11d8SJeff Layton /* init connection and backchannel */ 2873d20c11d8SJeff Layton nfsd4_init_conn(rqstp, conn, new); 2874d20c11d8SJeff Layton nfsd4_put_session(new); 2875d20c11d8SJeff Layton if (old) 2876d20c11d8SJeff Layton expire_client(old); 2877ec6b5d7bSAndy Adamson return status; 287881f0b2a4SJ. Bruce Fields out_free_conn: 2879d20c11d8SJeff Layton spin_unlock(&nn->client_lock); 288081f0b2a4SJ. Bruce Fields free_conn(conn); 2881d20c11d8SJeff Layton if (old) 2882d20c11d8SJeff Layton expire_client(old); 288381f0b2a4SJ. Bruce Fields out_free_session: 288481f0b2a4SJ. Bruce Fields __free_session(new); 288555c760cfSJ. Bruce Fields out_release_drc_mem: 288655c760cfSJ. Bruce Fields nfsd4_put_drc_mem(&cr_ses->fore_channel); 28871ca50792SJ. Bruce Fields return status; 2888069b6ad4SAndy Adamson } 2889069b6ad4SAndy Adamson 28901d1bc8f2SJ. Bruce Fields static __be32 nfsd4_map_bcts_dir(u32 *dir) 28911d1bc8f2SJ. Bruce Fields { 28921d1bc8f2SJ. Bruce Fields switch (*dir) { 28931d1bc8f2SJ. Bruce Fields case NFS4_CDFC4_FORE: 28941d1bc8f2SJ. Bruce Fields case NFS4_CDFC4_BACK: 28951d1bc8f2SJ. Bruce Fields return nfs_ok; 28961d1bc8f2SJ. Bruce Fields case NFS4_CDFC4_FORE_OR_BOTH: 28971d1bc8f2SJ. Bruce Fields case NFS4_CDFC4_BACK_OR_BOTH: 28981d1bc8f2SJ. Bruce Fields *dir = NFS4_CDFC4_BOTH; 28991d1bc8f2SJ. Bruce Fields return nfs_ok; 29001d1bc8f2SJ. Bruce Fields }; 29011d1bc8f2SJ. Bruce Fields return nfserr_inval; 29021d1bc8f2SJ. Bruce Fields } 29031d1bc8f2SJ. Bruce Fields 2904eb69853dSChristoph Hellwig __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, 2905eb69853dSChristoph Hellwig struct nfsd4_compound_state *cstate, 2906eb69853dSChristoph Hellwig union nfsd4_op_u *u) 2907cb73a9f4SJ. Bruce Fields { 2908eb69853dSChristoph Hellwig struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; 2909cb73a9f4SJ. Bruce Fields struct nfsd4_session *session = cstate->session; 2910c9a49628SStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 2911b78724b7SJ. Bruce Fields __be32 status; 2912cb73a9f4SJ. Bruce Fields 2913b78724b7SJ. Bruce Fields status = nfsd4_check_cb_sec(&bc->bc_cb_sec); 2914b78724b7SJ. Bruce Fields if (status) 2915b78724b7SJ. Bruce Fields return status; 2916c9a49628SStanislav Kinsbursky spin_lock(&nn->client_lock); 2917cb73a9f4SJ. Bruce Fields session->se_cb_prog = bc->bc_cb_program; 2918cb73a9f4SJ. Bruce Fields session->se_cb_sec = bc->bc_cb_sec; 2919c9a49628SStanislav Kinsbursky spin_unlock(&nn->client_lock); 2920cb73a9f4SJ. Bruce Fields 2921cb73a9f4SJ. Bruce Fields nfsd4_probe_callback(session->se_client); 2922cb73a9f4SJ. Bruce Fields 2923cb73a9f4SJ. Bruce Fields return nfs_ok; 2924cb73a9f4SJ. Bruce Fields } 2925cb73a9f4SJ. Bruce Fields 29261d1bc8f2SJ. Bruce Fields __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, 29271d1bc8f2SJ. Bruce Fields struct nfsd4_compound_state *cstate, 2928eb69853dSChristoph Hellwig union nfsd4_op_u *u) 29291d1bc8f2SJ. Bruce Fields { 2930eb69853dSChristoph Hellwig struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session; 29311d1bc8f2SJ. Bruce Fields __be32 status; 29323ba63671SJ. Bruce Fields struct nfsd4_conn *conn; 29334f6e6c17SJ. Bruce Fields struct nfsd4_session *session; 2934d4e19e70STrond Myklebust struct net *net = SVC_NET(rqstp); 2935d4e19e70STrond Myklebust struct nfsd_net *nn = net_generic(net, nfsd_net_id); 29361d1bc8f2SJ. Bruce Fields 29371d1bc8f2SJ. Bruce Fields if (!nfsd4_last_compound_op(rqstp)) 29381d1bc8f2SJ. Bruce Fields return nfserr_not_only_op; 2939c9a49628SStanislav Kinsbursky spin_lock(&nn->client_lock); 2940d4e19e70STrond Myklebust session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); 2941c9a49628SStanislav Kinsbursky spin_unlock(&nn->client_lock); 29424f6e6c17SJ. Bruce Fields if (!session) 2943d4e19e70STrond Myklebust goto out_no_session; 294457266a6eSJ. Bruce Fields status = nfserr_wrong_cred; 2945dedeb13fSAndrew Elble if (!nfsd4_mach_creds_match(session->se_client, rqstp)) 294657266a6eSJ. Bruce Fields goto out; 29471d1bc8f2SJ. Bruce Fields status = nfsd4_map_bcts_dir(&bcts->dir); 29483ba63671SJ. Bruce Fields if (status) 29494f6e6c17SJ. Bruce Fields goto out; 29503ba63671SJ. Bruce Fields conn = alloc_conn(rqstp, bcts->dir); 29514f6e6c17SJ. Bruce Fields status = nfserr_jukebox; 29523ba63671SJ. Bruce Fields if (!conn) 29534f6e6c17SJ. Bruce Fields goto out; 29544f6e6c17SJ. Bruce Fields nfsd4_init_conn(rqstp, conn, session); 29554f6e6c17SJ. Bruce Fields status = nfs_ok; 29564f6e6c17SJ. Bruce Fields out: 2957d4e19e70STrond Myklebust nfsd4_put_session(session); 2958d4e19e70STrond Myklebust out_no_session: 29594f6e6c17SJ. Bruce Fields return status; 29601d1bc8f2SJ. Bruce Fields } 29611d1bc8f2SJ. Bruce Fields 2962665d5072SJ. Bruce Fields static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid) 29635d4cec2fSJ. Bruce Fields { 2964665d5072SJ. Bruce Fields if (!cstate->session) 296551d87bc2SFengguang Wu return false; 2966665d5072SJ. Bruce Fields return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); 29675d4cec2fSJ. Bruce Fields } 29685d4cec2fSJ. Bruce Fields 2969069b6ad4SAndy Adamson __be32 2970eb69853dSChristoph Hellwig nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, 2971eb69853dSChristoph Hellwig union nfsd4_op_u *u) 2972069b6ad4SAndy Adamson { 2973ca0552f4SJ. Bruce Fields struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; 2974e10e0cfcSBenny Halevy struct nfsd4_session *ses; 2975abcdff09SJ. Bruce Fields __be32 status; 2976f0f51f5cSJ. Bruce Fields int ref_held_by_me = 0; 2977d4e19e70STrond Myklebust struct net *net = SVC_NET(r); 2978d4e19e70STrond Myklebust struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2979e10e0cfcSBenny Halevy 2980abcdff09SJ. Bruce Fields status = nfserr_not_only_op; 2981ca0552f4SJ. Bruce Fields if (nfsd4_compound_in_session(cstate, sessionid)) { 298257716355SJ. Bruce Fields if (!nfsd4_last_compound_op(r)) 2983abcdff09SJ. Bruce Fields goto out; 2984f0f51f5cSJ. Bruce Fields ref_held_by_me++; 298557716355SJ. Bruce Fields } 2986ca0552f4SJ. Bruce Fields dump_sessionid(__func__, sessionid); 2987c9a49628SStanislav Kinsbursky spin_lock(&nn->client_lock); 2988ca0552f4SJ. Bruce Fields ses = find_in_sessionid_hashtbl(sessionid, net, &status); 2989abcdff09SJ. Bruce Fields if (!ses) 2990abcdff09SJ. Bruce Fields goto out_client_lock; 299157266a6eSJ. Bruce Fields status = nfserr_wrong_cred; 2992dedeb13fSAndrew Elble if (!nfsd4_mach_creds_match(ses->se_client, r)) 2993d4e19e70STrond Myklebust goto out_put_session; 2994f0f51f5cSJ. Bruce Fields status = mark_session_dead_locked(ses, 1 + ref_held_by_me); 299566b2b9b2SJ. Bruce Fields if (status) 2996f0f51f5cSJ. Bruce Fields goto out_put_session; 2997e10e0cfcSBenny Halevy unhash_session(ses); 2998c9a49628SStanislav Kinsbursky spin_unlock(&nn->client_lock); 2999e10e0cfcSBenny Halevy 300084f5f7ccSJ. Bruce Fields nfsd4_probe_callback_sync(ses->se_client); 300119cf5c02SJ. Bruce Fields 3002c9a49628SStanislav Kinsbursky spin_lock(&nn->client_lock); 3003e10e0cfcSBenny Halevy status = nfs_ok; 3004f0f51f5cSJ. Bruce Fields out_put_session: 3005d4e19e70STrond Myklebust nfsd4_put_session_locked(ses); 3006abcdff09SJ. Bruce Fields out_client_lock: 3007abcdff09SJ. Bruce Fields spin_unlock(&nn->client_lock); 3008e10e0cfcSBenny Halevy out: 3009e10e0cfcSBenny Halevy return status; 3010069b6ad4SAndy Adamson } 3011069b6ad4SAndy Adamson 3012a663bdd8SJ. Bruce Fields static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) 3013328ead28SJ. Bruce Fields { 3014328ead28SJ. Bruce Fields struct nfsd4_conn *c; 3015328ead28SJ. Bruce Fields 3016328ead28SJ. Bruce Fields list_for_each_entry(c, &s->se_conns, cn_persession) { 3017a663bdd8SJ. Bruce Fields if (c->cn_xprt == xpt) { 3018328ead28SJ. Bruce Fields return c; 3019328ead28SJ. Bruce Fields } 3020328ead28SJ. Bruce Fields } 3021328ead28SJ. Bruce Fields return NULL; 3022328ead28SJ. Bruce Fields } 3023328ead28SJ. Bruce Fields 302457266a6eSJ. Bruce Fields static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) 3025328ead28SJ. Bruce Fields { 3026328ead28SJ. Bruce Fields struct nfs4_client *clp = ses->se_client; 3027a663bdd8SJ. Bruce Fields struct nfsd4_conn *c; 302857266a6eSJ. Bruce Fields __be32 status = nfs_ok; 302921b75b01SJ. Bruce Fields int ret; 3030328ead28SJ. Bruce Fields 3031328ead28SJ. Bruce Fields spin_lock(&clp->cl_lock); 3032a663bdd8SJ. Bruce Fields c = __nfsd4_find_conn(new->cn_xprt, ses); 303357266a6eSJ. Bruce Fields if (c) 303457266a6eSJ. Bruce Fields goto out_free; 303557266a6eSJ. Bruce Fields status = nfserr_conn_not_bound_to_session; 303657266a6eSJ. Bruce Fields if (clp->cl_mach_cred) 303757266a6eSJ. Bruce Fields goto out_free; 3038328ead28SJ. Bruce Fields __nfsd4_hash_conn(new, ses); 3039328ead28SJ. Bruce Fields spin_unlock(&clp->cl_lock); 304021b75b01SJ. Bruce Fields ret = nfsd4_register_conn(new); 304121b75b01SJ. Bruce Fields if (ret) 304221b75b01SJ. Bruce Fields /* oops; xprt is already down: */ 304321b75b01SJ. Bruce Fields nfsd4_conn_lost(&new->cn_xpt_user); 304457266a6eSJ. Bruce Fields return nfs_ok; 304557266a6eSJ. Bruce Fields out_free: 304657266a6eSJ. Bruce Fields spin_unlock(&clp->cl_lock); 304757266a6eSJ. Bruce Fields free_conn(new); 304857266a6eSJ. Bruce Fields return status; 3049328ead28SJ. Bruce Fields } 3050328ead28SJ. Bruce Fields 3051868b89c3SMi Jinlong static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) 3052868b89c3SMi Jinlong { 3053868b89c3SMi Jinlong struct nfsd4_compoundargs *args = rqstp->rq_argp; 3054868b89c3SMi Jinlong 3055868b89c3SMi Jinlong return args->opcnt > session->se_fchannel.maxops; 3056868b89c3SMi Jinlong } 3057868b89c3SMi Jinlong 3058ae82a8d0SMi Jinlong static bool nfsd4_request_too_big(struct svc_rqst *rqstp, 3059ae82a8d0SMi Jinlong struct nfsd4_session *session) 3060ae82a8d0SMi Jinlong { 3061ae82a8d0SMi Jinlong struct xdr_buf *xb = &rqstp->rq_arg; 3062ae82a8d0SMi Jinlong 3063ae82a8d0SMi Jinlong return xb->len > session->se_fchannel.maxreq_sz; 3064ae82a8d0SMi Jinlong } 3065ae82a8d0SMi Jinlong 306653da6a53SJ. Bruce Fields static bool replay_matches_cache(struct svc_rqst *rqstp, 306753da6a53SJ. Bruce Fields struct nfsd4_sequence *seq, struct nfsd4_slot *slot) 306853da6a53SJ. Bruce Fields { 306953da6a53SJ. Bruce Fields struct nfsd4_compoundargs *argp = rqstp->rq_argp; 307053da6a53SJ. Bruce Fields 307153da6a53SJ. Bruce Fields if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != 307253da6a53SJ. Bruce Fields (bool)seq->cachethis) 307353da6a53SJ. Bruce Fields return false; 307453da6a53SJ. Bruce Fields /* 307553da6a53SJ. Bruce Fields * If there's an error than the reply can have fewer ops than 307653da6a53SJ. Bruce Fields * the call. But if we cached a reply with *more* ops than the 307753da6a53SJ. Bruce Fields * call you're sending us now, then this new call is clearly not 307853da6a53SJ. Bruce Fields * really a replay of the old one: 307953da6a53SJ. Bruce Fields */ 308053da6a53SJ. Bruce Fields if (slot->sl_opcnt < argp->opcnt) 308153da6a53SJ. Bruce Fields return false; 308253da6a53SJ. Bruce Fields /* This is the only check explicitly called by spec: */ 308353da6a53SJ. Bruce Fields if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) 308453da6a53SJ. Bruce Fields return false; 308553da6a53SJ. Bruce Fields /* 308653da6a53SJ. Bruce Fields * There may be more comparisons we could actually do, but the 308753da6a53SJ. Bruce Fields * spec doesn't require us to catch every case where the calls 308853da6a53SJ. Bruce Fields * don't match (that would require caching the call as well as 308953da6a53SJ. Bruce Fields * the reply), so we don't bother. 309053da6a53SJ. Bruce Fields */ 309153da6a53SJ. Bruce Fields return true; 309253da6a53SJ. Bruce Fields } 309353da6a53SJ. Bruce Fields 3094069b6ad4SAndy Adamson __be32 3095eb69853dSChristoph Hellwig nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 3096eb69853dSChristoph Hellwig union nfsd4_op_u *u) 3097069b6ad4SAndy Adamson { 3098eb69853dSChristoph Hellwig struct nfsd4_sequence *seq = &u->sequence; 3099f9bb94c4SAndy Adamson struct nfsd4_compoundres *resp = rqstp->rq_resp; 310047ee5298SJ. Bruce Fields struct xdr_stream *xdr = &resp->xdr; 3101b85d4c01SBenny Halevy struct nfsd4_session *session; 3102221a6876SJ. Bruce Fields struct nfs4_client *clp; 3103b85d4c01SBenny Halevy struct nfsd4_slot *slot; 3104a663bdd8SJ. Bruce Fields struct nfsd4_conn *conn; 310557b7b43bSJ. Bruce Fields __be32 status; 310647ee5298SJ. Bruce Fields int buflen; 3107d4e19e70STrond Myklebust struct net *net = SVC_NET(rqstp); 3108d4e19e70STrond Myklebust struct nfsd_net *nn = net_generic(net, nfsd_net_id); 3109b85d4c01SBenny Halevy 3110f9bb94c4SAndy Adamson if (resp->opcnt != 1) 3111f9bb94c4SAndy Adamson return nfserr_sequence_pos; 3112f9bb94c4SAndy Adamson 3113a663bdd8SJ. Bruce Fields /* 3114a663bdd8SJ. Bruce Fields * Will be either used or freed by nfsd4_sequence_check_conn 3115a663bdd8SJ. Bruce Fields * below. 3116a663bdd8SJ. Bruce Fields */ 3117a663bdd8SJ. Bruce Fields conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); 3118a663bdd8SJ. Bruce Fields if (!conn) 3119a663bdd8SJ. Bruce Fields return nfserr_jukebox; 3120a663bdd8SJ. Bruce Fields 3121c9a49628SStanislav Kinsbursky spin_lock(&nn->client_lock); 3122d4e19e70STrond Myklebust session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); 3123b85d4c01SBenny Halevy if (!session) 3124221a6876SJ. Bruce Fields goto out_no_session; 3125221a6876SJ. Bruce Fields clp = session->se_client; 3126b85d4c01SBenny Halevy 3127868b89c3SMi Jinlong status = nfserr_too_many_ops; 3128868b89c3SMi Jinlong if (nfsd4_session_too_many_ops(rqstp, session)) 312966b2b9b2SJ. Bruce Fields goto out_put_session; 3130868b89c3SMi Jinlong 3131ae82a8d0SMi Jinlong status = nfserr_req_too_big; 3132ae82a8d0SMi Jinlong if (nfsd4_request_too_big(rqstp, session)) 313366b2b9b2SJ. Bruce Fields goto out_put_session; 3134ae82a8d0SMi Jinlong 3135b85d4c01SBenny Halevy status = nfserr_badslot; 31366c18ba9fSAlexandros Batsakis if (seq->slotid >= session->se_fchannel.maxreqs) 313766b2b9b2SJ. Bruce Fields goto out_put_session; 3138b85d4c01SBenny Halevy 3139557ce264SAndy Adamson slot = session->se_slots[seq->slotid]; 3140b85d4c01SBenny Halevy dprintk("%s: slotid %d\n", __func__, seq->slotid); 3141b85d4c01SBenny Halevy 3142a8dfdaebSAndy Adamson /* We do not negotiate the number of slots yet, so set the 3143a8dfdaebSAndy Adamson * maxslots to the session maxreqs which is used to encode 3144a8dfdaebSAndy Adamson * sr_highest_slotid and the sr_target_slot id to maxslots */ 3145a8dfdaebSAndy Adamson seq->maxslots = session->se_fchannel.maxreqs; 3146a8dfdaebSAndy Adamson 314773e79482SJ. Bruce Fields status = check_slot_seqid(seq->seqid, slot->sl_seqid, 314873e79482SJ. Bruce Fields slot->sl_flags & NFSD4_SLOT_INUSE); 3149b85d4c01SBenny Halevy if (status == nfserr_replay_cache) { 3150bf5c43c8SJ. Bruce Fields status = nfserr_seq_misordered; 3151bf5c43c8SJ. Bruce Fields if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) 315266b2b9b2SJ. Bruce Fields goto out_put_session; 315353da6a53SJ. Bruce Fields status = nfserr_seq_false_retry; 315453da6a53SJ. Bruce Fields if (!replay_matches_cache(rqstp, seq, slot)) 315553da6a53SJ. Bruce Fields goto out_put_session; 3156b85d4c01SBenny Halevy cstate->slot = slot; 3157b85d4c01SBenny Halevy cstate->session = session; 31584b24ca7dSJeff Layton cstate->clp = clp; 3159da3846a2SAndy Adamson /* Return the cached reply status and set cstate->status 3160557ce264SAndy Adamson * for nfsd4_proc_compound processing */ 3161bf864a31SAndy Adamson status = nfsd4_replay_cache_entry(resp, seq); 3162da3846a2SAndy Adamson cstate->status = nfserr_replay_cache; 3163aaf84eb9SBenny Halevy goto out; 3164b85d4c01SBenny Halevy } 3165b85d4c01SBenny Halevy if (status) 316666b2b9b2SJ. Bruce Fields goto out_put_session; 3167b85d4c01SBenny Halevy 316857266a6eSJ. Bruce Fields status = nfsd4_sequence_check_conn(conn, session); 3169a663bdd8SJ. Bruce Fields conn = NULL; 317057266a6eSJ. Bruce Fields if (status) 317157266a6eSJ. Bruce Fields goto out_put_session; 3172328ead28SJ. Bruce Fields 317347ee5298SJ. Bruce Fields buflen = (seq->cachethis) ? 317447ee5298SJ. Bruce Fields session->se_fchannel.maxresp_cached : 317547ee5298SJ. Bruce Fields session->se_fchannel.maxresp_sz; 317647ee5298SJ. Bruce Fields status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : 317747ee5298SJ. Bruce Fields nfserr_rep_too_big; 3178a5cddc88SJ. Bruce Fields if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) 317947ee5298SJ. Bruce Fields goto out_put_session; 318032aaa62eSJ. Bruce Fields svc_reserve(rqstp, buflen); 318147ee5298SJ. Bruce Fields 318247ee5298SJ. Bruce Fields status = nfs_ok; 3183b85d4c01SBenny Halevy /* Success! bump slot seqid */ 3184b85d4c01SBenny Halevy slot->sl_seqid = seq->seqid; 3185bf5c43c8SJ. Bruce Fields slot->sl_flags |= NFSD4_SLOT_INUSE; 318673e79482SJ. Bruce Fields if (seq->cachethis) 318773e79482SJ. Bruce Fields slot->sl_flags |= NFSD4_SLOT_CACHETHIS; 3188bf5c43c8SJ. Bruce Fields else 3189bf5c43c8SJ. Bruce Fields slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; 3190b85d4c01SBenny Halevy 3191b85d4c01SBenny Halevy cstate->slot = slot; 3192b85d4c01SBenny Halevy cstate->session = session; 31934b24ca7dSJeff Layton cstate->clp = clp; 3194b85d4c01SBenny Halevy 3195b85d4c01SBenny Halevy out: 31965423732aSBenny Halevy switch (clp->cl_cb_state) { 31975423732aSBenny Halevy case NFSD4_CB_DOWN: 3198fc0c3dd1SBenny Halevy seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; 31995423732aSBenny Halevy break; 32005423732aSBenny Halevy case NFSD4_CB_FAULT: 3201fc0c3dd1SBenny Halevy seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; 32025423732aSBenny Halevy break; 3203fc0c3dd1SBenny Halevy default: 3204fc0c3dd1SBenny Halevy seq->status_flags = 0; 32055423732aSBenny Halevy } 32063bd64a5bSJ. Bruce Fields if (!list_empty(&clp->cl_revoked)) 32073bd64a5bSJ. Bruce Fields seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; 3208221a6876SJ. Bruce Fields out_no_session: 32093f42d2c4SKinglong Mee if (conn) 32103f42d2c4SKinglong Mee free_conn(conn); 3211c9a49628SStanislav Kinsbursky spin_unlock(&nn->client_lock); 3212b85d4c01SBenny Halevy return status; 321366b2b9b2SJ. Bruce Fields out_put_session: 3214d4e19e70STrond Myklebust nfsd4_put_session_locked(session); 3215221a6876SJ. Bruce Fields goto out_no_session; 3216069b6ad4SAndy Adamson } 3217069b6ad4SAndy Adamson 3218b607664eSTrond Myklebust void 3219b607664eSTrond Myklebust nfsd4_sequence_done(struct nfsd4_compoundres *resp) 3220b607664eSTrond Myklebust { 3221b607664eSTrond Myklebust struct nfsd4_compound_state *cs = &resp->cstate; 3222b607664eSTrond Myklebust 3223b607664eSTrond Myklebust if (nfsd4_has_session(cs)) { 3224b607664eSTrond Myklebust if (cs->status != nfserr_replay_cache) { 3225b607664eSTrond Myklebust nfsd4_store_cache_entry(resp); 3226b607664eSTrond Myklebust cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; 3227b607664eSTrond Myklebust } 3228d4e19e70STrond Myklebust /* Drop session reference that was taken in nfsd4_sequence() */ 3229b607664eSTrond Myklebust nfsd4_put_session(cs->session); 32304b24ca7dSJeff Layton } else if (cs->clp) 32314b24ca7dSJeff Layton put_client_renew(cs->clp); 3232b607664eSTrond Myklebust } 3233b607664eSTrond Myklebust 3234345c2842SMi Jinlong __be32 3235eb69853dSChristoph Hellwig nfsd4_destroy_clientid(struct svc_rqst *rqstp, 3236eb69853dSChristoph Hellwig struct nfsd4_compound_state *cstate, 3237eb69853dSChristoph Hellwig union nfsd4_op_u *u) 3238345c2842SMi Jinlong { 3239eb69853dSChristoph Hellwig struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; 32406b10ad19STrond Myklebust struct nfs4_client *conf, *unconf; 32416b10ad19STrond Myklebust struct nfs4_client *clp = NULL; 324257b7b43bSJ. Bruce Fields __be32 status = 0; 32438daae4dcSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 3244345c2842SMi Jinlong 32456b10ad19STrond Myklebust spin_lock(&nn->client_lock); 32460a7ec377SStanislav Kinsbursky unconf = find_unconfirmed_client(&dc->clientid, true, nn); 32478daae4dcSStanislav Kinsbursky conf = find_confirmed_client(&dc->clientid, true, nn); 324878389046SJ. Bruce Fields WARN_ON_ONCE(conf && unconf); 3249345c2842SMi Jinlong 3250345c2842SMi Jinlong if (conf) { 3251c0293b01SJ. Bruce Fields if (client_has_state(conf)) { 3252345c2842SMi Jinlong status = nfserr_clientid_busy; 3253345c2842SMi Jinlong goto out; 3254345c2842SMi Jinlong } 3255fd699b8aSJeff Layton status = mark_client_expired_locked(conf); 3256fd699b8aSJeff Layton if (status) 3257fd699b8aSJeff Layton goto out; 32586b10ad19STrond Myklebust clp = conf; 3259345c2842SMi Jinlong } else if (unconf) 3260345c2842SMi Jinlong clp = unconf; 3261345c2842SMi Jinlong else { 3262345c2842SMi Jinlong status = nfserr_stale_clientid; 3263345c2842SMi Jinlong goto out; 3264345c2842SMi Jinlong } 3265dedeb13fSAndrew Elble if (!nfsd4_mach_creds_match(clp, rqstp)) { 32666b10ad19STrond Myklebust clp = NULL; 326757266a6eSJ. Bruce Fields status = nfserr_wrong_cred; 326857266a6eSJ. Bruce Fields goto out; 326957266a6eSJ. Bruce Fields } 32706b10ad19STrond Myklebust unhash_client_locked(clp); 3271345c2842SMi Jinlong out: 32726b10ad19STrond Myklebust spin_unlock(&nn->client_lock); 32736b10ad19STrond Myklebust if (clp) 32746b10ad19STrond Myklebust expire_client(clp); 3275345c2842SMi Jinlong return status; 3276345c2842SMi Jinlong } 3277345c2842SMi Jinlong 3278069b6ad4SAndy Adamson __be32 3279eb69853dSChristoph Hellwig nfsd4_reclaim_complete(struct svc_rqst *rqstp, 3280eb69853dSChristoph Hellwig struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 32814dc6ec00SJ. Bruce Fields { 3282eb69853dSChristoph Hellwig struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; 328357b7b43bSJ. Bruce Fields __be32 status = 0; 3284bcecf1ccSMi Jinlong 32854dc6ec00SJ. Bruce Fields if (rc->rca_one_fs) { 32864dc6ec00SJ. Bruce Fields if (!cstate->current_fh.fh_dentry) 32874dc6ec00SJ. Bruce Fields return nfserr_nofilehandle; 32884dc6ec00SJ. Bruce Fields /* 32894dc6ec00SJ. Bruce Fields * We don't take advantage of the rca_one_fs case. 32904dc6ec00SJ. Bruce Fields * That's OK, it's optional, we can safely ignore it. 32914dc6ec00SJ. Bruce Fields */ 32924dc6ec00SJ. Bruce Fields return nfs_ok; 32934dc6ec00SJ. Bruce Fields } 3294bcecf1ccSMi Jinlong 3295bcecf1ccSMi Jinlong status = nfserr_complete_already; 3296a52d726bSJeff Layton if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, 3297a52d726bSJeff Layton &cstate->session->se_client->cl_flags)) 3298bcecf1ccSMi Jinlong goto out; 3299bcecf1ccSMi Jinlong 3300bcecf1ccSMi Jinlong status = nfserr_stale_clientid; 3301bcecf1ccSMi Jinlong if (is_client_expired(cstate->session->se_client)) 33024dc6ec00SJ. Bruce Fields /* 33034dc6ec00SJ. Bruce Fields * The following error isn't really legal. 33044dc6ec00SJ. Bruce Fields * But we only get here if the client just explicitly 33054dc6ec00SJ. Bruce Fields * destroyed the client. Surely it no longer cares what 33064dc6ec00SJ. Bruce Fields * error it gets back on an operation for the dead 33074dc6ec00SJ. Bruce Fields * client. 33084dc6ec00SJ. Bruce Fields */ 3309bcecf1ccSMi Jinlong goto out; 3310bcecf1ccSMi Jinlong 3311bcecf1ccSMi Jinlong status = nfs_ok; 33122a4317c5SJeff Layton nfsd4_client_record_create(cstate->session->se_client); 3313bcecf1ccSMi Jinlong out: 3314bcecf1ccSMi Jinlong return status; 33154dc6ec00SJ. Bruce Fields } 33164dc6ec00SJ. Bruce Fields 33174dc6ec00SJ. Bruce Fields __be32 3318b591480bSJ.Bruce Fields nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 3319eb69853dSChristoph Hellwig union nfsd4_op_u *u) 33201da177e4SLinus Torvalds { 3321eb69853dSChristoph Hellwig struct nfsd4_setclientid *setclid = &u->setclientid; 3322a084daf5SJ. Bruce Fields struct xdr_netobj clname = setclid->se_name; 33231da177e4SLinus Torvalds nfs4_verifier clverifier = setclid->se_verf; 33243dbacee6STrond Myklebust struct nfs4_client *conf, *new; 33253dbacee6STrond Myklebust struct nfs4_client *unconf = NULL; 3326b37ad28bSAl Viro __be32 status; 3327c212cecfSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 3328a55370a3SNeilBrown 33295cc40fd7STrond Myklebust new = create_client(clname, rqstp, &clverifier); 33305cc40fd7STrond Myklebust if (new == NULL) 33315cc40fd7STrond Myklebust return nfserr_jukebox; 333263db4632SJ. Bruce Fields /* Cases below refer to rfc 3530 section 14.2.33: */ 33333dbacee6STrond Myklebust spin_lock(&nn->client_lock); 3334382a62e7SStanislav Kinsbursky conf = find_confirmed_client_by_name(&clname, nn); 33352b634821SJ. Bruce Fields if (conf && client_has_state(conf)) { 333663db4632SJ. Bruce Fields /* case 0: */ 33371da177e4SLinus Torvalds status = nfserr_clid_inuse; 3338e203d506SJ. Bruce Fields if (clp_used_exchangeid(conf)) 3339e203d506SJ. Bruce Fields goto out; 3340026722c2SJ. Bruce Fields if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { 3341363168b4SJeff Layton char addr_str[INET6_ADDRSTRLEN]; 3342363168b4SJeff Layton rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, 3343363168b4SJeff Layton sizeof(addr_str)); 3344026722c2SJ. Bruce Fields dprintk("NFSD: setclientid: string in use by client " 3345363168b4SJeff Layton "at %s\n", addr_str); 33461da177e4SLinus Torvalds goto out; 33471da177e4SLinus Torvalds } 33481da177e4SLinus Torvalds } 3349a99454aaSStanislav Kinsbursky unconf = find_unconfirmed_client_by_name(&clname, nn); 33501da177e4SLinus Torvalds if (unconf) 33513dbacee6STrond Myklebust unhash_client_locked(unconf); 335241eb1670SKinglong Mee if (conf && same_verf(&conf->cl_verifier, &clverifier)) { 335363db4632SJ. Bruce Fields /* case 1: probable callback update */ 33541da177e4SLinus Torvalds copy_clid(new, conf); 335541eb1670SKinglong Mee gen_confirm(new, nn); 335641eb1670SKinglong Mee } else /* case 4 (new client) or cases 2, 3 (client reboot): */ 3357c212cecfSStanislav Kinsbursky gen_clid(new, nn); 33588323c3b2SJ. Bruce Fields new->cl_minorversion = 0; 33596f3d772fSTakuma Umeya gen_callback(new, setclid, rqstp); 3360ac55fdc4SJeff Layton add_to_unconfirmed(new); 33611da177e4SLinus Torvalds setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; 33621da177e4SLinus Torvalds setclid->se_clientid.cl_id = new->cl_clientid.cl_id; 33631da177e4SLinus Torvalds memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); 33645cc40fd7STrond Myklebust new = NULL; 33651da177e4SLinus Torvalds status = nfs_ok; 33661da177e4SLinus Torvalds out: 33673dbacee6STrond Myklebust spin_unlock(&nn->client_lock); 33685cc40fd7STrond Myklebust if (new) 33695cc40fd7STrond Myklebust free_client(new); 33703dbacee6STrond Myklebust if (unconf) 33713dbacee6STrond Myklebust expire_client(unconf); 33721da177e4SLinus Torvalds return status; 33731da177e4SLinus Torvalds } 33741da177e4SLinus Torvalds 33751da177e4SLinus Torvalds 3376b37ad28bSAl Viro __be32 3377b591480bSJ.Bruce Fields nfsd4_setclientid_confirm(struct svc_rqst *rqstp, 3378b591480bSJ.Bruce Fields struct nfsd4_compound_state *cstate, 3379eb69853dSChristoph Hellwig union nfsd4_op_u *u) 33801da177e4SLinus Torvalds { 3381eb69853dSChristoph Hellwig struct nfsd4_setclientid_confirm *setclientid_confirm = 3382eb69853dSChristoph Hellwig &u->setclientid_confirm; 338321ab45a4SNeilBrown struct nfs4_client *conf, *unconf; 3384d20c11d8SJeff Layton struct nfs4_client *old = NULL; 33851da177e4SLinus Torvalds nfs4_verifier confirm = setclientid_confirm->sc_confirm; 33861da177e4SLinus Torvalds clientid_t * clid = &setclientid_confirm->sc_clientid; 3387b37ad28bSAl Viro __be32 status; 33887f2210faSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 33891da177e4SLinus Torvalds 33902c142baaSStanislav Kinsbursky if (STALE_CLIENTID(clid, nn)) 33911da177e4SLinus Torvalds return nfserr_stale_clientid; 339221ab45a4SNeilBrown 3393d20c11d8SJeff Layton spin_lock(&nn->client_lock); 33948daae4dcSStanislav Kinsbursky conf = find_confirmed_client(clid, false, nn); 33950a7ec377SStanislav Kinsbursky unconf = find_unconfirmed_client(clid, false, nn); 3396a186e767SJ. Bruce Fields /* 33978695b90aSJ. Bruce Fields * We try hard to give out unique clientid's, so if we get an 33988695b90aSJ. Bruce Fields * attempt to confirm the same clientid with a different cred, 3399f984a7ceSJ. Bruce Fields * the client may be buggy; this should never happen. 3400f984a7ceSJ. Bruce Fields * 3401f984a7ceSJ. Bruce Fields * Nevertheless, RFC 7530 recommends INUSE for this case: 3402a186e767SJ. Bruce Fields */ 3403f984a7ceSJ. Bruce Fields status = nfserr_clid_inuse; 34048695b90aSJ. Bruce Fields if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) 34058695b90aSJ. Bruce Fields goto out; 34068695b90aSJ. Bruce Fields if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) 34078695b90aSJ. Bruce Fields goto out; 340863db4632SJ. Bruce Fields /* cases below refer to rfc 3530 section 14.2.34: */ 340990d700b7SJ. Bruce Fields if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { 34107d22fc11SJ. Bruce Fields if (conf && same_verf(&confirm, &conf->cl_confirm)) { 34117d22fc11SJ. Bruce Fields /* case 2: probable retransmit */ 341290d700b7SJ. Bruce Fields status = nfs_ok; 34137d22fc11SJ. Bruce Fields } else /* case 4: client hasn't noticed we rebooted yet? */ 341490d700b7SJ. Bruce Fields status = nfserr_stale_clientid; 341590d700b7SJ. Bruce Fields goto out; 341690d700b7SJ. Bruce Fields } 341790d700b7SJ. Bruce Fields status = nfs_ok; 341890d700b7SJ. Bruce Fields if (conf) { /* case 1: callback update */ 3419d20c11d8SJeff Layton old = unconf; 3420d20c11d8SJeff Layton unhash_client_locked(old); 34215a3c9d71SJ. Bruce Fields nfsd4_change_callback(conf, &unconf->cl_cb_conn); 342290d700b7SJ. Bruce Fields } else { /* case 3: normal case; new or rebooted client */ 3423d20c11d8SJeff Layton old = find_confirmed_client_by_name(&unconf->cl_name, nn); 3424d20c11d8SJeff Layton if (old) { 34252b634821SJ. Bruce Fields status = nfserr_clid_inuse; 34262b634821SJ. Bruce Fields if (client_has_state(old) 34272b634821SJ. Bruce Fields && !same_creds(&unconf->cl_cred, 34282b634821SJ. Bruce Fields &old->cl_cred)) 34292b634821SJ. Bruce Fields goto out; 3430d20c11d8SJeff Layton status = mark_client_expired_locked(old); 34317abea1e8SJeff Layton if (status) { 34327abea1e8SJeff Layton old = NULL; 3433221a6876SJ. Bruce Fields goto out; 3434221a6876SJ. Bruce Fields } 34357abea1e8SJeff Layton } 34361da177e4SLinus Torvalds move_to_confirmed(unconf); 3437d20c11d8SJeff Layton conf = unconf; 343808e8987cSNeilBrown } 3439d20c11d8SJeff Layton get_client_locked(conf); 3440d20c11d8SJeff Layton spin_unlock(&nn->client_lock); 3441d20c11d8SJeff Layton nfsd4_probe_callback(conf); 3442d20c11d8SJeff Layton spin_lock(&nn->client_lock); 3443d20c11d8SJeff Layton put_client_renew_locked(conf); 34441da177e4SLinus Torvalds out: 3445d20c11d8SJeff Layton spin_unlock(&nn->client_lock); 3446d20c11d8SJeff Layton if (old) 3447d20c11d8SJeff Layton expire_client(old); 34481da177e4SLinus Torvalds return status; 34491da177e4SLinus Torvalds } 34501da177e4SLinus Torvalds 345132513b40SJ. Bruce Fields static struct nfs4_file *nfsd4_alloc_file(void) 34521da177e4SLinus Torvalds { 345332513b40SJ. Bruce Fields return kmem_cache_alloc(file_slab, GFP_KERNEL); 345432513b40SJ. Bruce Fields } 345532513b40SJ. Bruce Fields 345632513b40SJ. Bruce Fields /* OPEN Share state helper functions */ 34575b095e99SJeff Layton static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, 34585b095e99SJeff Layton struct nfs4_file *fp) 345932513b40SJ. Bruce Fields { 3460950e0118STrond Myklebust lockdep_assert_held(&state_lock); 3461950e0118STrond Myklebust 3462818a34ebSElena Reshetova refcount_set(&fp->fi_ref, 1); 34631d31a253STrond Myklebust spin_lock_init(&fp->fi_lock); 34648beefa24SNeilBrown INIT_LIST_HEAD(&fp->fi_stateids); 34658beefa24SNeilBrown INIT_LIST_HEAD(&fp->fi_delegations); 34668287f009SSachin Bhamare INIT_LIST_HEAD(&fp->fi_clnt_odstate); 3467e2cf80d7STrond Myklebust fh_copy_shallow(&fp->fi_fhandle, fh); 34680c637be8SJeff Layton fp->fi_deleg_file = NULL; 346947f9940cSMeelap Shah fp->fi_had_conflict = false; 3470baeb4ff0SJeff Layton fp->fi_share_deny = 0; 3471f9d7562fSJ. Bruce Fields memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); 3472f9d7562fSJ. Bruce Fields memset(fp->fi_access, 0, sizeof(fp->fi_access)); 34739cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS 34749cf514ccSChristoph Hellwig INIT_LIST_HEAD(&fp->fi_lo_states); 3475c5c707f9SChristoph Hellwig atomic_set(&fp->fi_lo_recalls, 0); 34769cf514ccSChristoph Hellwig #endif 34775b095e99SJeff Layton hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); 34781da177e4SLinus Torvalds } 34791da177e4SLinus Torvalds 3480e8ff2a84SJ. Bruce Fields void 3481e60d4398SNeilBrown nfsd4_free_slabs(void) 3482e60d4398SNeilBrown { 34839258a2d5SJeff Layton kmem_cache_destroy(client_slab); 3484abf1135bSChristoph Hellwig kmem_cache_destroy(openowner_slab); 3485abf1135bSChristoph Hellwig kmem_cache_destroy(lockowner_slab); 3486abf1135bSChristoph Hellwig kmem_cache_destroy(file_slab); 3487abf1135bSChristoph Hellwig kmem_cache_destroy(stateid_slab); 3488abf1135bSChristoph Hellwig kmem_cache_destroy(deleg_slab); 34899258a2d5SJeff Layton kmem_cache_destroy(odstate_slab); 3490e60d4398SNeilBrown } 34911da177e4SLinus Torvalds 349272083396SBryan Schumaker int 34931da177e4SLinus Torvalds nfsd4_init_slabs(void) 34941da177e4SLinus Torvalds { 34959258a2d5SJeff Layton client_slab = kmem_cache_create("nfsd4_clients", 34969258a2d5SJeff Layton sizeof(struct nfs4_client), 0, 0, NULL); 34979258a2d5SJeff Layton if (client_slab == NULL) 34989258a2d5SJeff Layton goto out; 3499fe0750e5SJ. Bruce Fields openowner_slab = kmem_cache_create("nfsd4_openowners", 3500fe0750e5SJ. Bruce Fields sizeof(struct nfs4_openowner), 0, 0, NULL); 3501fe0750e5SJ. Bruce Fields if (openowner_slab == NULL) 35029258a2d5SJeff Layton goto out_free_client_slab; 3503fe0750e5SJ. Bruce Fields lockowner_slab = kmem_cache_create("nfsd4_lockowners", 35043c40794bSYanchuan Nian sizeof(struct nfs4_lockowner), 0, 0, NULL); 3505fe0750e5SJ. Bruce Fields if (lockowner_slab == NULL) 3506abf1135bSChristoph Hellwig goto out_free_openowner_slab; 3507e60d4398SNeilBrown file_slab = kmem_cache_create("nfsd4_files", 350820c2df83SPaul Mundt sizeof(struct nfs4_file), 0, 0, NULL); 3509e60d4398SNeilBrown if (file_slab == NULL) 3510abf1135bSChristoph Hellwig goto out_free_lockowner_slab; 35115ac049acSNeilBrown stateid_slab = kmem_cache_create("nfsd4_stateids", 3512dcef0413SJ. Bruce Fields sizeof(struct nfs4_ol_stateid), 0, 0, NULL); 35135ac049acSNeilBrown if (stateid_slab == NULL) 3514abf1135bSChristoph Hellwig goto out_free_file_slab; 35155b2d21c1SNeilBrown deleg_slab = kmem_cache_create("nfsd4_delegations", 351620c2df83SPaul Mundt sizeof(struct nfs4_delegation), 0, 0, NULL); 35175b2d21c1SNeilBrown if (deleg_slab == NULL) 3518abf1135bSChristoph Hellwig goto out_free_stateid_slab; 35198287f009SSachin Bhamare odstate_slab = kmem_cache_create("nfsd4_odstate", 35208287f009SSachin Bhamare sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); 35218287f009SSachin Bhamare if (odstate_slab == NULL) 35228287f009SSachin Bhamare goto out_free_deleg_slab; 3523e60d4398SNeilBrown return 0; 3524abf1135bSChristoph Hellwig 35258287f009SSachin Bhamare out_free_deleg_slab: 35268287f009SSachin Bhamare kmem_cache_destroy(deleg_slab); 3527abf1135bSChristoph Hellwig out_free_stateid_slab: 3528abf1135bSChristoph Hellwig kmem_cache_destroy(stateid_slab); 3529abf1135bSChristoph Hellwig out_free_file_slab: 3530abf1135bSChristoph Hellwig kmem_cache_destroy(file_slab); 3531abf1135bSChristoph Hellwig out_free_lockowner_slab: 3532abf1135bSChristoph Hellwig kmem_cache_destroy(lockowner_slab); 3533abf1135bSChristoph Hellwig out_free_openowner_slab: 3534abf1135bSChristoph Hellwig kmem_cache_destroy(openowner_slab); 35359258a2d5SJeff Layton out_free_client_slab: 35369258a2d5SJeff Layton kmem_cache_destroy(client_slab); 3537abf1135bSChristoph Hellwig out: 35381da177e4SLinus Torvalds dprintk("nfsd4: out of memory while initializing nfsv4\n"); 35391da177e4SLinus Torvalds return -ENOMEM; 35401da177e4SLinus Torvalds } 35411da177e4SLinus Torvalds 3542ff194bd9SJ. Bruce Fields static void init_nfs4_replay(struct nfs4_replay *rp) 3543ff194bd9SJ. Bruce Fields { 3544ff194bd9SJ. Bruce Fields rp->rp_status = nfserr_serverfault; 3545ff194bd9SJ. Bruce Fields rp->rp_buflen = 0; 3546ff194bd9SJ. Bruce Fields rp->rp_buf = rp->rp_ibuf; 354758fb12e6SJeff Layton mutex_init(&rp->rp_mutex); 354858fb12e6SJeff Layton } 354958fb12e6SJeff Layton 355058fb12e6SJeff Layton static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, 355158fb12e6SJeff Layton struct nfs4_stateowner *so) 355258fb12e6SJeff Layton { 355358fb12e6SJeff Layton if (!nfsd4_has_session(cstate)) { 355458fb12e6SJeff Layton mutex_lock(&so->so_replay.rp_mutex); 3555b5971afaSKinglong Mee cstate->replay_owner = nfs4_get_stateowner(so); 355658fb12e6SJeff Layton } 355758fb12e6SJeff Layton } 355858fb12e6SJeff Layton 355958fb12e6SJeff Layton void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) 356058fb12e6SJeff Layton { 356158fb12e6SJeff Layton struct nfs4_stateowner *so = cstate->replay_owner; 356258fb12e6SJeff Layton 356358fb12e6SJeff Layton if (so != NULL) { 356458fb12e6SJeff Layton cstate->replay_owner = NULL; 356558fb12e6SJeff Layton mutex_unlock(&so->so_replay.rp_mutex); 356658fb12e6SJeff Layton nfs4_put_stateowner(so); 356758fb12e6SJeff Layton } 3568ff194bd9SJ. Bruce Fields } 3569ff194bd9SJ. Bruce Fields 3570fe0750e5SJ. Bruce Fields static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) 35711da177e4SLinus Torvalds { 35721da177e4SLinus Torvalds struct nfs4_stateowner *sop; 35731da177e4SLinus Torvalds 3574fe0750e5SJ. Bruce Fields sop = kmem_cache_alloc(slab, GFP_KERNEL); 3575ff194bd9SJ. Bruce Fields if (!sop) 3576ff194bd9SJ. Bruce Fields return NULL; 3577ff194bd9SJ. Bruce Fields 3578ff194bd9SJ. Bruce Fields sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); 3579ff194bd9SJ. Bruce Fields if (!sop->so_owner.data) { 3580fe0750e5SJ. Bruce Fields kmem_cache_free(slab, sop); 3581ff194bd9SJ. Bruce Fields return NULL; 3582ff194bd9SJ. Bruce Fields } 35831da177e4SLinus Torvalds sop->so_owner.len = owner->len; 3584ff194bd9SJ. Bruce Fields 3585ff194bd9SJ. Bruce Fields INIT_LIST_HEAD(&sop->so_stateids); 3586ff194bd9SJ. Bruce Fields sop->so_client = clp; 3587ff194bd9SJ. Bruce Fields init_nfs4_replay(&sop->so_replay); 35886b180f0bSJeff Layton atomic_set(&sop->so_count, 1); 35891da177e4SLinus Torvalds return sop; 35901da177e4SLinus Torvalds } 3591ff194bd9SJ. Bruce Fields 3592fe0750e5SJ. Bruce Fields static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) 3593ff194bd9SJ. Bruce Fields { 3594d4f0489fSTrond Myklebust lockdep_assert_held(&clp->cl_lock); 35959b531137SStanislav Kinsbursky 3596d4f0489fSTrond Myklebust list_add(&oo->oo_owner.so_strhash, 3597d4f0489fSTrond Myklebust &clp->cl_ownerstr_hashtbl[strhashval]); 3598fe0750e5SJ. Bruce Fields list_add(&oo->oo_perclient, &clp->cl_openowners); 35991da177e4SLinus Torvalds } 36001da177e4SLinus Torvalds 36018f4b54c5SJeff Layton static void nfs4_unhash_openowner(struct nfs4_stateowner *so) 36028f4b54c5SJeff Layton { 3603d4f0489fSTrond Myklebust unhash_openowner_locked(openowner(so)); 36048f4b54c5SJeff Layton } 36058f4b54c5SJeff Layton 36066b180f0bSJeff Layton static void nfs4_free_openowner(struct nfs4_stateowner *so) 36076b180f0bSJeff Layton { 36086b180f0bSJeff Layton struct nfs4_openowner *oo = openowner(so); 36096b180f0bSJeff Layton 36106b180f0bSJeff Layton kmem_cache_free(openowner_slab, oo); 36116b180f0bSJeff Layton } 36126b180f0bSJeff Layton 36136b180f0bSJeff Layton static const struct nfs4_stateowner_operations openowner_ops = { 36148f4b54c5SJeff Layton .so_unhash = nfs4_unhash_openowner, 36156b180f0bSJeff Layton .so_free = nfs4_free_openowner, 36166b180f0bSJeff Layton }; 36176b180f0bSJeff Layton 36187fc0564eSAndrew Elble static struct nfs4_ol_stateid * 36197fc0564eSAndrew Elble nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) 36207fc0564eSAndrew Elble { 36217fc0564eSAndrew Elble struct nfs4_ol_stateid *local, *ret = NULL; 36227fc0564eSAndrew Elble struct nfs4_openowner *oo = open->op_openowner; 36237fc0564eSAndrew Elble 36247fc0564eSAndrew Elble lockdep_assert_held(&fp->fi_lock); 36257fc0564eSAndrew Elble 36267fc0564eSAndrew Elble list_for_each_entry(local, &fp->fi_stateids, st_perfile) { 36277fc0564eSAndrew Elble /* ignore lock owners */ 36287fc0564eSAndrew Elble if (local->st_stateowner->so_is_open_owner == 0) 36297fc0564eSAndrew Elble continue; 363015ca08d3STrond Myklebust if (local->st_stateowner != &oo->oo_owner) 363115ca08d3STrond Myklebust continue; 363215ca08d3STrond Myklebust if (local->st_stid.sc_type == NFS4_OPEN_STID) { 36337fc0564eSAndrew Elble ret = local; 3634a15dfcd5SElena Reshetova refcount_inc(&ret->st_stid.sc_count); 36357fc0564eSAndrew Elble break; 36367fc0564eSAndrew Elble } 36377fc0564eSAndrew Elble } 36387fc0564eSAndrew Elble return ret; 36397fc0564eSAndrew Elble } 36407fc0564eSAndrew Elble 364115ca08d3STrond Myklebust static __be32 364215ca08d3STrond Myklebust nfsd4_verify_open_stid(struct nfs4_stid *s) 364315ca08d3STrond Myklebust { 364415ca08d3STrond Myklebust __be32 ret = nfs_ok; 364515ca08d3STrond Myklebust 364615ca08d3STrond Myklebust switch (s->sc_type) { 364715ca08d3STrond Myklebust default: 364815ca08d3STrond Myklebust break; 36494f176417STrond Myklebust case 0: 365015ca08d3STrond Myklebust case NFS4_CLOSED_STID: 365115ca08d3STrond Myklebust case NFS4_CLOSED_DELEG_STID: 365215ca08d3STrond Myklebust ret = nfserr_bad_stateid; 365315ca08d3STrond Myklebust break; 365415ca08d3STrond Myklebust case NFS4_REVOKED_DELEG_STID: 365515ca08d3STrond Myklebust ret = nfserr_deleg_revoked; 365615ca08d3STrond Myklebust } 365715ca08d3STrond Myklebust return ret; 365815ca08d3STrond Myklebust } 365915ca08d3STrond Myklebust 366015ca08d3STrond Myklebust /* Lock the stateid st_mutex, and deal with races with CLOSE */ 366115ca08d3STrond Myklebust static __be32 366215ca08d3STrond Myklebust nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) 366315ca08d3STrond Myklebust { 366415ca08d3STrond Myklebust __be32 ret; 366515ca08d3STrond Myklebust 36664f34bd05SAndrew Elble mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); 366715ca08d3STrond Myklebust ret = nfsd4_verify_open_stid(&stp->st_stid); 366815ca08d3STrond Myklebust if (ret != nfs_ok) 366915ca08d3STrond Myklebust mutex_unlock(&stp->st_mutex); 367015ca08d3STrond Myklebust return ret; 367115ca08d3STrond Myklebust } 367215ca08d3STrond Myklebust 367315ca08d3STrond Myklebust static struct nfs4_ol_stateid * 367415ca08d3STrond Myklebust nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) 367515ca08d3STrond Myklebust { 367615ca08d3STrond Myklebust struct nfs4_ol_stateid *stp; 367715ca08d3STrond Myklebust for (;;) { 367815ca08d3STrond Myklebust spin_lock(&fp->fi_lock); 367915ca08d3STrond Myklebust stp = nfsd4_find_existing_open(fp, open); 368015ca08d3STrond Myklebust spin_unlock(&fp->fi_lock); 368115ca08d3STrond Myklebust if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) 368215ca08d3STrond Myklebust break; 368315ca08d3STrond Myklebust nfs4_put_stid(&stp->st_stid); 368415ca08d3STrond Myklebust } 368515ca08d3STrond Myklebust return stp; 368615ca08d3STrond Myklebust } 368715ca08d3STrond Myklebust 3688fe0750e5SJ. Bruce Fields static struct nfs4_openowner * 368913d6f66bSTrond Myklebust alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, 3690db24b3b4SJeff Layton struct nfsd4_compound_state *cstate) 3691db24b3b4SJeff Layton { 369213d6f66bSTrond Myklebust struct nfs4_client *clp = cstate->clp; 36937ffb5880STrond Myklebust struct nfs4_openowner *oo, *ret; 36941da177e4SLinus Torvalds 3695fe0750e5SJ. Bruce Fields oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); 3696fe0750e5SJ. Bruce Fields if (!oo) 36971da177e4SLinus Torvalds return NULL; 36986b180f0bSJeff Layton oo->oo_owner.so_ops = &openowner_ops; 3699fe0750e5SJ. Bruce Fields oo->oo_owner.so_is_open_owner = 1; 3700fe0750e5SJ. Bruce Fields oo->oo_owner.so_seqid = open->op_seqid; 3701d3134b10SJeff Layton oo->oo_flags = 0; 3702db24b3b4SJeff Layton if (nfsd4_has_session(cstate)) 3703db24b3b4SJeff Layton oo->oo_flags |= NFS4_OO_CONFIRMED; 3704fe0750e5SJ. Bruce Fields oo->oo_time = 0; 370538c387b5SJ. Bruce Fields oo->oo_last_closed_stid = NULL; 3706fe0750e5SJ. Bruce Fields INIT_LIST_HEAD(&oo->oo_close_lru); 3707d4f0489fSTrond Myklebust spin_lock(&clp->cl_lock); 3708d4f0489fSTrond Myklebust ret = find_openstateowner_str_locked(strhashval, open, clp); 37097ffb5880STrond Myklebust if (ret == NULL) { 3710fe0750e5SJ. Bruce Fields hash_openowner(oo, clp, strhashval); 37117ffb5880STrond Myklebust ret = oo; 37127ffb5880STrond Myklebust } else 3713d50ffdedSKinglong Mee nfs4_free_stateowner(&oo->oo_owner); 3714d50ffdedSKinglong Mee 3715d4f0489fSTrond Myklebust spin_unlock(&clp->cl_lock); 3716c5952338SJeff Layton return ret; 37171da177e4SLinus Torvalds } 37181da177e4SLinus Torvalds 37197fc0564eSAndrew Elble static struct nfs4_ol_stateid * 37208c7245abSOleg Drokin init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) 37217fc0564eSAndrew Elble { 37221da177e4SLinus Torvalds 37237fc0564eSAndrew Elble struct nfs4_openowner *oo = open->op_openowner; 37247fc0564eSAndrew Elble struct nfs4_ol_stateid *retstp = NULL; 37258c7245abSOleg Drokin struct nfs4_ol_stateid *stp; 37267fc0564eSAndrew Elble 37278c7245abSOleg Drokin stp = open->op_stp; 37285cc1fb2aSOleg Drokin /* We are moving these outside of the spinlocks to avoid the warnings */ 37295cc1fb2aSOleg Drokin mutex_init(&stp->st_mutex); 37304f34bd05SAndrew Elble mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 37315cc1fb2aSOleg Drokin 373215ca08d3STrond Myklebust retry: 37337fc0564eSAndrew Elble spin_lock(&oo->oo_owner.so_client->cl_lock); 37347fc0564eSAndrew Elble spin_lock(&fp->fi_lock); 37357fc0564eSAndrew Elble 37367fc0564eSAndrew Elble retstp = nfsd4_find_existing_open(fp, open); 37377fc0564eSAndrew Elble if (retstp) 37387fc0564eSAndrew Elble goto out_unlock; 37398c7245abSOleg Drokin 37408c7245abSOleg Drokin open->op_stp = NULL; 3741a15dfcd5SElena Reshetova refcount_inc(&stp->st_stid.sc_count); 37423abdb607SJ. Bruce Fields stp->st_stid.sc_type = NFS4_OPEN_STID; 37433c87b9b7STrond Myklebust INIT_LIST_HEAD(&stp->st_locks); 3744b5971afaSKinglong Mee stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); 374513cd2184SNeilBrown get_nfs4_file(fp); 374611b9164aSTrond Myklebust stp->st_stid.sc_file = fp; 37471da177e4SLinus Torvalds stp->st_access_bmap = 0; 37481da177e4SLinus Torvalds stp->st_deny_bmap = 0; 37494c4cd222SNeilBrown stp->st_openstp = NULL; 37501c755dc1SJeff Layton list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); 37511d31a253STrond Myklebust list_add(&stp->st_perfile, &fp->fi_stateids); 37527fc0564eSAndrew Elble 37537fc0564eSAndrew Elble out_unlock: 37541d31a253STrond Myklebust spin_unlock(&fp->fi_lock); 37551c755dc1SJeff Layton spin_unlock(&oo->oo_owner.so_client->cl_lock); 37565cc1fb2aSOleg Drokin if (retstp) { 375715ca08d3STrond Myklebust /* Handle races with CLOSE */ 375815ca08d3STrond Myklebust if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { 375915ca08d3STrond Myklebust nfs4_put_stid(&retstp->st_stid); 376015ca08d3STrond Myklebust goto retry; 376115ca08d3STrond Myklebust } 37628c7245abSOleg Drokin /* To keep mutex tracking happy */ 37635cc1fb2aSOleg Drokin mutex_unlock(&stp->st_mutex); 37648c7245abSOleg Drokin stp = retstp; 37655cc1fb2aSOleg Drokin } 37668c7245abSOleg Drokin return stp; 37671da177e4SLinus Torvalds } 37681da177e4SLinus Torvalds 3769d3134b10SJeff Layton /* 3770d3134b10SJeff Layton * In the 4.0 case we need to keep the owners around a little while to handle 3771d3134b10SJeff Layton * CLOSE replay. We still do need to release any file access that is held by 3772d3134b10SJeff Layton * them before returning however. 3773d3134b10SJeff Layton */ 37741da177e4SLinus Torvalds static void 3775d3134b10SJeff Layton move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) 37761da177e4SLinus Torvalds { 3777217526e7SJeff Layton struct nfs4_ol_stateid *last; 3778d3134b10SJeff Layton struct nfs4_openowner *oo = openowner(s->st_stateowner); 3779d3134b10SJeff Layton struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, 3780d3134b10SJeff Layton nfsd_net_id); 378173758fedSStanislav Kinsbursky 3782fe0750e5SJ. Bruce Fields dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); 37831da177e4SLinus Torvalds 3784b401be22SJeff Layton /* 3785b401be22SJeff Layton * We know that we hold one reference via nfsd4_close, and another 3786b401be22SJeff Layton * "persistent" reference for the client. If the refcount is higher 3787b401be22SJeff Layton * than 2, then there are still calls in progress that are using this 3788b401be22SJeff Layton * stateid. We can't put the sc_file reference until they are finished. 3789b401be22SJeff Layton * Wait for the refcount to drop to 2. Since it has been unhashed, 3790b401be22SJeff Layton * there should be no danger of the refcount going back up again at 3791b401be22SJeff Layton * this point. 3792b401be22SJeff Layton */ 3793a15dfcd5SElena Reshetova wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); 3794b401be22SJeff Layton 3795d3134b10SJeff Layton release_all_access(s); 3796d3134b10SJeff Layton if (s->st_stid.sc_file) { 3797d3134b10SJeff Layton put_nfs4_file(s->st_stid.sc_file); 3798d3134b10SJeff Layton s->st_stid.sc_file = NULL; 3799d3134b10SJeff Layton } 3800217526e7SJeff Layton 3801217526e7SJeff Layton spin_lock(&nn->client_lock); 3802217526e7SJeff Layton last = oo->oo_last_closed_stid; 3803d3134b10SJeff Layton oo->oo_last_closed_stid = s; 380473758fedSStanislav Kinsbursky list_move_tail(&oo->oo_close_lru, &nn->close_lru); 3805fe0750e5SJ. Bruce Fields oo->oo_time = get_seconds(); 3806217526e7SJeff Layton spin_unlock(&nn->client_lock); 3807217526e7SJeff Layton if (last) 3808217526e7SJeff Layton nfs4_put_stid(&last->st_stid); 38091da177e4SLinus Torvalds } 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds /* search file_hashtbl[] for file */ 38121da177e4SLinus Torvalds static struct nfs4_file * 38135b095e99SJeff Layton find_file_locked(struct knfsd_fh *fh, unsigned int hashval) 38141da177e4SLinus Torvalds { 38151da177e4SLinus Torvalds struct nfs4_file *fp; 38161da177e4SLinus Torvalds 38175b095e99SJeff Layton hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) { 38184d94c2efSChristoph Hellwig if (fh_match(&fp->fi_fhandle, fh)) { 3819818a34ebSElena Reshetova if (refcount_inc_not_zero(&fp->fi_ref)) 38201da177e4SLinus Torvalds return fp; 38211da177e4SLinus Torvalds } 382213cd2184SNeilBrown } 38231da177e4SLinus Torvalds return NULL; 38241da177e4SLinus Torvalds } 38251da177e4SLinus Torvalds 3826e6ba76e1SChristoph Hellwig struct nfs4_file * 3827ca943217STrond Myklebust find_file(struct knfsd_fh *fh) 3828950e0118STrond Myklebust { 3829950e0118STrond Myklebust struct nfs4_file *fp; 38305b095e99SJeff Layton unsigned int hashval = file_hashval(fh); 3831950e0118STrond Myklebust 38325b095e99SJeff Layton rcu_read_lock(); 38335b095e99SJeff Layton fp = find_file_locked(fh, hashval); 38345b095e99SJeff Layton rcu_read_unlock(); 3835950e0118STrond Myklebust return fp; 3836950e0118STrond Myklebust } 3837950e0118STrond Myklebust 3838950e0118STrond Myklebust static struct nfs4_file * 3839f9c00c3aSJeff Layton find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) 3840950e0118STrond Myklebust { 3841950e0118STrond Myklebust struct nfs4_file *fp; 38425b095e99SJeff Layton unsigned int hashval = file_hashval(fh); 38435b095e99SJeff Layton 38445b095e99SJeff Layton rcu_read_lock(); 38455b095e99SJeff Layton fp = find_file_locked(fh, hashval); 38465b095e99SJeff Layton rcu_read_unlock(); 38475b095e99SJeff Layton if (fp) 38485b095e99SJeff Layton return fp; 3849950e0118STrond Myklebust 3850950e0118STrond Myklebust spin_lock(&state_lock); 38515b095e99SJeff Layton fp = find_file_locked(fh, hashval); 38525b095e99SJeff Layton if (likely(fp == NULL)) { 38535b095e99SJeff Layton nfsd4_init_file(fh, hashval, new); 3854950e0118STrond Myklebust fp = new; 3855950e0118STrond Myklebust } 3856950e0118STrond Myklebust spin_unlock(&state_lock); 3857950e0118STrond Myklebust 3858950e0118STrond Myklebust return fp; 3859950e0118STrond Myklebust } 3860950e0118STrond Myklebust 38614f83aa30SJ. Bruce Fields /* 38621da177e4SLinus Torvalds * Called to check deny when READ with all zero stateid or 38631da177e4SLinus Torvalds * WRITE with all zero or all one stateid 38641da177e4SLinus Torvalds */ 3865b37ad28bSAl Viro static __be32 38661da177e4SLinus Torvalds nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) 38671da177e4SLinus Torvalds { 38681da177e4SLinus Torvalds struct nfs4_file *fp; 3869baeb4ff0SJeff Layton __be32 ret = nfs_ok; 38701da177e4SLinus Torvalds 3871ca943217STrond Myklebust fp = find_file(¤t_fh->fh_handle); 387213cd2184SNeilBrown if (!fp) 3873baeb4ff0SJeff Layton return ret; 3874baeb4ff0SJeff Layton /* Check for conflicting share reservations */ 38751d31a253STrond Myklebust spin_lock(&fp->fi_lock); 3876baeb4ff0SJeff Layton if (fp->fi_share_deny & deny_type) 3877baeb4ff0SJeff Layton ret = nfserr_locked; 38781d31a253STrond Myklebust spin_unlock(&fp->fi_lock); 387913cd2184SNeilBrown put_nfs4_file(fp); 388013cd2184SNeilBrown return ret; 38811da177e4SLinus Torvalds } 38821da177e4SLinus Torvalds 38830162ac2bSChristoph Hellwig static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) 38841da177e4SLinus Torvalds { 38850162ac2bSChristoph Hellwig struct nfs4_delegation *dp = cb_to_delegation(cb); 388611b9164aSTrond Myklebust struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, 388711b9164aSTrond Myklebust nfsd_net_id); 3888e8c69d17SJ. Bruce Fields 388911b9164aSTrond Myklebust block_delegations(&dp->dl_stid.sc_file->fi_fhandle); 3890f54fe962SJeff Layton 389102e1215fSJeff Layton /* 389202e1215fSJeff Layton * We can't do this in nfsd_break_deleg_cb because it is 3893f54fe962SJeff Layton * already holding inode->i_lock. 3894f54fe962SJeff Layton * 3895dff1399fSJeff Layton * If the dl_time != 0, then we know that it has already been 3896dff1399fSJeff Layton * queued for a lease break. Don't queue it again. 3897dff1399fSJeff Layton */ 3898f54fe962SJeff Layton spin_lock(&state_lock); 3899dff1399fSJeff Layton if (dp->dl_time == 0) { 39001da177e4SLinus Torvalds dp->dl_time = get_seconds(); 390102e1215fSJeff Layton list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); 390202e1215fSJeff Layton } 390302e1215fSJeff Layton spin_unlock(&state_lock); 3904dff1399fSJeff Layton } 39051da177e4SLinus Torvalds 39060162ac2bSChristoph Hellwig static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, 39070162ac2bSChristoph Hellwig struct rpc_task *task) 39080162ac2bSChristoph Hellwig { 39090162ac2bSChristoph Hellwig struct nfs4_delegation *dp = cb_to_delegation(cb); 39100162ac2bSChristoph Hellwig 3911a457974fSAndrew Elble if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) 3912a457974fSAndrew Elble return 1; 3913a457974fSAndrew Elble 39140162ac2bSChristoph Hellwig switch (task->tk_status) { 39150162ac2bSChristoph Hellwig case 0: 39160162ac2bSChristoph Hellwig return 1; 39170162ac2bSChristoph Hellwig case -EBADHANDLE: 39180162ac2bSChristoph Hellwig case -NFS4ERR_BAD_STATEID: 39190162ac2bSChristoph Hellwig /* 39200162ac2bSChristoph Hellwig * Race: client probably got cb_recall before open reply 39210162ac2bSChristoph Hellwig * granting delegation. 39220162ac2bSChristoph Hellwig */ 39230162ac2bSChristoph Hellwig if (dp->dl_retries--) { 39240162ac2bSChristoph Hellwig rpc_delay(task, 2 * HZ); 39250162ac2bSChristoph Hellwig return 0; 39260162ac2bSChristoph Hellwig } 39270162ac2bSChristoph Hellwig /*FALLTHRU*/ 39280162ac2bSChristoph Hellwig default: 39290162ac2bSChristoph Hellwig return -1; 39300162ac2bSChristoph Hellwig } 39310162ac2bSChristoph Hellwig } 39320162ac2bSChristoph Hellwig 39330162ac2bSChristoph Hellwig static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) 39340162ac2bSChristoph Hellwig { 39350162ac2bSChristoph Hellwig struct nfs4_delegation *dp = cb_to_delegation(cb); 39360162ac2bSChristoph Hellwig 39370162ac2bSChristoph Hellwig nfs4_put_stid(&dp->dl_stid); 39380162ac2bSChristoph Hellwig } 39390162ac2bSChristoph Hellwig 3940c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { 39410162ac2bSChristoph Hellwig .prepare = nfsd4_cb_recall_prepare, 39420162ac2bSChristoph Hellwig .done = nfsd4_cb_recall_done, 39430162ac2bSChristoph Hellwig .release = nfsd4_cb_recall_release, 39440162ac2bSChristoph Hellwig }; 39450162ac2bSChristoph Hellwig 394602e1215fSJeff Layton static void nfsd_break_one_deleg(struct nfs4_delegation *dp) 394702e1215fSJeff Layton { 394802e1215fSJeff Layton /* 394902e1215fSJeff Layton * We're assuming the state code never drops its reference 395002e1215fSJeff Layton * without first removing the lease. Since we're in this lease 39514a269efbSJ. Bruce Fields * callback (and since the lease code is serialized by the 39524a269efbSJ. Bruce Fields * i_lock) we know the server hasn't removed the lease yet, and 39534a269efbSJ. Bruce Fields * we know it's safe to take a reference. 395402e1215fSJeff Layton */ 3955a15dfcd5SElena Reshetova refcount_inc(&dp->dl_stid.sc_count); 3956f0b5de1bSChristoph Hellwig nfsd4_run_cb(&dp->dl_recall); 39576b57d9c8SJ. Bruce Fields } 39586b57d9c8SJ. Bruce Fields 39591c8c601aSJeff Layton /* Called from break_lease() with i_lock held. */ 39604d01b7f5SJeff Layton static bool 39614d01b7f5SJeff Layton nfsd_break_deleg_cb(struct file_lock *fl) 39626b57d9c8SJ. Bruce Fields { 39634d01b7f5SJeff Layton bool ret = false; 3964653e514eSJ. Bruce Fields struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; 3965653e514eSJ. Bruce Fields struct nfs4_file *fp = dp->dl_stid.sc_file; 39666b57d9c8SJ. Bruce Fields 39670272e1fdSJ. Bruce Fields /* 39680272e1fdSJ. Bruce Fields * We don't want the locks code to timeout the lease for us; 3969acfdf5c3SJ. Bruce Fields * we'll remove it ourself if a delegation isn't returned 39706b57d9c8SJ. Bruce Fields * in time: 39710272e1fdSJ. Bruce Fields */ 39720272e1fdSJ. Bruce Fields fl->fl_break_time = 0; 39731da177e4SLinus Torvalds 397402e1215fSJeff Layton spin_lock(&fp->fi_lock); 3975417c6629SJeff Layton fp->fi_had_conflict = true; 39765d926e8cSJ. Bruce Fields nfsd_break_one_deleg(dp); 397702e1215fSJeff Layton spin_unlock(&fp->fi_lock); 39784d01b7f5SJeff Layton return ret; 39791da177e4SLinus Torvalds } 39801da177e4SLinus Torvalds 3981c45198edSJeff Layton static int 39827448cc37SJeff Layton nfsd_change_deleg_cb(struct file_lock *onlist, int arg, 39837448cc37SJeff Layton struct list_head *dispose) 39841da177e4SLinus Torvalds { 39851da177e4SLinus Torvalds if (arg & F_UNLCK) 3986c45198edSJeff Layton return lease_modify(onlist, arg, dispose); 39871da177e4SLinus Torvalds else 39881da177e4SLinus Torvalds return -EAGAIN; 39891da177e4SLinus Torvalds } 39901da177e4SLinus Torvalds 39917b021967SAlexey Dobriyan static const struct lock_manager_operations nfsd_lease_mng_ops = { 39928fb47a4fSJ. Bruce Fields .lm_break = nfsd_break_deleg_cb, 39938fb47a4fSJ. Bruce Fields .lm_change = nfsd_change_deleg_cb, 39941da177e4SLinus Torvalds }; 39951da177e4SLinus Torvalds 39967a8711c9SJ. Bruce Fields static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) 39977a8711c9SJ. Bruce Fields { 39987a8711c9SJ. Bruce Fields if (nfsd4_has_session(cstate)) 39997a8711c9SJ. Bruce Fields return nfs_ok; 40007a8711c9SJ. Bruce Fields if (seqid == so->so_seqid - 1) 40017a8711c9SJ. Bruce Fields return nfserr_replay_me; 40027a8711c9SJ. Bruce Fields if (seqid == so->so_seqid) 40037a8711c9SJ. Bruce Fields return nfs_ok; 40047a8711c9SJ. Bruce Fields return nfserr_bad_seqid; 40057a8711c9SJ. Bruce Fields } 40061da177e4SLinus Torvalds 40074b24ca7dSJeff Layton static __be32 lookup_clientid(clientid_t *clid, 40084b24ca7dSJeff Layton struct nfsd4_compound_state *cstate, 40094b24ca7dSJeff Layton struct nfsd_net *nn) 40104b24ca7dSJeff Layton { 40114b24ca7dSJeff Layton struct nfs4_client *found; 40124b24ca7dSJeff Layton 40134b24ca7dSJeff Layton if (cstate->clp) { 40144b24ca7dSJeff Layton found = cstate->clp; 40154b24ca7dSJeff Layton if (!same_clid(&found->cl_clientid, clid)) 40164b24ca7dSJeff Layton return nfserr_stale_clientid; 40174b24ca7dSJeff Layton return nfs_ok; 40184b24ca7dSJeff Layton } 40194b24ca7dSJeff Layton 40204b24ca7dSJeff Layton if (STALE_CLIENTID(clid, nn)) 40214b24ca7dSJeff Layton return nfserr_stale_clientid; 40224b24ca7dSJeff Layton 40234b24ca7dSJeff Layton /* 40244b24ca7dSJeff Layton * For v4.1+ we get the client in the SEQUENCE op. If we don't have one 40254b24ca7dSJeff Layton * cached already then we know this is for is for v4.0 and "sessions" 40264b24ca7dSJeff Layton * will be false. 40274b24ca7dSJeff Layton */ 40284b24ca7dSJeff Layton WARN_ON_ONCE(cstate->session); 40293e339f96STrond Myklebust spin_lock(&nn->client_lock); 40304b24ca7dSJeff Layton found = find_confirmed_client(clid, false, nn); 40313e339f96STrond Myklebust if (!found) { 40323e339f96STrond Myklebust spin_unlock(&nn->client_lock); 40334b24ca7dSJeff Layton return nfserr_expired; 40343e339f96STrond Myklebust } 40353e339f96STrond Myklebust atomic_inc(&found->cl_refcount); 40363e339f96STrond Myklebust spin_unlock(&nn->client_lock); 40374b24ca7dSJeff Layton 40384b24ca7dSJeff Layton /* Cache the nfs4_client in cstate! */ 40394b24ca7dSJeff Layton cstate->clp = found; 40404b24ca7dSJeff Layton return nfs_ok; 40414b24ca7dSJeff Layton } 40424b24ca7dSJeff Layton 4043b37ad28bSAl Viro __be32 40446668958fSAndy Adamson nfsd4_process_open1(struct nfsd4_compound_state *cstate, 40453320fef1SStanislav Kinsbursky struct nfsd4_open *open, struct nfsd_net *nn) 40461da177e4SLinus Torvalds { 40471da177e4SLinus Torvalds clientid_t *clientid = &open->op_clientid; 40481da177e4SLinus Torvalds struct nfs4_client *clp = NULL; 40491da177e4SLinus Torvalds unsigned int strhashval; 4050fe0750e5SJ. Bruce Fields struct nfs4_openowner *oo = NULL; 40514cdc951bSJ. Bruce Fields __be32 status; 40521da177e4SLinus Torvalds 40532c142baaSStanislav Kinsbursky if (STALE_CLIENTID(&open->op_clientid, nn)) 40541da177e4SLinus Torvalds return nfserr_stale_clientid; 405532513b40SJ. Bruce Fields /* 405632513b40SJ. Bruce Fields * In case we need it later, after we've already created the 405732513b40SJ. Bruce Fields * file and don't want to risk a further failure: 405832513b40SJ. Bruce Fields */ 405932513b40SJ. Bruce Fields open->op_file = nfsd4_alloc_file(); 406032513b40SJ. Bruce Fields if (open->op_file == NULL) 406132513b40SJ. Bruce Fields return nfserr_jukebox; 40621da177e4SLinus Torvalds 406313d6f66bSTrond Myklebust status = lookup_clientid(clientid, cstate, nn); 406413d6f66bSTrond Myklebust if (status) 406513d6f66bSTrond Myklebust return status; 406613d6f66bSTrond Myklebust clp = cstate->clp; 40672d91e895STrond Myklebust 4068d4f0489fSTrond Myklebust strhashval = ownerstr_hashval(&open->op_owner); 4069d4f0489fSTrond Myklebust oo = find_openstateowner_str(strhashval, open, clp); 40702d91e895STrond Myklebust open->op_openowner = oo; 40712d91e895STrond Myklebust if (!oo) { 4072bcf130f9SJ. Bruce Fields goto new_owner; 40730f442aa2SJ. Bruce Fields } 4074dad1c067SJ. Bruce Fields if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { 40750f442aa2SJ. Bruce Fields /* Replace unconfirmed owners without checking for replay. */ 4076fe0750e5SJ. Bruce Fields release_openowner(oo); 4077fe0750e5SJ. Bruce Fields open->op_openowner = NULL; 4078bcf130f9SJ. Bruce Fields goto new_owner; 40790f442aa2SJ. Bruce Fields } 40804cdc951bSJ. Bruce Fields status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); 40814cdc951bSJ. Bruce Fields if (status) 40824cdc951bSJ. Bruce Fields return status; 40834cdc951bSJ. Bruce Fields goto alloc_stateid; 4084bcf130f9SJ. Bruce Fields new_owner: 408513d6f66bSTrond Myklebust oo = alloc_init_open_stateowner(strhashval, open, cstate); 4086fe0750e5SJ. Bruce Fields if (oo == NULL) 40873e772463SJ. Bruce Fields return nfserr_jukebox; 4088fe0750e5SJ. Bruce Fields open->op_openowner = oo; 40894cdc951bSJ. Bruce Fields alloc_stateid: 4090b49e084dSJeff Layton open->op_stp = nfs4_alloc_open_stateid(clp); 40914cdc951bSJ. Bruce Fields if (!open->op_stp) 40924cdc951bSJ. Bruce Fields return nfserr_jukebox; 40938287f009SSachin Bhamare 40948287f009SSachin Bhamare if (nfsd4_has_session(cstate) && 40958287f009SSachin Bhamare (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { 40968287f009SSachin Bhamare open->op_odstate = alloc_clnt_odstate(clp); 40978287f009SSachin Bhamare if (!open->op_odstate) 40988287f009SSachin Bhamare return nfserr_jukebox; 40998287f009SSachin Bhamare } 41008287f009SSachin Bhamare 41010f442aa2SJ. Bruce Fields return nfs_ok; 41021da177e4SLinus Torvalds } 41031da177e4SLinus Torvalds 4104b37ad28bSAl Viro static inline __be32 41054a6e43e6SNeilBrown nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) 41064a6e43e6SNeilBrown { 41074a6e43e6SNeilBrown if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) 41084a6e43e6SNeilBrown return nfserr_openmode; 41094a6e43e6SNeilBrown else 41104a6e43e6SNeilBrown return nfs_ok; 41114a6e43e6SNeilBrown } 41124a6e43e6SNeilBrown 4113c47d832bSDaniel Mack static int share_access_to_flags(u32 share_access) 411424a0111eSJ. Bruce Fields { 411524a0111eSJ. Bruce Fields return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; 411624a0111eSJ. Bruce Fields } 411724a0111eSJ. Bruce Fields 411838c2f4b1SJ. Bruce Fields static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s) 4119f459e453SJ. Bruce Fields { 4120f459e453SJ. Bruce Fields struct nfs4_stid *ret; 4121f459e453SJ. Bruce Fields 412295da1b3aSAndrew Elble ret = find_stateid_by_type(cl, s, 412395da1b3aSAndrew Elble NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID); 4124f459e453SJ. Bruce Fields if (!ret) 4125f459e453SJ. Bruce Fields return NULL; 4126f459e453SJ. Bruce Fields return delegstateid(ret); 4127f459e453SJ. Bruce Fields } 4128f459e453SJ. Bruce Fields 41298b289b2cSJ. Bruce Fields static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) 41308b289b2cSJ. Bruce Fields { 41318b289b2cSJ. Bruce Fields return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || 41328b289b2cSJ. Bruce Fields open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; 41338b289b2cSJ. Bruce Fields } 41348b289b2cSJ. Bruce Fields 4135b37ad28bSAl Viro static __be32 413641d22663SJ. Bruce Fields nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, 4137567d9829SNeilBrown struct nfs4_delegation **dp) 4138567d9829SNeilBrown { 4139567d9829SNeilBrown int flags; 4140b37ad28bSAl Viro __be32 status = nfserr_bad_stateid; 4141dcd94cc2STrond Myklebust struct nfs4_delegation *deleg; 4142567d9829SNeilBrown 4143dcd94cc2STrond Myklebust deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); 4144dcd94cc2STrond Myklebust if (deleg == NULL) 4145c44c5eebSNeilBrown goto out; 414695da1b3aSAndrew Elble if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) { 414795da1b3aSAndrew Elble nfs4_put_stid(&deleg->dl_stid); 414895da1b3aSAndrew Elble if (cl->cl_minorversion) 414995da1b3aSAndrew Elble status = nfserr_deleg_revoked; 415095da1b3aSAndrew Elble goto out; 415195da1b3aSAndrew Elble } 415224a0111eSJ. Bruce Fields flags = share_access_to_flags(open->op_share_access); 4153dcd94cc2STrond Myklebust status = nfs4_check_delegmode(deleg, flags); 4154dcd94cc2STrond Myklebust if (status) { 4155dcd94cc2STrond Myklebust nfs4_put_stid(&deleg->dl_stid); 4156dcd94cc2STrond Myklebust goto out; 4157dcd94cc2STrond Myklebust } 4158dcd94cc2STrond Myklebust *dp = deleg; 4159c44c5eebSNeilBrown out: 41608b289b2cSJ. Bruce Fields if (!nfsd4_is_deleg_cur(open)) 4161c44c5eebSNeilBrown return nfs_ok; 4162c44c5eebSNeilBrown if (status) 4163c44c5eebSNeilBrown return status; 4164dad1c067SJ. Bruce Fields open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 4165c44c5eebSNeilBrown return nfs_ok; 4166567d9829SNeilBrown } 4167567d9829SNeilBrown 416821fb4016SJ. Bruce Fields static inline int nfs4_access_to_access(u32 nfs4_access) 416921fb4016SJ. Bruce Fields { 417021fb4016SJ. Bruce Fields int flags = 0; 417121fb4016SJ. Bruce Fields 417221fb4016SJ. Bruce Fields if (nfs4_access & NFS4_SHARE_ACCESS_READ) 417321fb4016SJ. Bruce Fields flags |= NFSD_MAY_READ; 417421fb4016SJ. Bruce Fields if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) 417521fb4016SJ. Bruce Fields flags |= NFSD_MAY_WRITE; 417621fb4016SJ. Bruce Fields return flags; 417721fb4016SJ. Bruce Fields } 417821fb4016SJ. Bruce Fields 4179b37ad28bSAl Viro static inline __be32 41801da177e4SLinus Torvalds nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, 41811da177e4SLinus Torvalds struct nfsd4_open *open) 41821da177e4SLinus Torvalds { 41831da177e4SLinus Torvalds struct iattr iattr = { 41841da177e4SLinus Torvalds .ia_valid = ATTR_SIZE, 41851da177e4SLinus Torvalds .ia_size = 0, 41861da177e4SLinus Torvalds }; 41871da177e4SLinus Torvalds if (!open->op_truncate) 41881da177e4SLinus Torvalds return 0; 41891da177e4SLinus Torvalds if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) 41909246585aSAl Viro return nfserr_inval; 41911da177e4SLinus Torvalds return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); 41921da177e4SLinus Torvalds } 41931da177e4SLinus Torvalds 41947e6a72e5SChristoph Hellwig static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, 41956eb3a1d0SJeff Layton struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, 41966eb3a1d0SJeff Layton struct nfsd4_open *open) 41977e6a72e5SChristoph Hellwig { 4198de18643dSTrond Myklebust struct file *filp = NULL; 41997e6a72e5SChristoph Hellwig __be32 status; 42007e6a72e5SChristoph Hellwig int oflag = nfs4_access_to_omode(open->op_share_access); 42017e6a72e5SChristoph Hellwig int access = nfs4_access_to_access(open->op_share_access); 4202baeb4ff0SJeff Layton unsigned char old_access_bmap, old_deny_bmap; 42037e6a72e5SChristoph Hellwig 4204de18643dSTrond Myklebust spin_lock(&fp->fi_lock); 4205baeb4ff0SJeff Layton 4206baeb4ff0SJeff Layton /* 4207baeb4ff0SJeff Layton * Are we trying to set a deny mode that would conflict with 4208baeb4ff0SJeff Layton * current access? 4209baeb4ff0SJeff Layton */ 4210baeb4ff0SJeff Layton status = nfs4_file_check_deny(fp, open->op_share_deny); 4211baeb4ff0SJeff Layton if (status != nfs_ok) { 4212baeb4ff0SJeff Layton spin_unlock(&fp->fi_lock); 4213baeb4ff0SJeff Layton goto out; 4214baeb4ff0SJeff Layton } 4215baeb4ff0SJeff Layton 4216baeb4ff0SJeff Layton /* set access to the file */ 4217baeb4ff0SJeff Layton status = nfs4_file_get_access(fp, open->op_share_access); 4218baeb4ff0SJeff Layton if (status != nfs_ok) { 4219baeb4ff0SJeff Layton spin_unlock(&fp->fi_lock); 4220baeb4ff0SJeff Layton goto out; 4221baeb4ff0SJeff Layton } 4222baeb4ff0SJeff Layton 4223baeb4ff0SJeff Layton /* Set access bits in stateid */ 4224baeb4ff0SJeff Layton old_access_bmap = stp->st_access_bmap; 4225baeb4ff0SJeff Layton set_access(open->op_share_access, stp); 4226baeb4ff0SJeff Layton 4227baeb4ff0SJeff Layton /* Set new deny mask */ 4228baeb4ff0SJeff Layton old_deny_bmap = stp->st_deny_bmap; 4229baeb4ff0SJeff Layton set_deny(open->op_share_deny, stp); 4230baeb4ff0SJeff Layton fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); 4231baeb4ff0SJeff Layton 42327e6a72e5SChristoph Hellwig if (!fp->fi_fds[oflag]) { 4233de18643dSTrond Myklebust spin_unlock(&fp->fi_lock); 4234de18643dSTrond Myklebust status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); 42357e6a72e5SChristoph Hellwig if (status) 4236baeb4ff0SJeff Layton goto out_put_access; 4237de18643dSTrond Myklebust spin_lock(&fp->fi_lock); 4238de18643dSTrond Myklebust if (!fp->fi_fds[oflag]) { 4239de18643dSTrond Myklebust fp->fi_fds[oflag] = filp; 4240de18643dSTrond Myklebust filp = NULL; 4241de18643dSTrond Myklebust } 42427e6a72e5SChristoph Hellwig } 4243de18643dSTrond Myklebust spin_unlock(&fp->fi_lock); 4244de18643dSTrond Myklebust if (filp) 4245de18643dSTrond Myklebust fput(filp); 42467e6a72e5SChristoph Hellwig 42477e6a72e5SChristoph Hellwig status = nfsd4_truncate(rqstp, cur_fh, open); 42487e6a72e5SChristoph Hellwig if (status) 42497e6a72e5SChristoph Hellwig goto out_put_access; 42507e6a72e5SChristoph Hellwig out: 42517e6a72e5SChristoph Hellwig return status; 4252baeb4ff0SJeff Layton out_put_access: 4253baeb4ff0SJeff Layton stp->st_access_bmap = old_access_bmap; 4254baeb4ff0SJeff Layton nfs4_file_put_access(fp, open->op_share_access); 4255baeb4ff0SJeff Layton reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); 4256baeb4ff0SJeff Layton goto out; 42577e6a72e5SChristoph Hellwig } 42587e6a72e5SChristoph Hellwig 4259b37ad28bSAl Viro static __be32 4260dcef0413SJ. Bruce Fields nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open) 42611da177e4SLinus Torvalds { 4262b37ad28bSAl Viro __be32 status; 42636ac75368SArnd Bergmann unsigned char old_deny_bmap = stp->st_deny_bmap; 42641da177e4SLinus Torvalds 42656eb3a1d0SJeff Layton if (!test_access(open->op_share_access, stp)) 4266baeb4ff0SJeff Layton return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); 42677e6a72e5SChristoph Hellwig 4268baeb4ff0SJeff Layton /* test and set deny mode */ 4269baeb4ff0SJeff Layton spin_lock(&fp->fi_lock); 4270baeb4ff0SJeff Layton status = nfs4_file_check_deny(fp, open->op_share_deny); 4271baeb4ff0SJeff Layton if (status == nfs_ok) { 4272baeb4ff0SJeff Layton set_deny(open->op_share_deny, stp); 4273baeb4ff0SJeff Layton fp->fi_share_deny |= 4274baeb4ff0SJeff Layton (open->op_share_deny & NFS4_SHARE_DENY_BOTH); 42751da177e4SLinus Torvalds } 4276baeb4ff0SJeff Layton spin_unlock(&fp->fi_lock); 42771da177e4SLinus Torvalds 4278baeb4ff0SJeff Layton if (status != nfs_ok) 4279baeb4ff0SJeff Layton return status; 4280baeb4ff0SJeff Layton 4281baeb4ff0SJeff Layton status = nfsd4_truncate(rqstp, cur_fh, open); 4282baeb4ff0SJeff Layton if (status != nfs_ok) 4283baeb4ff0SJeff Layton reset_union_bmap_deny(old_deny_bmap, stp); 4284baeb4ff0SJeff Layton return status; 4285baeb4ff0SJeff Layton } 42861da177e4SLinus Torvalds 428714a24e99SJ. Bruce Fields /* Should we give out recallable state?: */ 428814a24e99SJ. Bruce Fields static bool nfsd4_cb_channel_good(struct nfs4_client *clp) 428914a24e99SJ. Bruce Fields { 429014a24e99SJ. Bruce Fields if (clp->cl_cb_state == NFSD4_CB_UP) 429114a24e99SJ. Bruce Fields return true; 429214a24e99SJ. Bruce Fields /* 429314a24e99SJ. Bruce Fields * In the sessions case, since we don't have to establish a 429414a24e99SJ. Bruce Fields * separate connection for callbacks, we assume it's OK 429514a24e99SJ. Bruce Fields * until we hear otherwise: 429614a24e99SJ. Bruce Fields */ 429714a24e99SJ. Bruce Fields return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; 429814a24e99SJ. Bruce Fields } 429914a24e99SJ. Bruce Fields 4300653e514eSJ. Bruce Fields static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, 4301653e514eSJ. Bruce Fields int flag) 430222d38c4cSJ. Bruce Fields { 430322d38c4cSJ. Bruce Fields struct file_lock *fl; 430422d38c4cSJ. Bruce Fields 430522d38c4cSJ. Bruce Fields fl = locks_alloc_lock(); 430622d38c4cSJ. Bruce Fields if (!fl) 430722d38c4cSJ. Bruce Fields return NULL; 430822d38c4cSJ. Bruce Fields fl->fl_lmops = &nfsd_lease_mng_ops; 4309617588d5SJ. Bruce Fields fl->fl_flags = FL_DELEG; 431022d38c4cSJ. Bruce Fields fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; 431122d38c4cSJ. Bruce Fields fl->fl_end = OFFSET_MAX; 4312653e514eSJ. Bruce Fields fl->fl_owner = (fl_owner_t)dp; 431322d38c4cSJ. Bruce Fields fl->fl_pid = current->tgid; 4314353601e7SJ. Bruce Fields fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file; 431522d38c4cSJ. Bruce Fields return fl; 431622d38c4cSJ. Bruce Fields } 431722d38c4cSJ. Bruce Fields 43180b26693cSJeff Layton static struct nfs4_delegation * 43190b26693cSJeff Layton nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, 43208287f009SSachin Bhamare struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) 4321acfdf5c3SJ. Bruce Fields { 432268b18f52SJ. Bruce Fields int status = 0; 43230b26693cSJeff Layton struct nfs4_delegation *dp; 4324353601e7SJ. Bruce Fields struct file *filp; 4325353601e7SJ. Bruce Fields struct file_lock *fl; 4326417c6629SJeff Layton 4327353601e7SJ. Bruce Fields /* 4328353601e7SJ. Bruce Fields * The fi_had_conflict and nfs_get_existing_delegation checks 4329353601e7SJ. Bruce Fields * here are just optimizations; we'll need to recheck them at 4330353601e7SJ. Bruce Fields * the end: 4331353601e7SJ. Bruce Fields */ 4332bf7bd3e9SJ. Bruce Fields if (fp->fi_had_conflict) 43330b26693cSJeff Layton return ERR_PTR(-EAGAIN); 43340b26693cSJeff Layton 4335353601e7SJ. Bruce Fields filp = find_readable_file(fp); 4336353601e7SJ. Bruce Fields if (!filp) { 4337353601e7SJ. Bruce Fields /* We should always have a readable file here */ 4338353601e7SJ. Bruce Fields WARN_ON_ONCE(1); 4339353601e7SJ. Bruce Fields return ERR_PTR(-EBADF); 4340353601e7SJ. Bruce Fields } 434134ed9872SAndrew Elble spin_lock(&state_lock); 434234ed9872SAndrew Elble spin_lock(&fp->fi_lock); 434368b18f52SJ. Bruce Fields if (nfs4_delegation_exists(clp, fp)) 434468b18f52SJ. Bruce Fields status = -EAGAIN; 4345353601e7SJ. Bruce Fields else if (!fp->fi_deleg_file) { 4346353601e7SJ. Bruce Fields fp->fi_deleg_file = filp; 4347353601e7SJ. Bruce Fields /* increment early to prevent fi_deleg_file from being 4348353601e7SJ. Bruce Fields * cleared */ 4349353601e7SJ. Bruce Fields fp->fi_delegees = 1; 4350353601e7SJ. Bruce Fields filp = NULL; 4351353601e7SJ. Bruce Fields } else 4352353601e7SJ. Bruce Fields fp->fi_delegees++; 4353353601e7SJ. Bruce Fields spin_unlock(&fp->fi_lock); 4354353601e7SJ. Bruce Fields spin_unlock(&state_lock); 4355353601e7SJ. Bruce Fields if (filp) 4356353601e7SJ. Bruce Fields fput(filp); 4357353601e7SJ. Bruce Fields if (status) 4358353601e7SJ. Bruce Fields return ERR_PTR(status); 4359353601e7SJ. Bruce Fields 4360353601e7SJ. Bruce Fields status = -ENOMEM; 4361353601e7SJ. Bruce Fields dp = alloc_init_deleg(clp, fp, fh, odstate); 4362353601e7SJ. Bruce Fields if (!dp) 4363353601e7SJ. Bruce Fields goto out_delegees; 4364353601e7SJ. Bruce Fields 4365353601e7SJ. Bruce Fields fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); 4366353601e7SJ. Bruce Fields if (!fl) 4367353601e7SJ. Bruce Fields goto out_stid; 4368353601e7SJ. Bruce Fields 4369353601e7SJ. Bruce Fields status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL); 4370353601e7SJ. Bruce Fields if (fl) 4371353601e7SJ. Bruce Fields locks_free_lock(fl); 4372353601e7SJ. Bruce Fields if (status) 4373353601e7SJ. Bruce Fields goto out_clnt_odstate; 4374353601e7SJ. Bruce Fields 4375353601e7SJ. Bruce Fields spin_lock(&state_lock); 4376353601e7SJ. Bruce Fields spin_lock(&fp->fi_lock); 4377353601e7SJ. Bruce Fields if (fp->fi_had_conflict) 4378353601e7SJ. Bruce Fields status = -EAGAIN; 4379353601e7SJ. Bruce Fields else 4380353601e7SJ. Bruce Fields status = hash_delegation_locked(dp, fp); 438134ed9872SAndrew Elble spin_unlock(&fp->fi_lock); 438234ed9872SAndrew Elble spin_unlock(&state_lock); 438334ed9872SAndrew Elble 438434ed9872SAndrew Elble if (status) 4385692ad280SAndrew Elble goto out_unlock; 4386692ad280SAndrew Elble 43870b26693cSJeff Layton return dp; 4388692ad280SAndrew Elble out_unlock: 4389692ad280SAndrew Elble vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp); 4390353601e7SJ. Bruce Fields out_clnt_odstate: 4391353601e7SJ. Bruce Fields put_clnt_odstate(dp->dl_clnt_odstate); 4392353601e7SJ. Bruce Fields out_stid: 4393353601e7SJ. Bruce Fields nfs4_put_stid(&dp->dl_stid); 4394353601e7SJ. Bruce Fields out_delegees: 4395353601e7SJ. Bruce Fields put_deleg_file(fp); 4396353601e7SJ. Bruce Fields return ERR_PTR(status); 4397edab9782SJ. Bruce Fields } 4398edab9782SJ. Bruce Fields 43994aa8913cSBenny Halevy static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) 44004aa8913cSBenny Halevy { 44014aa8913cSBenny Halevy open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; 44024aa8913cSBenny Halevy if (status == -EAGAIN) 44034aa8913cSBenny Halevy open->op_why_no_deleg = WND4_CONTENTION; 44044aa8913cSBenny Halevy else { 44054aa8913cSBenny Halevy open->op_why_no_deleg = WND4_RESOURCE; 44064aa8913cSBenny Halevy switch (open->op_deleg_want) { 44074aa8913cSBenny Halevy case NFS4_SHARE_WANT_READ_DELEG: 44084aa8913cSBenny Halevy case NFS4_SHARE_WANT_WRITE_DELEG: 44094aa8913cSBenny Halevy case NFS4_SHARE_WANT_ANY_DELEG: 44104aa8913cSBenny Halevy break; 44114aa8913cSBenny Halevy case NFS4_SHARE_WANT_CANCEL: 44124aa8913cSBenny Halevy open->op_why_no_deleg = WND4_CANCELLED; 44134aa8913cSBenny Halevy break; 44144aa8913cSBenny Halevy case NFS4_SHARE_WANT_NO_DELEG: 4415063b0fb9SJ. Bruce Fields WARN_ON_ONCE(1); 44164aa8913cSBenny Halevy } 44174aa8913cSBenny Halevy } 44184aa8913cSBenny Halevy } 44194aa8913cSBenny Halevy 44201da177e4SLinus Torvalds /* 44211da177e4SLinus Torvalds * Attempt to hand out a delegation. 442299c41515SJ. Bruce Fields * 442399c41515SJ. Bruce Fields * Note we don't support write delegations, and won't until the vfs has 442499c41515SJ. Bruce Fields * proper support for them. 44251da177e4SLinus Torvalds */ 44261da177e4SLinus Torvalds static void 44274cf59221SJeff Layton nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, 44284cf59221SJeff Layton struct nfs4_ol_stateid *stp) 44291da177e4SLinus Torvalds { 44301da177e4SLinus Torvalds struct nfs4_delegation *dp; 44314cf59221SJeff Layton struct nfs4_openowner *oo = openowner(stp->st_stateowner); 44324cf59221SJeff Layton struct nfs4_client *clp = stp->st_stid.sc_client; 443314a24e99SJ. Bruce Fields int cb_up; 443499c41515SJ. Bruce Fields int status = 0; 44351da177e4SLinus Torvalds 4436fe0750e5SJ. Bruce Fields cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); 44377b190fecSNeilBrown open->op_recall = 0; 44387b190fecSNeilBrown switch (open->op_claim_type) { 44397b190fecSNeilBrown case NFS4_OPEN_CLAIM_PREVIOUS: 44402bf23875SJ. Bruce Fields if (!cb_up) 44417b190fecSNeilBrown open->op_recall = 1; 444299c41515SJ. Bruce Fields if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) 444399c41515SJ. Bruce Fields goto out_no_deleg; 44447b190fecSNeilBrown break; 44457b190fecSNeilBrown case NFS4_OPEN_CLAIM_NULL: 4446ed47b062SMing Chen case NFS4_OPEN_CLAIM_FH: 444799c41515SJ. Bruce Fields /* 444899c41515SJ. Bruce Fields * Let's not give out any delegations till everyone's 4449c87fb4a3SJ. Bruce Fields * had the chance to reclaim theirs, *and* until 4450c87fb4a3SJ. Bruce Fields * NLM locks have all been reclaimed: 445199c41515SJ. Bruce Fields */ 44524cf59221SJeff Layton if (locks_in_grace(clp->net)) 445399c41515SJ. Bruce Fields goto out_no_deleg; 4454dad1c067SJ. Bruce Fields if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) 445599c41515SJ. Bruce Fields goto out_no_deleg; 44569a0590aeSSteve Dickson /* 44579a0590aeSSteve Dickson * Also, if the file was opened for write or 44589a0590aeSSteve Dickson * create, there's a good chance the client's 44599a0590aeSSteve Dickson * about to write to it, resulting in an 44609a0590aeSSteve Dickson * immediate recall (since we don't support 44619a0590aeSSteve Dickson * write delegations): 44629a0590aeSSteve Dickson */ 44631da177e4SLinus Torvalds if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) 446499c41515SJ. Bruce Fields goto out_no_deleg; 446599c41515SJ. Bruce Fields if (open->op_create == NFS4_OPEN_CREATE) 446699c41515SJ. Bruce Fields goto out_no_deleg; 44677b190fecSNeilBrown break; 44687b190fecSNeilBrown default: 446999c41515SJ. Bruce Fields goto out_no_deleg; 44707b190fecSNeilBrown } 44718287f009SSachin Bhamare dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate); 44720b26693cSJeff Layton if (IS_ERR(dp)) 4473dd239cc0SJ. Bruce Fields goto out_no_deleg; 44741da177e4SLinus Torvalds 4475d5477a8dSJ. Bruce Fields memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); 44761da177e4SLinus Torvalds 44778c10cbdbSBenny Halevy dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", 4478d5477a8dSJ. Bruce Fields STATEID_VAL(&dp->dl_stid.sc_stateid)); 447999c41515SJ. Bruce Fields open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; 448067cb1279STrond Myklebust nfs4_put_stid(&dp->dl_stid); 4481dd239cc0SJ. Bruce Fields return; 4482dd239cc0SJ. Bruce Fields out_no_deleg: 448399c41515SJ. Bruce Fields open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; 44847b190fecSNeilBrown if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && 4485d08d32e6SJ. Bruce Fields open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { 44861da177e4SLinus Torvalds dprintk("NFSD: WARNING: refusing delegation reclaim\n"); 4487d08d32e6SJ. Bruce Fields open->op_recall = 1; 4488d08d32e6SJ. Bruce Fields } 4489dd239cc0SJ. Bruce Fields 4490dd239cc0SJ. Bruce Fields /* 4.1 client asking for a delegation? */ 4491dd239cc0SJ. Bruce Fields if (open->op_deleg_want) 4492dd239cc0SJ. Bruce Fields nfsd4_open_deleg_none_ext(open, status); 4493dd239cc0SJ. Bruce Fields return; 44941da177e4SLinus Torvalds } 44951da177e4SLinus Torvalds 4496e27f49c3SBenny Halevy static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, 4497e27f49c3SBenny Halevy struct nfs4_delegation *dp) 4498e27f49c3SBenny Halevy { 4499e27f49c3SBenny Halevy if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && 4500e27f49c3SBenny Halevy dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { 4501e27f49c3SBenny Halevy open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; 4502e27f49c3SBenny Halevy open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; 4503e27f49c3SBenny Halevy } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && 4504e27f49c3SBenny Halevy dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { 4505e27f49c3SBenny Halevy open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; 4506e27f49c3SBenny Halevy open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; 4507e27f49c3SBenny Halevy } 4508e27f49c3SBenny Halevy /* Otherwise the client must be confused wanting a delegation 4509e27f49c3SBenny Halevy * it already has, therefore we don't return 4510e27f49c3SBenny Halevy * NFS4_OPEN_DELEGATE_NONE_EXT and reason. 4511e27f49c3SBenny Halevy */ 4512e27f49c3SBenny Halevy } 4513e27f49c3SBenny Halevy 4514b37ad28bSAl Viro __be32 45151da177e4SLinus Torvalds nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) 45161da177e4SLinus Torvalds { 45176668958fSAndy Adamson struct nfsd4_compoundres *resp = rqstp->rq_resp; 451838c2f4b1SJ. Bruce Fields struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; 45191da177e4SLinus Torvalds struct nfs4_file *fp = NULL; 4520dcef0413SJ. Bruce Fields struct nfs4_ol_stateid *stp = NULL; 4521567d9829SNeilBrown struct nfs4_delegation *dp = NULL; 4522b37ad28bSAl Viro __be32 status; 4523d8a1a000STrond Myklebust bool new_stp = false; 45241da177e4SLinus Torvalds 45251da177e4SLinus Torvalds /* 45261da177e4SLinus Torvalds * Lookup file; if found, lookup stateid and check open request, 45271da177e4SLinus Torvalds * and check for delegations in the process of being recalled. 45281da177e4SLinus Torvalds * If not found, create the nfs4_file struct 45291da177e4SLinus Torvalds */ 4530f9c00c3aSJeff Layton fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); 4531950e0118STrond Myklebust if (fp != open->op_file) { 453241d22663SJ. Bruce Fields status = nfs4_check_deleg(cl, open, &dp); 4533c44c5eebSNeilBrown if (status) 4534c44c5eebSNeilBrown goto out; 453515ca08d3STrond Myklebust stp = nfsd4_find_and_lock_existing_open(fp, open); 45361da177e4SLinus Torvalds } else { 4537950e0118STrond Myklebust open->op_file = NULL; 4538c44c5eebSNeilBrown status = nfserr_bad_stateid; 45398b289b2cSJ. Bruce Fields if (nfsd4_is_deleg_cur(open)) 4540c44c5eebSNeilBrown goto out; 45411da177e4SLinus Torvalds } 45421da177e4SLinus Torvalds 4543d8a1a000STrond Myklebust if (!stp) { 4544d8a1a000STrond Myklebust stp = init_open_stateid(fp, open); 4545d8a1a000STrond Myklebust if (!open->op_stp) 4546d8a1a000STrond Myklebust new_stp = true; 4547d8a1a000STrond Myklebust } 4548d8a1a000STrond Myklebust 45491da177e4SLinus Torvalds /* 45501da177e4SLinus Torvalds * OPEN the file, or upgrade an existing OPEN. 45511da177e4SLinus Torvalds * If truncate fails, the OPEN fails. 4552d8a1a000STrond Myklebust * 4553d8a1a000STrond Myklebust * stp is already locked. 45541da177e4SLinus Torvalds */ 4555d8a1a000STrond Myklebust if (!new_stp) { 45561da177e4SLinus Torvalds /* Stateid was found, this is an OPEN upgrade */ 4557f9d7562fSJ. Bruce Fields status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); 455835a92fe8SJeff Layton if (status) { 4559feb9dad5SOleg Drokin mutex_unlock(&stp->st_mutex); 45601da177e4SLinus Torvalds goto out; 456135a92fe8SJeff Layton } 45621da177e4SLinus Torvalds } else { 45636eb3a1d0SJeff Layton status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); 45646eb3a1d0SJeff Layton if (status) { 4565d8a1a000STrond Myklebust stp->st_stid.sc_type = NFS4_CLOSED_STID; 45666eb3a1d0SJeff Layton release_open_stateid(stp); 4567d8a1a000STrond Myklebust mutex_unlock(&stp->st_mutex); 45686eb3a1d0SJeff Layton goto out; 45696eb3a1d0SJeff Layton } 45708287f009SSachin Bhamare 45718287f009SSachin Bhamare stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, 45728287f009SSachin Bhamare open->op_odstate); 45738287f009SSachin Bhamare if (stp->st_clnt_odstate == open->op_odstate) 45748287f009SSachin Bhamare open->op_odstate = NULL; 45751da177e4SLinus Torvalds } 4576d8a1a000STrond Myklebust 45779767feb2SJeff Layton nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); 4578feb9dad5SOleg Drokin mutex_unlock(&stp->st_mutex); 45791da177e4SLinus Torvalds 4580d24433cdSBenny Halevy if (nfsd4_has_session(&resp->cstate)) { 4581d24433cdSBenny Halevy if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { 4582d24433cdSBenny Halevy open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; 4583d24433cdSBenny Halevy open->op_why_no_deleg = WND4_NOT_WANTED; 4584d24433cdSBenny Halevy goto nodeleg; 4585d24433cdSBenny Halevy } 4586d24433cdSBenny Halevy } 4587d24433cdSBenny Halevy 45881da177e4SLinus Torvalds /* 45891da177e4SLinus Torvalds * Attempt to hand out a delegation. No error return, because the 45901da177e4SLinus Torvalds * OPEN succeeds even if we fail. 45911da177e4SLinus Torvalds */ 45924cf59221SJeff Layton nfs4_open_delegation(current_fh, open, stp); 4593d24433cdSBenny Halevy nodeleg: 45941da177e4SLinus Torvalds status = nfs_ok; 45951da177e4SLinus Torvalds 45968c10cbdbSBenny Halevy dprintk("%s: stateid=" STATEID_FMT "\n", __func__, 4597dcef0413SJ. Bruce Fields STATEID_VAL(&stp->st_stid.sc_stateid)); 45981da177e4SLinus Torvalds out: 4599d24433cdSBenny Halevy /* 4.1 client trying to upgrade/downgrade delegation? */ 4600d24433cdSBenny Halevy if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && 4601e27f49c3SBenny Halevy open->op_deleg_want) 4602e27f49c3SBenny Halevy nfsd4_deleg_xgrade_none_ext(open, dp); 4603d24433cdSBenny Halevy 460413cd2184SNeilBrown if (fp) 460513cd2184SNeilBrown put_nfs4_file(fp); 460637515177SNeilBrown if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) 460787186022SKinglong Mee open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; 46081da177e4SLinus Torvalds /* 46091da177e4SLinus Torvalds * To finish the open response, we just need to set the rflags. 46101da177e4SLinus Torvalds */ 46111da177e4SLinus Torvalds open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; 461219e4c347SJeff Layton if (nfsd4_has_session(&resp->cstate)) 461319e4c347SJeff Layton open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; 461419e4c347SJeff Layton else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) 46151da177e4SLinus Torvalds open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; 461619e4c347SJeff Layton 4617dcd94cc2STrond Myklebust if (dp) 4618dcd94cc2STrond Myklebust nfs4_put_stid(&dp->dl_stid); 4619d6f2bc5dSTrond Myklebust if (stp) 4620d6f2bc5dSTrond Myklebust nfs4_put_stid(&stp->st_stid); 46211da177e4SLinus Torvalds 46221da177e4SLinus Torvalds return status; 46231da177e4SLinus Torvalds } 46241da177e4SLinus Torvalds 462558fb12e6SJeff Layton void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, 462642297899SJeff Layton struct nfsd4_open *open) 4627d29b20cdSJ. Bruce Fields { 4628d29b20cdSJ. Bruce Fields if (open->op_openowner) { 4629d3134b10SJeff Layton struct nfs4_stateowner *so = &open->op_openowner->oo_owner; 4630d29b20cdSJ. Bruce Fields 4631d3134b10SJeff Layton nfsd4_cstate_assign_replay(cstate, so); 4632d3134b10SJeff Layton nfs4_put_stateowner(so); 4633d29b20cdSJ. Bruce Fields } 463432513b40SJ. Bruce Fields if (open->op_file) 46355b095e99SJeff Layton kmem_cache_free(file_slab, open->op_file); 46364cdc951bSJ. Bruce Fields if (open->op_stp) 46376011695dSTrond Myklebust nfs4_put_stid(&open->op_stp->st_stid); 46388287f009SSachin Bhamare if (open->op_odstate) 46398287f009SSachin Bhamare kmem_cache_free(odstate_slab, open->op_odstate); 4640d29b20cdSJ. Bruce Fields } 4641d29b20cdSJ. Bruce Fields 4642b37ad28bSAl Viro __be32 4643b591480bSJ.Bruce Fields nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 4644eb69853dSChristoph Hellwig union nfsd4_op_u *u) 46451da177e4SLinus Torvalds { 4646eb69853dSChristoph Hellwig clientid_t *clid = &u->renew; 46471da177e4SLinus Torvalds struct nfs4_client *clp; 4648b37ad28bSAl Viro __be32 status; 46497f2210faSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 46501da177e4SLinus Torvalds 46511da177e4SLinus Torvalds dprintk("process_renew(%08x/%08x): starting\n", 46521da177e4SLinus Torvalds clid->cl_boot, clid->cl_id); 46534b24ca7dSJeff Layton status = lookup_clientid(clid, cstate, nn); 46549b2ef62bSJ. Bruce Fields if (status) 46551da177e4SLinus Torvalds goto out; 46564b24ca7dSJeff Layton clp = cstate->clp; 46571da177e4SLinus Torvalds status = nfserr_cb_path_down; 4658ea1da636SNeilBrown if (!list_empty(&clp->cl_delegations) 465977a3569dSJ. Bruce Fields && clp->cl_cb_state != NFSD4_CB_UP) 46601da177e4SLinus Torvalds goto out; 46611da177e4SLinus Torvalds status = nfs_ok; 46621da177e4SLinus Torvalds out: 46631da177e4SLinus Torvalds return status; 46641da177e4SLinus Torvalds } 46651da177e4SLinus Torvalds 46667f5ef2e9SJeff Layton void 466712760c66SStanislav Kinsbursky nfsd4_end_grace(struct nfsd_net *nn) 4668a76b4319SNeilBrown { 466933dcc481SJeff Layton /* do nothing if grace period already ended */ 4670a51c84edSStanislav Kinsbursky if (nn->grace_ended) 467133dcc481SJeff Layton return; 467233dcc481SJeff Layton 4673a76b4319SNeilBrown dprintk("NFSD: end of grace period\n"); 4674a51c84edSStanislav Kinsbursky nn->grace_ended = true; 467570b28235SJ. Bruce Fields /* 467670b28235SJ. Bruce Fields * If the server goes down again right now, an NFSv4 467770b28235SJ. Bruce Fields * client will still be allowed to reclaim after it comes back up, 467870b28235SJ. Bruce Fields * even if it hasn't yet had a chance to reclaim state this time. 467970b28235SJ. Bruce Fields * 468070b28235SJ. Bruce Fields */ 4681919b8049SJeff Layton nfsd4_record_grace_done(nn); 468270b28235SJ. Bruce Fields /* 468370b28235SJ. Bruce Fields * At this point, NFSv4 clients can still reclaim. But if the 468470b28235SJ. Bruce Fields * server crashes, any that have not yet reclaimed will be out 468570b28235SJ. Bruce Fields * of luck on the next boot. 468670b28235SJ. Bruce Fields * 468770b28235SJ. Bruce Fields * (NFSv4.1+ clients are considered to have reclaimed once they 468870b28235SJ. Bruce Fields * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to 468970b28235SJ. Bruce Fields * have reclaimed after their first OPEN.) 469070b28235SJ. Bruce Fields */ 46915e1533c7SStanislav Kinsbursky locks_end_grace(&nn->nfsd4_manager); 469270b28235SJ. Bruce Fields /* 469370b28235SJ. Bruce Fields * At this point, and once lockd and/or any other containers 469470b28235SJ. Bruce Fields * exit their grace period, further reclaims will fail and 469570b28235SJ. Bruce Fields * regular locking can resume. 469670b28235SJ. Bruce Fields */ 4697a76b4319SNeilBrown } 4698a76b4319SNeilBrown 469903f318caSJ. Bruce Fields /* 470003f318caSJ. Bruce Fields * If we've waited a lease period but there are still clients trying to 470103f318caSJ. Bruce Fields * reclaim, wait a little longer to give them a chance to finish. 470203f318caSJ. Bruce Fields */ 470303f318caSJ. Bruce Fields static bool clients_still_reclaiming(struct nfsd_net *nn) 470403f318caSJ. Bruce Fields { 470503f318caSJ. Bruce Fields unsigned long now = get_seconds(); 470603f318caSJ. Bruce Fields unsigned long double_grace_period_end = nn->boot_time + 470703f318caSJ. Bruce Fields 2 * nn->nfsd4_lease; 470803f318caSJ. Bruce Fields 470903f318caSJ. Bruce Fields if (!nn->somebody_reclaimed) 471003f318caSJ. Bruce Fields return false; 471103f318caSJ. Bruce Fields nn->somebody_reclaimed = false; 471203f318caSJ. Bruce Fields /* 471303f318caSJ. Bruce Fields * If we've given them *two* lease times to reclaim, and they're 471403f318caSJ. Bruce Fields * still not done, give up: 471503f318caSJ. Bruce Fields */ 471603f318caSJ. Bruce Fields if (time_after(now, double_grace_period_end)) 471703f318caSJ. Bruce Fields return false; 471803f318caSJ. Bruce Fields return true; 471903f318caSJ. Bruce Fields } 472003f318caSJ. Bruce Fields 4721fd39ca9aSNeilBrown static time_t 472209121281SStanislav Kinsbursky nfs4_laundromat(struct nfsd_net *nn) 47231da177e4SLinus Torvalds { 47241da177e4SLinus Torvalds struct nfs4_client *clp; 4725fe0750e5SJ. Bruce Fields struct nfs4_openowner *oo; 47261da177e4SLinus Torvalds struct nfs4_delegation *dp; 4727217526e7SJeff Layton struct nfs4_ol_stateid *stp; 47287919d0a2SJeff Layton struct nfsd4_blocked_lock *nbl; 47291da177e4SLinus Torvalds struct list_head *pos, *next, reaplist; 47303d733711SStanislav Kinsbursky time_t cutoff = get_seconds() - nn->nfsd4_lease; 4731a832e7aeSJeff Layton time_t t, new_timeo = nn->nfsd4_lease; 47321da177e4SLinus Torvalds 47331da177e4SLinus Torvalds dprintk("NFSD: laundromat service - starting\n"); 473403f318caSJ. Bruce Fields 473503f318caSJ. Bruce Fields if (clients_still_reclaiming(nn)) { 473603f318caSJ. Bruce Fields new_timeo = 0; 473703f318caSJ. Bruce Fields goto out; 473803f318caSJ. Bruce Fields } 473912760c66SStanislav Kinsbursky nfsd4_end_grace(nn); 474036acb66bSBenny Halevy INIT_LIST_HEAD(&reaplist); 4741c9a49628SStanislav Kinsbursky spin_lock(&nn->client_lock); 47425ed58bb2SStanislav Kinsbursky list_for_each_safe(pos, next, &nn->client_lru) { 47431da177e4SLinus Torvalds clp = list_entry(pos, struct nfs4_client, cl_lru); 47441da177e4SLinus Torvalds if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { 47451da177e4SLinus Torvalds t = clp->cl_time - cutoff; 4746a832e7aeSJeff Layton new_timeo = min(new_timeo, t); 47471da177e4SLinus Torvalds break; 47481da177e4SLinus Torvalds } 4749221a6876SJ. Bruce Fields if (mark_client_expired_locked(clp)) { 4750d7682988SBenny Halevy dprintk("NFSD: client in use (clientid %08x)\n", 4751d7682988SBenny Halevy clp->cl_clientid.cl_id); 4752d7682988SBenny Halevy continue; 4753d7682988SBenny Halevy } 47544864af97STrond Myklebust list_add(&clp->cl_lru, &reaplist); 475536acb66bSBenny Halevy } 4756c9a49628SStanislav Kinsbursky spin_unlock(&nn->client_lock); 475736acb66bSBenny Halevy list_for_each_safe(pos, next, &reaplist) { 475836acb66bSBenny Halevy clp = list_entry(pos, struct nfs4_client, cl_lru); 47591da177e4SLinus Torvalds dprintk("NFSD: purging unused client (clientid %08x)\n", 47601da177e4SLinus Torvalds clp->cl_clientid.cl_id); 47614864af97STrond Myklebust list_del_init(&clp->cl_lru); 47621da177e4SLinus Torvalds expire_client(clp); 47631da177e4SLinus Torvalds } 4764cdc97505SBenny Halevy spin_lock(&state_lock); 4765e8c69d17SJ. Bruce Fields list_for_each_safe(pos, next, &nn->del_recall_lru) { 47661da177e4SLinus Torvalds dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 47671da177e4SLinus Torvalds if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { 4768a832e7aeSJeff Layton t = dp->dl_time - cutoff; 4769a832e7aeSJeff Layton new_timeo = min(new_timeo, t); 47701da177e4SLinus Torvalds break; 47711da177e4SLinus Torvalds } 47723fcbbd24SJeff Layton WARN_ON(!unhash_delegation_locked(dp)); 477342690676SJeff Layton list_add(&dp->dl_recall_lru, &reaplist); 47741da177e4SLinus Torvalds } 4775cdc97505SBenny Halevy spin_unlock(&state_lock); 47762d4a532dSJeff Layton while (!list_empty(&reaplist)) { 47772d4a532dSJeff Layton dp = list_first_entry(&reaplist, struct nfs4_delegation, 47782d4a532dSJeff Layton dl_recall_lru); 47792d4a532dSJeff Layton list_del_init(&dp->dl_recall_lru); 47803bd64a5bSJ. Bruce Fields revoke_delegation(dp); 47811da177e4SLinus Torvalds } 4782217526e7SJeff Layton 4783217526e7SJeff Layton spin_lock(&nn->client_lock); 4784217526e7SJeff Layton while (!list_empty(&nn->close_lru)) { 4785217526e7SJeff Layton oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, 4786217526e7SJeff Layton oo_close_lru); 4787217526e7SJeff Layton if (time_after((unsigned long)oo->oo_time, 4788217526e7SJeff Layton (unsigned long)cutoff)) { 4789a832e7aeSJeff Layton t = oo->oo_time - cutoff; 4790a832e7aeSJeff Layton new_timeo = min(new_timeo, t); 47911da177e4SLinus Torvalds break; 47921da177e4SLinus Torvalds } 4793217526e7SJeff Layton list_del_init(&oo->oo_close_lru); 4794217526e7SJeff Layton stp = oo->oo_last_closed_stid; 4795217526e7SJeff Layton oo->oo_last_closed_stid = NULL; 4796217526e7SJeff Layton spin_unlock(&nn->client_lock); 4797217526e7SJeff Layton nfs4_put_stid(&stp->st_stid); 4798217526e7SJeff Layton spin_lock(&nn->client_lock); 47991da177e4SLinus Torvalds } 4800217526e7SJeff Layton spin_unlock(&nn->client_lock); 4801217526e7SJeff Layton 48027919d0a2SJeff Layton /* 48037919d0a2SJeff Layton * It's possible for a client to try and acquire an already held lock 48047919d0a2SJeff Layton * that is being held for a long time, and then lose interest in it. 48057919d0a2SJeff Layton * So, we clean out any un-revisited request after a lease period 48067919d0a2SJeff Layton * under the assumption that the client is no longer interested. 48077919d0a2SJeff Layton * 48087919d0a2SJeff Layton * RFC5661, sec. 9.6 states that the client must not rely on getting 48097919d0a2SJeff Layton * notifications and must continue to poll for locks, even when the 48107919d0a2SJeff Layton * server supports them. Thus this shouldn't lead to clients blocking 48117919d0a2SJeff Layton * indefinitely once the lock does become free. 48127919d0a2SJeff Layton */ 48137919d0a2SJeff Layton BUG_ON(!list_empty(&reaplist)); 48140cc11a61SJeff Layton spin_lock(&nn->blocked_locks_lock); 48157919d0a2SJeff Layton while (!list_empty(&nn->blocked_locks_lru)) { 48167919d0a2SJeff Layton nbl = list_first_entry(&nn->blocked_locks_lru, 48177919d0a2SJeff Layton struct nfsd4_blocked_lock, nbl_lru); 48187919d0a2SJeff Layton if (time_after((unsigned long)nbl->nbl_time, 48197919d0a2SJeff Layton (unsigned long)cutoff)) { 48207919d0a2SJeff Layton t = nbl->nbl_time - cutoff; 48217919d0a2SJeff Layton new_timeo = min(new_timeo, t); 48227919d0a2SJeff Layton break; 48237919d0a2SJeff Layton } 48247919d0a2SJeff Layton list_move(&nbl->nbl_lru, &reaplist); 48257919d0a2SJeff Layton list_del_init(&nbl->nbl_list); 48267919d0a2SJeff Layton } 48270cc11a61SJeff Layton spin_unlock(&nn->blocked_locks_lock); 48287919d0a2SJeff Layton 48297919d0a2SJeff Layton while (!list_empty(&reaplist)) { 483064ebe124SNaofumi Honda nbl = list_first_entry(&reaplist, 48317919d0a2SJeff Layton struct nfsd4_blocked_lock, nbl_lru); 48327919d0a2SJeff Layton list_del_init(&nbl->nbl_lru); 48337919d0a2SJeff Layton posix_unblock_lock(&nbl->nbl_lock); 48347919d0a2SJeff Layton free_blocked_lock(nbl); 48357919d0a2SJeff Layton } 483603f318caSJ. Bruce Fields out: 4837a832e7aeSJeff Layton new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); 4838a832e7aeSJeff Layton return new_timeo; 48391da177e4SLinus Torvalds } 48401da177e4SLinus Torvalds 4841a254b246SHarvey Harrison static struct workqueue_struct *laundry_wq; 4842a254b246SHarvey Harrison static void laundromat_main(struct work_struct *); 4843a254b246SHarvey Harrison 4844a254b246SHarvey Harrison static void 484509121281SStanislav Kinsbursky laundromat_main(struct work_struct *laundry) 48461da177e4SLinus Torvalds { 48471da177e4SLinus Torvalds time_t t; 48482e55f3abSGeliang Tang struct delayed_work *dwork = to_delayed_work(laundry); 484909121281SStanislav Kinsbursky struct nfsd_net *nn = container_of(dwork, struct nfsd_net, 485009121281SStanislav Kinsbursky laundromat_work); 48511da177e4SLinus Torvalds 485209121281SStanislav Kinsbursky t = nfs4_laundromat(nn); 48531da177e4SLinus Torvalds dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); 485409121281SStanislav Kinsbursky queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); 48551da177e4SLinus Torvalds } 48561da177e4SLinus Torvalds 48578fcd461dSJeff Layton static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) 4858f8816512SNeilBrown { 48598fcd461dSJeff Layton if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) 4860f7a4d872SJ. Bruce Fields return nfserr_bad_stateid; 4861f7a4d872SJ. Bruce Fields return nfs_ok; 48621da177e4SLinus Torvalds } 48631da177e4SLinus Torvalds 48641da177e4SLinus Torvalds static inline int 486582c5ff1bSJeff Layton access_permit_read(struct nfs4_ol_stateid *stp) 48661da177e4SLinus Torvalds { 486782c5ff1bSJeff Layton return test_access(NFS4_SHARE_ACCESS_READ, stp) || 486882c5ff1bSJeff Layton test_access(NFS4_SHARE_ACCESS_BOTH, stp) || 486982c5ff1bSJeff Layton test_access(NFS4_SHARE_ACCESS_WRITE, stp); 48701da177e4SLinus Torvalds } 48711da177e4SLinus Torvalds 48721da177e4SLinus Torvalds static inline int 487382c5ff1bSJeff Layton access_permit_write(struct nfs4_ol_stateid *stp) 48741da177e4SLinus Torvalds { 487582c5ff1bSJeff Layton return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || 487682c5ff1bSJeff Layton test_access(NFS4_SHARE_ACCESS_BOTH, stp); 48771da177e4SLinus Torvalds } 48781da177e4SLinus Torvalds 48791da177e4SLinus Torvalds static 4880dcef0413SJ. Bruce Fields __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) 48811da177e4SLinus Torvalds { 4882b37ad28bSAl Viro __be32 status = nfserr_openmode; 48831da177e4SLinus Torvalds 488402921914SJ. Bruce Fields /* For lock stateid's, we test the parent open, not the lock: */ 488502921914SJ. Bruce Fields if (stp->st_openstp) 488602921914SJ. Bruce Fields stp = stp->st_openstp; 488782c5ff1bSJeff Layton if ((flags & WR_STATE) && !access_permit_write(stp)) 48881da177e4SLinus Torvalds goto out; 488982c5ff1bSJeff Layton if ((flags & RD_STATE) && !access_permit_read(stp)) 48901da177e4SLinus Torvalds goto out; 48911da177e4SLinus Torvalds status = nfs_ok; 48921da177e4SLinus Torvalds out: 48931da177e4SLinus Torvalds return status; 48941da177e4SLinus Torvalds } 48951da177e4SLinus Torvalds 4896b37ad28bSAl Viro static inline __be32 48975ccb0066SStanislav Kinsbursky check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) 48981da177e4SLinus Torvalds { 4899203a8c8eSJ. Bruce Fields if (ONE_STATEID(stateid) && (flags & RD_STATE)) 49001da177e4SLinus Torvalds return nfs_ok; 4901c87fb4a3SJ. Bruce Fields else if (opens_in_grace(net)) { 490225985edcSLucas De Marchi /* Answer in remaining cases depends on existence of 49031da177e4SLinus Torvalds * conflicting state; so we must wait out the grace period. */ 49041da177e4SLinus Torvalds return nfserr_grace; 49051da177e4SLinus Torvalds } else if (flags & WR_STATE) 49061da177e4SLinus Torvalds return nfs4_share_conflict(current_fh, 49071da177e4SLinus Torvalds NFS4_SHARE_DENY_WRITE); 49081da177e4SLinus Torvalds else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ 49091da177e4SLinus Torvalds return nfs4_share_conflict(current_fh, 49101da177e4SLinus Torvalds NFS4_SHARE_DENY_READ); 49111da177e4SLinus Torvalds } 49121da177e4SLinus Torvalds 49131da177e4SLinus Torvalds /* 49141da177e4SLinus Torvalds * Allow READ/WRITE during grace period on recovered state only for files 49151da177e4SLinus Torvalds * that are not able to provide mandatory locking. 49161da177e4SLinus Torvalds */ 49171da177e4SLinus Torvalds static inline int 49185ccb0066SStanislav Kinsbursky grace_disallows_io(struct net *net, struct inode *inode) 49191da177e4SLinus Torvalds { 4920c87fb4a3SJ. Bruce Fields return opens_in_grace(net) && mandatory_lock(inode); 49211da177e4SLinus Torvalds } 49221da177e4SLinus Torvalds 492357b7b43bSJ. Bruce Fields static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) 49240836f587SJ. Bruce Fields { 49256668958fSAndy Adamson /* 49266668958fSAndy Adamson * When sessions are used the stateid generation number is ignored 49276668958fSAndy Adamson * when it is zero. 49286668958fSAndy Adamson */ 492928dde241SJ. Bruce Fields if (has_session && in->si_generation == 0) 493081b82965SJ. Bruce Fields return nfs_ok; 493181b82965SJ. Bruce Fields 493281b82965SJ. Bruce Fields if (in->si_generation == ref->si_generation) 493381b82965SJ. Bruce Fields return nfs_ok; 49346668958fSAndy Adamson 49350836f587SJ. Bruce Fields /* If the client sends us a stateid from the future, it's buggy: */ 493614b7f4a1SJeff Layton if (nfsd4_stateid_generation_after(in, ref)) 49370836f587SJ. Bruce Fields return nfserr_bad_stateid; 49380836f587SJ. Bruce Fields /* 493981b82965SJ. Bruce Fields * However, we could see a stateid from the past, even from a 494081b82965SJ. Bruce Fields * non-buggy client. For example, if the client sends a lock 494181b82965SJ. Bruce Fields * while some IO is outstanding, the lock may bump si_generation 494281b82965SJ. Bruce Fields * while the IO is still in flight. The client could avoid that 494381b82965SJ. Bruce Fields * situation by waiting for responses on all the IO requests, 494481b82965SJ. Bruce Fields * but better performance may result in retrying IO that 494581b82965SJ. Bruce Fields * receives an old_stateid error if requests are rarely 494681b82965SJ. Bruce Fields * reordered in flight: 49470836f587SJ. Bruce Fields */ 49480836f587SJ. Bruce Fields return nfserr_old_stateid; 49490836f587SJ. Bruce Fields } 49500836f587SJ. Bruce Fields 495103da3169STrond Myklebust static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) 495203da3169STrond Myklebust { 495303da3169STrond Myklebust __be32 ret; 495403da3169STrond Myklebust 495503da3169STrond Myklebust spin_lock(&s->sc_lock); 495603da3169STrond Myklebust ret = nfsd4_verify_open_stid(s); 495703da3169STrond Myklebust if (ret == nfs_ok) 495803da3169STrond Myklebust ret = check_stateid_generation(in, &s->sc_stateid, has_session); 495903da3169STrond Myklebust spin_unlock(&s->sc_lock); 496003da3169STrond Myklebust return ret; 496103da3169STrond Myklebust } 496203da3169STrond Myklebust 4963ebe9cb3bSChristoph Hellwig static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) 4964ebe9cb3bSChristoph Hellwig { 4965ebe9cb3bSChristoph Hellwig if (ols->st_stateowner->so_is_open_owner && 4966ebe9cb3bSChristoph Hellwig !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) 4967ebe9cb3bSChristoph Hellwig return nfserr_bad_stateid; 4968ebe9cb3bSChristoph Hellwig return nfs_ok; 4969ebe9cb3bSChristoph Hellwig } 4970ebe9cb3bSChristoph Hellwig 49717df302f7SChuck Lever static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) 497217456804SBryan Schumaker { 497397b7e3b6SJ. Bruce Fields struct nfs4_stid *s; 49741af71cc8SJeff Layton __be32 status = nfserr_bad_stateid; 497517456804SBryan Schumaker 4976ae254dacSAndrew Elble if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || 4977ae254dacSAndrew Elble CLOSE_STATEID(stateid)) 49781af71cc8SJeff Layton return status; 49797df302f7SChuck Lever /* Client debugging aid. */ 49807df302f7SChuck Lever if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { 49817df302f7SChuck Lever char addr_str[INET6_ADDRSTRLEN]; 49827df302f7SChuck Lever rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, 49837df302f7SChuck Lever sizeof(addr_str)); 49847df302f7SChuck Lever pr_warn_ratelimited("NFSD: client %s testing state ID " 49857df302f7SChuck Lever "with incorrect client ID\n", addr_str); 49861af71cc8SJeff Layton return status; 49877df302f7SChuck Lever } 49881af71cc8SJeff Layton spin_lock(&cl->cl_lock); 49891af71cc8SJeff Layton s = find_stateid_locked(cl, stateid); 499097b7e3b6SJ. Bruce Fields if (!s) 49911af71cc8SJeff Layton goto out_unlock; 499203da3169STrond Myklebust status = nfsd4_stid_check_stateid_generation(stateid, s, 1); 499317456804SBryan Schumaker if (status) 49941af71cc8SJeff Layton goto out_unlock; 499523340032SJ. Bruce Fields switch (s->sc_type) { 499623340032SJ. Bruce Fields case NFS4_DELEG_STID: 49971af71cc8SJeff Layton status = nfs_ok; 49981af71cc8SJeff Layton break; 49993bd64a5bSJ. Bruce Fields case NFS4_REVOKED_DELEG_STID: 50001af71cc8SJeff Layton status = nfserr_deleg_revoked; 50011af71cc8SJeff Layton break; 500223340032SJ. Bruce Fields case NFS4_OPEN_STID: 500323340032SJ. Bruce Fields case NFS4_LOCK_STID: 5004ebe9cb3bSChristoph Hellwig status = nfsd4_check_openowner_confirmed(openlockstateid(s)); 50051af71cc8SJeff Layton break; 500623340032SJ. Bruce Fields default: 500723340032SJ. Bruce Fields printk("unknown stateid type %x\n", s->sc_type); 5008b0fc29d6STrond Myklebust /* Fallthrough */ 500923340032SJ. Bruce Fields case NFS4_CLOSED_STID: 5010b0fc29d6STrond Myklebust case NFS4_CLOSED_DELEG_STID: 50111af71cc8SJeff Layton status = nfserr_bad_stateid; 501223340032SJ. Bruce Fields } 50131af71cc8SJeff Layton out_unlock: 50141af71cc8SJeff Layton spin_unlock(&cl->cl_lock); 50151af71cc8SJeff Layton return status; 501617456804SBryan Schumaker } 501717456804SBryan Schumaker 5018cd61c522SChristoph Hellwig __be32 50192dd6e458STrond Myklebust nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, 50202dd6e458STrond Myklebust stateid_t *stateid, unsigned char typemask, 50212dd6e458STrond Myklebust struct nfs4_stid **s, struct nfsd_net *nn) 502238c2f4b1SJ. Bruce Fields { 50230eb6f20aSJ. Bruce Fields __be32 status; 502495da1b3aSAndrew Elble bool return_revoked = false; 502595da1b3aSAndrew Elble 502695da1b3aSAndrew Elble /* 502795da1b3aSAndrew Elble * only return revoked delegations if explicitly asked. 502895da1b3aSAndrew Elble * otherwise we report revoked or bad_stateid status. 502995da1b3aSAndrew Elble */ 503095da1b3aSAndrew Elble if (typemask & NFS4_REVOKED_DELEG_STID) 503195da1b3aSAndrew Elble return_revoked = true; 503295da1b3aSAndrew Elble else if (typemask & NFS4_DELEG_STID) 503395da1b3aSAndrew Elble typemask |= NFS4_REVOKED_DELEG_STID; 503438c2f4b1SJ. Bruce Fields 5035ae254dacSAndrew Elble if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || 5036ae254dacSAndrew Elble CLOSE_STATEID(stateid)) 503738c2f4b1SJ. Bruce Fields return nfserr_bad_stateid; 50384b24ca7dSJeff Layton status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); 5039a8a7c677STrond Myklebust if (status == nfserr_stale_clientid) { 50404b24ca7dSJeff Layton if (cstate->session) 5041a8a7c677STrond Myklebust return nfserr_bad_stateid; 504238c2f4b1SJ. Bruce Fields return nfserr_stale_stateid; 5043a8a7c677STrond Myklebust } 50440eb6f20aSJ. Bruce Fields if (status) 50450eb6f20aSJ. Bruce Fields return status; 50464b24ca7dSJeff Layton *s = find_stateid_by_type(cstate->clp, stateid, typemask); 504738c2f4b1SJ. Bruce Fields if (!*s) 504838c2f4b1SJ. Bruce Fields return nfserr_bad_stateid; 504995da1b3aSAndrew Elble if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) { 505095da1b3aSAndrew Elble nfs4_put_stid(*s); 505195da1b3aSAndrew Elble if (cstate->minorversion) 505295da1b3aSAndrew Elble return nfserr_deleg_revoked; 505395da1b3aSAndrew Elble return nfserr_bad_stateid; 505495da1b3aSAndrew Elble } 505538c2f4b1SJ. Bruce Fields return nfs_ok; 505638c2f4b1SJ. Bruce Fields } 505738c2f4b1SJ. Bruce Fields 5058a0649b2dSChristoph Hellwig static struct file * 5059a0649b2dSChristoph Hellwig nfs4_find_file(struct nfs4_stid *s, int flags) 5060a0649b2dSChristoph Hellwig { 5061af90f707SChristoph Hellwig if (!s) 5062af90f707SChristoph Hellwig return NULL; 5063af90f707SChristoph Hellwig 5064a0649b2dSChristoph Hellwig switch (s->sc_type) { 5065a0649b2dSChristoph Hellwig case NFS4_DELEG_STID: 5066a0649b2dSChristoph Hellwig if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) 5067a0649b2dSChristoph Hellwig return NULL; 5068a0649b2dSChristoph Hellwig return get_file(s->sc_file->fi_deleg_file); 5069a0649b2dSChristoph Hellwig case NFS4_OPEN_STID: 5070a0649b2dSChristoph Hellwig case NFS4_LOCK_STID: 5071a0649b2dSChristoph Hellwig if (flags & RD_STATE) 5072a0649b2dSChristoph Hellwig return find_readable_file(s->sc_file); 5073a0649b2dSChristoph Hellwig else 5074a0649b2dSChristoph Hellwig return find_writeable_file(s->sc_file); 5075a0649b2dSChristoph Hellwig break; 5076a0649b2dSChristoph Hellwig } 5077a0649b2dSChristoph Hellwig 5078a0649b2dSChristoph Hellwig return NULL; 5079a0649b2dSChristoph Hellwig } 5080a0649b2dSChristoph Hellwig 5081a0649b2dSChristoph Hellwig static __be32 5082a0649b2dSChristoph Hellwig nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags) 5083a0649b2dSChristoph Hellwig { 5084a0649b2dSChristoph Hellwig __be32 status; 5085a0649b2dSChristoph Hellwig 5086a0649b2dSChristoph Hellwig status = nfsd4_check_openowner_confirmed(ols); 5087a0649b2dSChristoph Hellwig if (status) 5088a0649b2dSChristoph Hellwig return status; 5089a0649b2dSChristoph Hellwig return nfs4_check_openmode(ols, flags); 5090a0649b2dSChristoph Hellwig } 5091a0649b2dSChristoph Hellwig 5092af90f707SChristoph Hellwig static __be32 5093af90f707SChristoph Hellwig nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, 5094af90f707SChristoph Hellwig struct file **filpp, bool *tmp_file, int flags) 5095af90f707SChristoph Hellwig { 5096af90f707SChristoph Hellwig int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; 5097af90f707SChristoph Hellwig struct file *file; 5098af90f707SChristoph Hellwig __be32 status; 5099af90f707SChristoph Hellwig 5100af90f707SChristoph Hellwig file = nfs4_find_file(s, flags); 5101af90f707SChristoph Hellwig if (file) { 5102af90f707SChristoph Hellwig status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, 5103af90f707SChristoph Hellwig acc | NFSD_MAY_OWNER_OVERRIDE); 5104af90f707SChristoph Hellwig if (status) { 5105af90f707SChristoph Hellwig fput(file); 5106af90f707SChristoph Hellwig return status; 5107af90f707SChristoph Hellwig } 5108af90f707SChristoph Hellwig 5109af90f707SChristoph Hellwig *filpp = file; 5110af90f707SChristoph Hellwig } else { 5111af90f707SChristoph Hellwig status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp); 5112af90f707SChristoph Hellwig if (status) 5113af90f707SChristoph Hellwig return status; 5114af90f707SChristoph Hellwig 5115af90f707SChristoph Hellwig if (tmp_file) 5116af90f707SChristoph Hellwig *tmp_file = true; 5117af90f707SChristoph Hellwig } 5118af90f707SChristoph Hellwig 5119af90f707SChristoph Hellwig return 0; 5120af90f707SChristoph Hellwig } 5121af90f707SChristoph Hellwig 51221da177e4SLinus Torvalds /* 51231da177e4SLinus Torvalds * Checks for stateid operations 51241da177e4SLinus Torvalds */ 5125b37ad28bSAl Viro __be32 5126af90f707SChristoph Hellwig nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, 5127aa0d6aedSAnna Schumaker struct nfsd4_compound_state *cstate, struct svc_fh *fhp, 5128aa0d6aedSAnna Schumaker stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file) 51291da177e4SLinus Torvalds { 5130a0649b2dSChristoph Hellwig struct inode *ino = d_inode(fhp->fh_dentry); 5131af90f707SChristoph Hellwig struct net *net = SVC_NET(rqstp); 51323320fef1SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 5133af90f707SChristoph Hellwig struct nfs4_stid *s = NULL; 5134b37ad28bSAl Viro __be32 status; 51351da177e4SLinus Torvalds 51361da177e4SLinus Torvalds if (filpp) 51371da177e4SLinus Torvalds *filpp = NULL; 5138af90f707SChristoph Hellwig if (tmp_file) 5139af90f707SChristoph Hellwig *tmp_file = false; 51401da177e4SLinus Torvalds 51415ccb0066SStanislav Kinsbursky if (grace_disallows_io(net, ino)) 51421da177e4SLinus Torvalds return nfserr_grace; 51431da177e4SLinus Torvalds 5144af90f707SChristoph Hellwig if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { 5145af90f707SChristoph Hellwig status = check_special_stateids(net, fhp, stateid, flags); 5146af90f707SChristoph Hellwig goto done; 5147af90f707SChristoph Hellwig } 51481da177e4SLinus Torvalds 51492dd6e458STrond Myklebust status = nfsd4_lookup_stateid(cstate, stateid, 5150db24b3b4SJeff Layton NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, 51512dd6e458STrond Myklebust &s, nn); 515238c2f4b1SJ. Bruce Fields if (status) 5153c2d1d6a8STrond Myklebust return status; 515403da3169STrond Myklebust status = nfsd4_stid_check_stateid_generation(stateid, s, 5155a0649b2dSChristoph Hellwig nfsd4_has_session(cstate)); 51560c2a498fSJ. Bruce Fields if (status) 51570c2a498fSJ. Bruce Fields goto out; 5158a0649b2dSChristoph Hellwig 5159f7a4d872SJ. Bruce Fields switch (s->sc_type) { 5160f7a4d872SJ. Bruce Fields case NFS4_DELEG_STID: 5161a0649b2dSChristoph Hellwig status = nfs4_check_delegmode(delegstateid(s), flags); 5162f7a4d872SJ. Bruce Fields break; 5163f7a4d872SJ. Bruce Fields case NFS4_OPEN_STID: 5164f7a4d872SJ. Bruce Fields case NFS4_LOCK_STID: 5165a0649b2dSChristoph Hellwig status = nfs4_check_olstateid(fhp, openlockstateid(s), flags); 5166f7a4d872SJ. Bruce Fields break; 5167f7a4d872SJ. Bruce Fields default: 516814bcab1aSTrond Myklebust status = nfserr_bad_stateid; 5169a0649b2dSChristoph Hellwig break; 51701da177e4SLinus Torvalds } 51718fcd461dSJeff Layton if (status) 51728fcd461dSJeff Layton goto out; 51738fcd461dSJeff Layton status = nfs4_check_fh(fhp, s); 5174a0649b2dSChristoph Hellwig 5175af90f707SChristoph Hellwig done: 5176af90f707SChristoph Hellwig if (!status && filpp) 5177af90f707SChristoph Hellwig status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags); 51781da177e4SLinus Torvalds out: 5179af90f707SChristoph Hellwig if (s) 5180fd911011STrond Myklebust nfs4_put_stid(s); 51811da177e4SLinus Torvalds return status; 51821da177e4SLinus Torvalds } 51831da177e4SLinus Torvalds 5184e1ca12dfSBryan Schumaker /* 518517456804SBryan Schumaker * Test if the stateid is valid 518617456804SBryan Schumaker */ 518717456804SBryan Schumaker __be32 518817456804SBryan Schumaker nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 5189eb69853dSChristoph Hellwig union nfsd4_op_u *u) 519017456804SBryan Schumaker { 5191eb69853dSChristoph Hellwig struct nfsd4_test_stateid *test_stateid = &u->test_stateid; 519203cfb420SBryan Schumaker struct nfsd4_test_stateid_id *stateid; 519303cfb420SBryan Schumaker struct nfs4_client *cl = cstate->session->se_client; 519403cfb420SBryan Schumaker 519503cfb420SBryan Schumaker list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) 51967df302f7SChuck Lever stateid->ts_id_status = 51977df302f7SChuck Lever nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); 519803cfb420SBryan Schumaker 519917456804SBryan Schumaker return nfs_ok; 520017456804SBryan Schumaker } 520117456804SBryan Schumaker 520242691398SChuck Lever static __be32 520342691398SChuck Lever nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) 520442691398SChuck Lever { 520542691398SChuck Lever struct nfs4_ol_stateid *stp = openlockstateid(s); 520642691398SChuck Lever __be32 ret; 520742691398SChuck Lever 5208659aefb6STrond Myklebust ret = nfsd4_lock_ol_stateid(stp); 5209659aefb6STrond Myklebust if (ret) 5210659aefb6STrond Myklebust goto out_put_stid; 521142691398SChuck Lever 521242691398SChuck Lever ret = check_stateid_generation(stateid, &s->sc_stateid, 1); 521342691398SChuck Lever if (ret) 521442691398SChuck Lever goto out; 521542691398SChuck Lever 521642691398SChuck Lever ret = nfserr_locks_held; 521742691398SChuck Lever if (check_for_locks(stp->st_stid.sc_file, 521842691398SChuck Lever lockowner(stp->st_stateowner))) 521942691398SChuck Lever goto out; 522042691398SChuck Lever 522142691398SChuck Lever release_lock_stateid(stp); 522242691398SChuck Lever ret = nfs_ok; 522342691398SChuck Lever 522442691398SChuck Lever out: 522542691398SChuck Lever mutex_unlock(&stp->st_mutex); 5226659aefb6STrond Myklebust out_put_stid: 522742691398SChuck Lever nfs4_put_stid(s); 522842691398SChuck Lever return ret; 522942691398SChuck Lever } 523042691398SChuck Lever 5231e1ca12dfSBryan Schumaker __be32 5232e1ca12dfSBryan Schumaker nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 5233eb69853dSChristoph Hellwig union nfsd4_op_u *u) 5234e1ca12dfSBryan Schumaker { 5235eb69853dSChristoph Hellwig struct nfsd4_free_stateid *free_stateid = &u->free_stateid; 5236e1ca12dfSBryan Schumaker stateid_t *stateid = &free_stateid->fr_stateid; 52372da1cec7SJ. Bruce Fields struct nfs4_stid *s; 52383bd64a5bSJ. Bruce Fields struct nfs4_delegation *dp; 523938c2f4b1SJ. Bruce Fields struct nfs4_client *cl = cstate->session->se_client; 52402da1cec7SJ. Bruce Fields __be32 ret = nfserr_bad_stateid; 5241e1ca12dfSBryan Schumaker 52421af71cc8SJeff Layton spin_lock(&cl->cl_lock); 52431af71cc8SJeff Layton s = find_stateid_locked(cl, stateid); 52442da1cec7SJ. Bruce Fields if (!s) 52451af71cc8SJeff Layton goto out_unlock; 524603da3169STrond Myklebust spin_lock(&s->sc_lock); 52472da1cec7SJ. Bruce Fields switch (s->sc_type) { 52482da1cec7SJ. Bruce Fields case NFS4_DELEG_STID: 5249e1ca12dfSBryan Schumaker ret = nfserr_locks_held; 52501af71cc8SJeff Layton break; 52512da1cec7SJ. Bruce Fields case NFS4_OPEN_STID: 52521af71cc8SJeff Layton ret = check_stateid_generation(stateid, &s->sc_stateid, 1); 52531af71cc8SJeff Layton if (ret) 52541af71cc8SJeff Layton break; 52551af71cc8SJeff Layton ret = nfserr_locks_held; 52561af71cc8SJeff Layton break; 52572da1cec7SJ. Bruce Fields case NFS4_LOCK_STID: 525803da3169STrond Myklebust spin_unlock(&s->sc_lock); 5259a15dfcd5SElena Reshetova refcount_inc(&s->sc_count); 52601af71cc8SJeff Layton spin_unlock(&cl->cl_lock); 526142691398SChuck Lever ret = nfsd4_free_lock_stateid(stateid, s); 52621af71cc8SJeff Layton goto out; 52633bd64a5bSJ. Bruce Fields case NFS4_REVOKED_DELEG_STID: 526403da3169STrond Myklebust spin_unlock(&s->sc_lock); 52653bd64a5bSJ. Bruce Fields dp = delegstateid(s); 52662d4a532dSJeff Layton list_del_init(&dp->dl_recall_lru); 52672d4a532dSJeff Layton spin_unlock(&cl->cl_lock); 52686011695dSTrond Myklebust nfs4_put_stid(s); 52693bd64a5bSJ. Bruce Fields ret = nfs_ok; 52701af71cc8SJeff Layton goto out; 52711af71cc8SJeff Layton /* Default falls through and returns nfserr_bad_stateid */ 5272e1ca12dfSBryan Schumaker } 527303da3169STrond Myklebust spin_unlock(&s->sc_lock); 52741af71cc8SJeff Layton out_unlock: 52751af71cc8SJeff Layton spin_unlock(&cl->cl_lock); 5276e1ca12dfSBryan Schumaker out: 5277e1ca12dfSBryan Schumaker return ret; 5278e1ca12dfSBryan Schumaker } 5279e1ca12dfSBryan Schumaker 52804c4cd222SNeilBrown static inline int 52814c4cd222SNeilBrown setlkflg (int type) 52824c4cd222SNeilBrown { 52834c4cd222SNeilBrown return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? 52844c4cd222SNeilBrown RD_STATE : WR_STATE; 52854c4cd222SNeilBrown } 52861da177e4SLinus Torvalds 5287dcef0413SJ. Bruce Fields static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) 5288c0a5d93eSJ. Bruce Fields { 5289c0a5d93eSJ. Bruce Fields struct svc_fh *current_fh = &cstate->current_fh; 5290c0a5d93eSJ. Bruce Fields struct nfs4_stateowner *sop = stp->st_stateowner; 5291c0a5d93eSJ. Bruce Fields __be32 status; 5292c0a5d93eSJ. Bruce Fields 5293c0a5d93eSJ. Bruce Fields status = nfsd4_check_seqid(cstate, sop, seqid); 5294c0a5d93eSJ. Bruce Fields if (status) 5295c0a5d93eSJ. Bruce Fields return status; 52969271d7e5STrond Myklebust status = nfsd4_lock_ol_stateid(stp); 52979271d7e5STrond Myklebust if (status != nfs_ok) 52989271d7e5STrond Myklebust return status; 5299f7a4d872SJ. Bruce Fields status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); 530035a92fe8SJeff Layton if (status == nfs_ok) 530135a92fe8SJeff Layton status = nfs4_check_fh(current_fh, &stp->st_stid); 530235a92fe8SJeff Layton if (status != nfs_ok) 5303feb9dad5SOleg Drokin mutex_unlock(&stp->st_mutex); 5304f7a4d872SJ. Bruce Fields return status; 5305c0a5d93eSJ. Bruce Fields } 5306c0a5d93eSJ. Bruce Fields 53071da177e4SLinus Torvalds /* 53081da177e4SLinus Torvalds * Checks for sequence id mutating operations. 53091da177e4SLinus Torvalds */ 5310b37ad28bSAl Viro static __be32 5311dd453dfdSBenny Halevy nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, 53122288d0e3SJ. Bruce Fields stateid_t *stateid, char typemask, 53133320fef1SStanislav Kinsbursky struct nfs4_ol_stateid **stpp, 53143320fef1SStanislav Kinsbursky struct nfsd_net *nn) 53151da177e4SLinus Torvalds { 53160836f587SJ. Bruce Fields __be32 status; 531738c2f4b1SJ. Bruce Fields struct nfs4_stid *s; 5318e17f99b7STrond Myklebust struct nfs4_ol_stateid *stp = NULL; 53191da177e4SLinus Torvalds 53208c10cbdbSBenny Halevy dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, 53218c10cbdbSBenny Halevy seqid, STATEID_VAL(stateid)); 53221da177e4SLinus Torvalds 53231da177e4SLinus Torvalds *stpp = NULL; 53242dd6e458STrond Myklebust status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); 5325c0a5d93eSJ. Bruce Fields if (status) 5326c0a5d93eSJ. Bruce Fields return status; 5327e17f99b7STrond Myklebust stp = openlockstateid(s); 532858fb12e6SJeff Layton nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); 53291da177e4SLinus Torvalds 5330e17f99b7STrond Myklebust status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); 5331fd911011STrond Myklebust if (!status) 5332e17f99b7STrond Myklebust *stpp = stp; 5333fd911011STrond Myklebust else 5334fd911011STrond Myklebust nfs4_put_stid(&stp->st_stid); 5335e17f99b7STrond Myklebust return status; 53361da177e4SLinus Torvalds } 53371da177e4SLinus Torvalds 53383320fef1SStanislav Kinsbursky static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, 53393320fef1SStanislav Kinsbursky stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) 5340c0a5d93eSJ. Bruce Fields { 5341c0a5d93eSJ. Bruce Fields __be32 status; 5342c0a5d93eSJ. Bruce Fields struct nfs4_openowner *oo; 53434cbfc9f7STrond Myklebust struct nfs4_ol_stateid *stp; 53441da177e4SLinus Torvalds 5345c0a5d93eSJ. Bruce Fields status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, 53464cbfc9f7STrond Myklebust NFS4_OPEN_STID, &stp, nn); 53470836f587SJ. Bruce Fields if (status) 53480836f587SJ. Bruce Fields return status; 53494cbfc9f7STrond Myklebust oo = openowner(stp->st_stateowner); 53504cbfc9f7STrond Myklebust if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { 5351feb9dad5SOleg Drokin mutex_unlock(&stp->st_mutex); 53524cbfc9f7STrond Myklebust nfs4_put_stid(&stp->st_stid); 5353c0a5d93eSJ. Bruce Fields return nfserr_bad_stateid; 53544cbfc9f7STrond Myklebust } 53554cbfc9f7STrond Myklebust *stpp = stp; 53563a4f98bbSNeilBrown return nfs_ok; 53571da177e4SLinus Torvalds } 53581da177e4SLinus Torvalds 5359b37ad28bSAl Viro __be32 5360ca364317SJ.Bruce Fields nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 5361eb69853dSChristoph Hellwig union nfsd4_op_u *u) 53621da177e4SLinus Torvalds { 5363eb69853dSChristoph Hellwig struct nfsd4_open_confirm *oc = &u->open_confirm; 5364b37ad28bSAl Viro __be32 status; 5365fe0750e5SJ. Bruce Fields struct nfs4_openowner *oo; 5366dcef0413SJ. Bruce Fields struct nfs4_ol_stateid *stp; 53673320fef1SStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 53681da177e4SLinus Torvalds 5369a6a9f18fSAl Viro dprintk("NFSD: nfsd4_open_confirm on file %pd\n", 5370a6a9f18fSAl Viro cstate->current_fh.fh_dentry); 53711da177e4SLinus Torvalds 5372ca364317SJ.Bruce Fields status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); 5373a8cddc5dSJ. Bruce Fields if (status) 5374a8cddc5dSJ. Bruce Fields return status; 53751da177e4SLinus Torvalds 53769072d5c6SJ. Bruce Fields status = nfs4_preprocess_seqid_op(cstate, 5377ca364317SJ.Bruce Fields oc->oc_seqid, &oc->oc_req_stateid, 53783320fef1SStanislav Kinsbursky NFS4_OPEN_STID, &stp, nn); 53799072d5c6SJ. Bruce Fields if (status) 53801da177e4SLinus Torvalds goto out; 5381fe0750e5SJ. Bruce Fields oo = openowner(stp->st_stateowner); 538268b66e82SJ. Bruce Fields status = nfserr_bad_stateid; 538335a92fe8SJeff Layton if (oo->oo_flags & NFS4_OO_CONFIRMED) { 5384feb9dad5SOleg Drokin mutex_unlock(&stp->st_mutex); 53852585fc79STrond Myklebust goto put_stateid; 538635a92fe8SJeff Layton } 5387dad1c067SJ. Bruce Fields oo->oo_flags |= NFS4_OO_CONFIRMED; 53889767feb2SJeff Layton nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); 5389feb9dad5SOleg Drokin mutex_unlock(&stp->st_mutex); 53908c10cbdbSBenny Halevy dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", 5391dcef0413SJ. Bruce Fields __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); 5392c7b9a459SNeilBrown 53932a4317c5SJeff Layton nfsd4_client_record_create(oo->oo_owner.so_client); 539468b66e82SJ. Bruce Fields status = nfs_ok; 53952585fc79STrond Myklebust put_stateid: 53962585fc79STrond Myklebust nfs4_put_stid(&stp->st_stid); 53971da177e4SLinus Torvalds out: 53989411b1d4SJ. Bruce Fields nfsd4_bump_seqid(cstate, status); 53991da177e4SLinus Torvalds return status; 54001da177e4SLinus Torvalds } 54011da177e4SLinus Torvalds 54026409a5a6SJ. Bruce Fields static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) 54031da177e4SLinus Torvalds { 540482c5ff1bSJeff Layton if (!test_access(access, stp)) 54056409a5a6SJ. Bruce Fields return; 540611b9164aSTrond Myklebust nfs4_file_put_access(stp->st_stid.sc_file, access); 540782c5ff1bSJeff Layton clear_access(access, stp); 5408f197c271SJ. Bruce Fields } 54096409a5a6SJ. Bruce Fields 54106409a5a6SJ. Bruce Fields static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) 54116409a5a6SJ. Bruce Fields { 54126409a5a6SJ. Bruce Fields switch (to_access) { 54136409a5a6SJ. Bruce Fields case NFS4_SHARE_ACCESS_READ: 54146409a5a6SJ. Bruce Fields nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); 54156409a5a6SJ. Bruce Fields nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); 54166409a5a6SJ. Bruce Fields break; 54176409a5a6SJ. Bruce Fields case NFS4_SHARE_ACCESS_WRITE: 54186409a5a6SJ. Bruce Fields nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); 54196409a5a6SJ. Bruce Fields nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); 54206409a5a6SJ. Bruce Fields break; 54216409a5a6SJ. Bruce Fields case NFS4_SHARE_ACCESS_BOTH: 54226409a5a6SJ. Bruce Fields break; 54236409a5a6SJ. Bruce Fields default: 5424063b0fb9SJ. Bruce Fields WARN_ON_ONCE(1); 54251da177e4SLinus Torvalds } 54261da177e4SLinus Torvalds } 54271da177e4SLinus Torvalds 5428b37ad28bSAl Viro __be32 5429ca364317SJ.Bruce Fields nfsd4_open_downgrade(struct svc_rqst *rqstp, 5430eb69853dSChristoph Hellwig struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) 54311da177e4SLinus Torvalds { 5432eb69853dSChristoph Hellwig struct nfsd4_open_downgrade *od = &u->open_downgrade; 5433b37ad28bSAl Viro __be32 status; 5434dcef0413SJ. Bruce Fields struct nfs4_ol_stateid *stp; 54353320fef1SStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 54361da177e4SLinus Torvalds 5437a6a9f18fSAl Viro dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 5438a6a9f18fSAl Viro cstate->current_fh.fh_dentry); 54391da177e4SLinus Torvalds 5440c30e92dfSJ. Bruce Fields /* We don't yet support WANT bits: */ 54412c8bd7e0SBenny Halevy if (od->od_deleg_want) 54422c8bd7e0SBenny Halevy dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, 54432c8bd7e0SBenny Halevy od->od_deleg_want); 54441da177e4SLinus Torvalds 5445c0a5d93eSJ. Bruce Fields status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, 54463320fef1SStanislav Kinsbursky &od->od_stateid, &stp, nn); 54479072d5c6SJ. Bruce Fields if (status) 54481da177e4SLinus Torvalds goto out; 54491da177e4SLinus Torvalds status = nfserr_inval; 545082c5ff1bSJeff Layton if (!test_access(od->od_share_access, stp)) { 5451c11c591fSJeff Layton dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", 54521da177e4SLinus Torvalds stp->st_access_bmap, od->od_share_access); 54530667b1e9STrond Myklebust goto put_stateid; 54541da177e4SLinus Torvalds } 5455ce0fc43cSJeff Layton if (!test_deny(od->od_share_deny, stp)) { 5456c11c591fSJeff Layton dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", 54571da177e4SLinus Torvalds stp->st_deny_bmap, od->od_share_deny); 54580667b1e9STrond Myklebust goto put_stateid; 54591da177e4SLinus Torvalds } 54606409a5a6SJ. Bruce Fields nfs4_stateid_downgrade(stp, od->od_share_access); 5461ce0fc43cSJeff Layton reset_union_bmap_deny(od->od_share_deny, stp); 54629767feb2SJeff Layton nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); 54631da177e4SLinus Torvalds status = nfs_ok; 54640667b1e9STrond Myklebust put_stateid: 5465feb9dad5SOleg Drokin mutex_unlock(&stp->st_mutex); 54660667b1e9STrond Myklebust nfs4_put_stid(&stp->st_stid); 54671da177e4SLinus Torvalds out: 54689411b1d4SJ. Bruce Fields nfsd4_bump_seqid(cstate, status); 54691da177e4SLinus Torvalds return status; 54701da177e4SLinus Torvalds } 54711da177e4SLinus Torvalds 5472f7a4d872SJ. Bruce Fields static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) 5473f7a4d872SJ. Bruce Fields { 5474acf9295bSTrond Myklebust struct nfs4_client *clp = s->st_stid.sc_client; 5475e8568739SJeff Layton bool unhashed; 5476d83017f9SJeff Layton LIST_HEAD(reaplist); 5477acf9295bSTrond Myklebust 54782c41beb0SJeff Layton spin_lock(&clp->cl_lock); 5479e8568739SJeff Layton unhashed = unhash_open_stateid(s, &reaplist); 5480acf9295bSTrond Myklebust 5481d83017f9SJeff Layton if (clp->cl_minorversion) { 5482e8568739SJeff Layton if (unhashed) 5483d83017f9SJeff Layton put_ol_stateid_locked(s, &reaplist); 5484d83017f9SJeff Layton spin_unlock(&clp->cl_lock); 5485d83017f9SJeff Layton free_ol_stateid_reaplist(&reaplist); 5486d83017f9SJeff Layton } else { 5487d83017f9SJeff Layton spin_unlock(&clp->cl_lock); 5488d83017f9SJeff Layton free_ol_stateid_reaplist(&reaplist); 5489e8568739SJeff Layton if (unhashed) 5490d3134b10SJeff Layton move_to_close_lru(s, clp->net); 549138c387b5SJ. Bruce Fields } 5492d83017f9SJeff Layton } 549338c387b5SJ. Bruce Fields 54941da177e4SLinus Torvalds /* 54951da177e4SLinus Torvalds * nfs4_unlock_state() called after encode 54961da177e4SLinus Torvalds */ 5497b37ad28bSAl Viro __be32 5498ca364317SJ.Bruce Fields nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 5499eb69853dSChristoph Hellwig union nfsd4_op_u *u) 55001da177e4SLinus Torvalds { 5501eb69853dSChristoph Hellwig struct nfsd4_close *close = &u->close; 5502b37ad28bSAl Viro __be32 status; 5503dcef0413SJ. Bruce Fields struct nfs4_ol_stateid *stp; 55043320fef1SStanislav Kinsbursky struct net *net = SVC_NET(rqstp); 55053320fef1SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 55061da177e4SLinus Torvalds 5507a6a9f18fSAl Viro dprintk("NFSD: nfsd4_close on file %pd\n", 5508a6a9f18fSAl Viro cstate->current_fh.fh_dentry); 55091da177e4SLinus Torvalds 5510f7a4d872SJ. Bruce Fields status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, 5511f7a4d872SJ. Bruce Fields &close->cl_stateid, 5512f7a4d872SJ. Bruce Fields NFS4_OPEN_STID|NFS4_CLOSED_STID, 55133320fef1SStanislav Kinsbursky &stp, nn); 55149411b1d4SJ. Bruce Fields nfsd4_bump_seqid(cstate, status); 55159072d5c6SJ. Bruce Fields if (status) 55161da177e4SLinus Torvalds goto out; 551715ca08d3STrond Myklebust 551815ca08d3STrond Myklebust stp->st_stid.sc_type = NFS4_CLOSED_STID; 5519bd2decacSJeff Layton 5520bd2decacSJeff Layton /* 5521bd2decacSJeff Layton * Technically we don't _really_ have to increment or copy it, since 5522bd2decacSJeff Layton * it should just be gone after this operation and we clobber the 5523bd2decacSJeff Layton * copied value below, but we continue to do so here just to ensure 5524bd2decacSJeff Layton * that racing ops see that there was a state change. 5525bd2decacSJeff Layton */ 55269767feb2SJeff Layton nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); 55271da177e4SLinus Torvalds 5528f7a4d872SJ. Bruce Fields nfsd4_close_open_stateid(stp); 552915ca08d3STrond Myklebust mutex_unlock(&stp->st_mutex); 55308a0b589dSTrond Myklebust 5531bd2decacSJeff Layton /* v4.1+ suggests that we send a special stateid in here, since the 5532bd2decacSJeff Layton * clients should just ignore this anyway. Since this is not useful 5533bd2decacSJeff Layton * for v4.0 clients either, we set it to the special close_stateid 5534bd2decacSJeff Layton * universally. 5535bd2decacSJeff Layton * 5536bd2decacSJeff Layton * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 5537bd2decacSJeff Layton */ 5538bd2decacSJeff Layton memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); 5539fb500a7cSTrond Myklebust 55408a0b589dSTrond Myklebust /* put reference from nfs4_preprocess_seqid_op */ 55418a0b589dSTrond Myklebust nfs4_put_stid(&stp->st_stid); 55421da177e4SLinus Torvalds out: 55431da177e4SLinus Torvalds return status; 55441da177e4SLinus Torvalds } 55451da177e4SLinus Torvalds 5546b37ad28bSAl Viro __be32 5547ca364317SJ.Bruce Fields nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 5548eb69853dSChristoph Hellwig union nfsd4_op_u *u) 55491da177e4SLinus Torvalds { 5550eb69853dSChristoph Hellwig struct nfsd4_delegreturn *dr = &u->delegreturn; 5551203a8c8eSJ. Bruce Fields struct nfs4_delegation *dp; 5552203a8c8eSJ. Bruce Fields stateid_t *stateid = &dr->dr_stateid; 555338c2f4b1SJ. Bruce Fields struct nfs4_stid *s; 5554b37ad28bSAl Viro __be32 status; 55553320fef1SStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 55561da177e4SLinus Torvalds 5557ca364317SJ.Bruce Fields if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) 5558203a8c8eSJ. Bruce Fields return status; 55591da177e4SLinus Torvalds 55602dd6e458STrond Myklebust status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); 556138c2f4b1SJ. Bruce Fields if (status) 5562203a8c8eSJ. Bruce Fields goto out; 556338c2f4b1SJ. Bruce Fields dp = delegstateid(s); 556403da3169STrond Myklebust status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate)); 5565203a8c8eSJ. Bruce Fields if (status) 5566fd911011STrond Myklebust goto put_stateid; 5567203a8c8eSJ. Bruce Fields 55683bd64a5bSJ. Bruce Fields destroy_delegation(dp); 5569fd911011STrond Myklebust put_stateid: 5570fd911011STrond Myklebust nfs4_put_stid(&dp->dl_stid); 55711da177e4SLinus Torvalds out: 55721da177e4SLinus Torvalds return status; 55731da177e4SLinus Torvalds } 55741da177e4SLinus Torvalds 557587df4de8SBenny Halevy static inline u64 557687df4de8SBenny Halevy end_offset(u64 start, u64 len) 557787df4de8SBenny Halevy { 557887df4de8SBenny Halevy u64 end; 557987df4de8SBenny Halevy 558087df4de8SBenny Halevy end = start + len; 558187df4de8SBenny Halevy return end >= start ? end: NFS4_MAX_UINT64; 558287df4de8SBenny Halevy } 558387df4de8SBenny Halevy 558487df4de8SBenny Halevy /* last octet in a range */ 558587df4de8SBenny Halevy static inline u64 558687df4de8SBenny Halevy last_byte_offset(u64 start, u64 len) 558787df4de8SBenny Halevy { 558887df4de8SBenny Halevy u64 end; 558987df4de8SBenny Halevy 5590063b0fb9SJ. Bruce Fields WARN_ON_ONCE(!len); 559187df4de8SBenny Halevy end = start + len; 559287df4de8SBenny Halevy return end > start ? end - 1: NFS4_MAX_UINT64; 559387df4de8SBenny Halevy } 559487df4de8SBenny Halevy 55951da177e4SLinus Torvalds /* 55961da177e4SLinus Torvalds * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that 55971da177e4SLinus Torvalds * we can't properly handle lock requests that go beyond the (2^63 - 1)-th 55981da177e4SLinus Torvalds * byte, because of sign extension problems. Since NFSv4 calls for 64-bit 55991da177e4SLinus Torvalds * locking, this prevents us from being completely protocol-compliant. The 56001da177e4SLinus Torvalds * real solution to this problem is to start using unsigned file offsets in 56011da177e4SLinus Torvalds * the VFS, but this is a very deep change! 56021da177e4SLinus Torvalds */ 56031da177e4SLinus Torvalds static inline void 56041da177e4SLinus Torvalds nfs4_transform_lock_offset(struct file_lock *lock) 56051da177e4SLinus Torvalds { 56061da177e4SLinus Torvalds if (lock->fl_start < 0) 56071da177e4SLinus Torvalds lock->fl_start = OFFSET_MAX; 56081da177e4SLinus Torvalds if (lock->fl_end < 0) 56091da177e4SLinus Torvalds lock->fl_end = OFFSET_MAX; 56101da177e4SLinus Torvalds } 56111da177e4SLinus Torvalds 5612cae80b30SJeff Layton static fl_owner_t 5613cae80b30SJeff Layton nfsd4_fl_get_owner(fl_owner_t owner) 5614aef9583bSKinglong Mee { 5615cae80b30SJeff Layton struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; 5616cae80b30SJeff Layton 5617cae80b30SJeff Layton nfs4_get_stateowner(&lo->lo_owner); 5618cae80b30SJeff Layton return owner; 5619aef9583bSKinglong Mee } 5620aef9583bSKinglong Mee 5621cae80b30SJeff Layton static void 5622cae80b30SJeff Layton nfsd4_fl_put_owner(fl_owner_t owner) 5623aef9583bSKinglong Mee { 5624cae80b30SJeff Layton struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; 5625aef9583bSKinglong Mee 5626cae80b30SJeff Layton if (lo) 5627aef9583bSKinglong Mee nfs4_put_stateowner(&lo->lo_owner); 5628aef9583bSKinglong Mee } 5629aef9583bSKinglong Mee 563076d348faSJeff Layton static void 563176d348faSJeff Layton nfsd4_lm_notify(struct file_lock *fl) 563276d348faSJeff Layton { 563376d348faSJeff Layton struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner; 563476d348faSJeff Layton struct net *net = lo->lo_owner.so_client->net; 563576d348faSJeff Layton struct nfsd_net *nn = net_generic(net, nfsd_net_id); 563676d348faSJeff Layton struct nfsd4_blocked_lock *nbl = container_of(fl, 563776d348faSJeff Layton struct nfsd4_blocked_lock, nbl_lock); 563876d348faSJeff Layton bool queue = false; 563976d348faSJeff Layton 56407919d0a2SJeff Layton /* An empty list means that something else is going to be using it */ 56410cc11a61SJeff Layton spin_lock(&nn->blocked_locks_lock); 564276d348faSJeff Layton if (!list_empty(&nbl->nbl_list)) { 564376d348faSJeff Layton list_del_init(&nbl->nbl_list); 56447919d0a2SJeff Layton list_del_init(&nbl->nbl_lru); 564576d348faSJeff Layton queue = true; 564676d348faSJeff Layton } 56470cc11a61SJeff Layton spin_unlock(&nn->blocked_locks_lock); 564876d348faSJeff Layton 564976d348faSJeff Layton if (queue) 565076d348faSJeff Layton nfsd4_run_cb(&nbl->nbl_cb); 565176d348faSJeff Layton } 565276d348faSJeff Layton 56537b021967SAlexey Dobriyan static const struct lock_manager_operations nfsd_posix_mng_ops = { 565476d348faSJeff Layton .lm_notify = nfsd4_lm_notify, 5655aef9583bSKinglong Mee .lm_get_owner = nfsd4_fl_get_owner, 5656aef9583bSKinglong Mee .lm_put_owner = nfsd4_fl_put_owner, 5657d5b9026aSNeilBrown }; 56581da177e4SLinus Torvalds 56591da177e4SLinus Torvalds static inline void 56601da177e4SLinus Torvalds nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) 56611da177e4SLinus Torvalds { 5662fe0750e5SJ. Bruce Fields struct nfs4_lockowner *lo; 56631da177e4SLinus Torvalds 5664d5b9026aSNeilBrown if (fl->fl_lmops == &nfsd_posix_mng_ops) { 5665fe0750e5SJ. Bruce Fields lo = (struct nfs4_lockowner *) fl->fl_owner; 5666fe0750e5SJ. Bruce Fields deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, 5667fe0750e5SJ. Bruce Fields lo->lo_owner.so_owner.len, GFP_KERNEL); 56687c13f344SJ. Bruce Fields if (!deny->ld_owner.data) 56697c13f344SJ. Bruce Fields /* We just don't care that much */ 56707c13f344SJ. Bruce Fields goto nevermind; 5671fe0750e5SJ. Bruce Fields deny->ld_owner.len = lo->lo_owner.so_owner.len; 5672fe0750e5SJ. Bruce Fields deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; 5673d5b9026aSNeilBrown } else { 56747c13f344SJ. Bruce Fields nevermind: 56757c13f344SJ. Bruce Fields deny->ld_owner.len = 0; 56767c13f344SJ. Bruce Fields deny->ld_owner.data = NULL; 5677d5b9026aSNeilBrown deny->ld_clientid.cl_boot = 0; 5678d5b9026aSNeilBrown deny->ld_clientid.cl_id = 0; 56791da177e4SLinus Torvalds } 56801da177e4SLinus Torvalds deny->ld_start = fl->fl_start; 568187df4de8SBenny Halevy deny->ld_length = NFS4_MAX_UINT64; 568287df4de8SBenny Halevy if (fl->fl_end != NFS4_MAX_UINT64) 56831da177e4SLinus Torvalds deny->ld_length = fl->fl_end - fl->fl_start + 1; 56841da177e4SLinus Torvalds deny->ld_type = NFS4_READ_LT; 56851da177e4SLinus Torvalds if (fl->fl_type != F_RDLCK) 56861da177e4SLinus Torvalds deny->ld_type = NFS4_WRITE_LT; 56871da177e4SLinus Torvalds } 56881da177e4SLinus Torvalds 5689fe0750e5SJ. Bruce Fields static struct nfs4_lockowner * 5690c8623999SKinglong Mee find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) 56911da177e4SLinus Torvalds { 5692d4f0489fSTrond Myklebust unsigned int strhashval = ownerstr_hashval(owner); 5693b3c32bcdSTrond Myklebust struct nfs4_stateowner *so; 56941da177e4SLinus Torvalds 56950a880a28STrond Myklebust lockdep_assert_held(&clp->cl_lock); 56960a880a28STrond Myklebust 5697d4f0489fSTrond Myklebust list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], 5698d4f0489fSTrond Myklebust so_strhash) { 5699b3c32bcdSTrond Myklebust if (so->so_is_open_owner) 5700b3c32bcdSTrond Myklebust continue; 5701b5971afaSKinglong Mee if (same_owner_str(so, owner)) 5702b5971afaSKinglong Mee return lockowner(nfs4_get_stateowner(so)); 57031da177e4SLinus Torvalds } 57041da177e4SLinus Torvalds return NULL; 57051da177e4SLinus Torvalds } 57061da177e4SLinus Torvalds 5707c58c6610STrond Myklebust static struct nfs4_lockowner * 5708c8623999SKinglong Mee find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) 5709c58c6610STrond Myklebust { 5710c58c6610STrond Myklebust struct nfs4_lockowner *lo; 5711c58c6610STrond Myklebust 5712d4f0489fSTrond Myklebust spin_lock(&clp->cl_lock); 5713c8623999SKinglong Mee lo = find_lockowner_str_locked(clp, owner); 5714d4f0489fSTrond Myklebust spin_unlock(&clp->cl_lock); 5715c58c6610STrond Myklebust return lo; 5716c58c6610STrond Myklebust } 5717c58c6610STrond Myklebust 57188f4b54c5SJeff Layton static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) 57198f4b54c5SJeff Layton { 5720c58c6610STrond Myklebust unhash_lockowner_locked(lockowner(sop)); 57218f4b54c5SJeff Layton } 57228f4b54c5SJeff Layton 57236b180f0bSJeff Layton static void nfs4_free_lockowner(struct nfs4_stateowner *sop) 57246b180f0bSJeff Layton { 57256b180f0bSJeff Layton struct nfs4_lockowner *lo = lockowner(sop); 57266b180f0bSJeff Layton 57276b180f0bSJeff Layton kmem_cache_free(lockowner_slab, lo); 57286b180f0bSJeff Layton } 57296b180f0bSJeff Layton 57306b180f0bSJeff Layton static const struct nfs4_stateowner_operations lockowner_ops = { 57318f4b54c5SJeff Layton .so_unhash = nfs4_unhash_lockowner, 57326b180f0bSJeff Layton .so_free = nfs4_free_lockowner, 57336b180f0bSJeff Layton }; 57346b180f0bSJeff Layton 57351da177e4SLinus Torvalds /* 57361da177e4SLinus Torvalds * Alloc a lock owner structure. 57371da177e4SLinus Torvalds * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 573825985edcSLucas De Marchi * occurred. 57391da177e4SLinus Torvalds * 574016bfdaafSJ. Bruce Fields * strhashval = ownerstr_hashval 57411da177e4SLinus Torvalds */ 5742fe0750e5SJ. Bruce Fields static struct nfs4_lockowner * 5743c58c6610STrond Myklebust alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, 5744c58c6610STrond Myklebust struct nfs4_ol_stateid *open_stp, 5745c58c6610STrond Myklebust struct nfsd4_lock *lock) 5746c58c6610STrond Myklebust { 5747c58c6610STrond Myklebust struct nfs4_lockowner *lo, *ret; 57481da177e4SLinus Torvalds 5749fe0750e5SJ. Bruce Fields lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); 5750fe0750e5SJ. Bruce Fields if (!lo) 57511da177e4SLinus Torvalds return NULL; 575276d348faSJeff Layton INIT_LIST_HEAD(&lo->lo_blocked); 5753fe0750e5SJ. Bruce Fields INIT_LIST_HEAD(&lo->lo_owner.so_stateids); 5754fe0750e5SJ. Bruce Fields lo->lo_owner.so_is_open_owner = 0; 57555db1c03fSJeff Layton lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; 57566b180f0bSJeff Layton lo->lo_owner.so_ops = &lockowner_ops; 5757d4f0489fSTrond Myklebust spin_lock(&clp->cl_lock); 5758c8623999SKinglong Mee ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); 5759c58c6610STrond Myklebust if (ret == NULL) { 5760c58c6610STrond Myklebust list_add(&lo->lo_owner.so_strhash, 5761d4f0489fSTrond Myklebust &clp->cl_ownerstr_hashtbl[strhashval]); 5762c58c6610STrond Myklebust ret = lo; 5763c58c6610STrond Myklebust } else 5764d50ffdedSKinglong Mee nfs4_free_stateowner(&lo->lo_owner); 5765d50ffdedSKinglong Mee 5766d4f0489fSTrond Myklebust spin_unlock(&clp->cl_lock); 5767340f0ba1SJ. Bruce Fields return ret; 57681da177e4SLinus Torvalds } 57691da177e4SLinus Torvalds 5770fd1fd685STrond Myklebust static struct nfs4_ol_stateid * 5771fd1fd685STrond Myklebust find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) 5772fd1fd685STrond Myklebust { 5773fd1fd685STrond Myklebust struct nfs4_ol_stateid *lst; 5774fd1fd685STrond Myklebust struct nfs4_client *clp = lo->lo_owner.so_client; 5775fd1fd685STrond Myklebust 5776fd1fd685STrond Myklebust lockdep_assert_held(&clp->cl_lock); 5777fd1fd685STrond Myklebust 5778fd1fd685STrond Myklebust list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { 5779fd1fd685STrond Myklebust if (lst->st_stid.sc_type != NFS4_LOCK_STID) 5780fd1fd685STrond Myklebust continue; 5781fd1fd685STrond Myklebust if (lst->st_stid.sc_file == fp) { 5782fd1fd685STrond Myklebust refcount_inc(&lst->st_stid.sc_count); 5783fd1fd685STrond Myklebust return lst; 5784fd1fd685STrond Myklebust } 5785fd1fd685STrond Myklebust } 5786fd1fd685STrond Myklebust return NULL; 5787fd1fd685STrond Myklebust } 5788fd1fd685STrond Myklebust 5789beeca19cSTrond Myklebust static struct nfs4_ol_stateid * 5790356a95ecSJeff Layton init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, 5791356a95ecSJeff Layton struct nfs4_file *fp, struct inode *inode, 5792f9c00c3aSJeff Layton struct nfs4_ol_stateid *open_stp) 57931da177e4SLinus Torvalds { 5794d3b313a4SJ. Bruce Fields struct nfs4_client *clp = lo->lo_owner.so_client; 5795beeca19cSTrond Myklebust struct nfs4_ol_stateid *retstp; 57961da177e4SLinus Torvalds 5797beeca19cSTrond Myklebust mutex_init(&stp->st_mutex); 57984f34bd05SAndrew Elble mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); 5799beeca19cSTrond Myklebust retry: 5800beeca19cSTrond Myklebust spin_lock(&clp->cl_lock); 5801beeca19cSTrond Myklebust spin_lock(&fp->fi_lock); 5802beeca19cSTrond Myklebust retstp = find_lock_stateid(lo, fp); 5803beeca19cSTrond Myklebust if (retstp) 5804beeca19cSTrond Myklebust goto out_unlock; 5805356a95ecSJeff Layton 5806a15dfcd5SElena Reshetova refcount_inc(&stp->st_stid.sc_count); 58073abdb607SJ. Bruce Fields stp->st_stid.sc_type = NFS4_LOCK_STID; 5808b5971afaSKinglong Mee stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); 580913cd2184SNeilBrown get_nfs4_file(fp); 581011b9164aSTrond Myklebust stp->st_stid.sc_file = fp; 58110997b173SJ. Bruce Fields stp->st_access_bmap = 0; 58121da177e4SLinus Torvalds stp->st_deny_bmap = open_stp->st_deny_bmap; 58134c4cd222SNeilBrown stp->st_openstp = open_stp; 58143c87b9b7STrond Myklebust list_add(&stp->st_locks, &open_stp->st_locks); 58151c755dc1SJeff Layton list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); 58161d31a253STrond Myklebust list_add(&stp->st_perfile, &fp->fi_stateids); 5817beeca19cSTrond Myklebust out_unlock: 58181d31a253STrond Myklebust spin_unlock(&fp->fi_lock); 5819beeca19cSTrond Myklebust spin_unlock(&clp->cl_lock); 5820beeca19cSTrond Myklebust if (retstp) { 5821beeca19cSTrond Myklebust if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { 5822beeca19cSTrond Myklebust nfs4_put_stid(&retstp->st_stid); 5823beeca19cSTrond Myklebust goto retry; 5824beeca19cSTrond Myklebust } 5825beeca19cSTrond Myklebust /* To keep mutex tracking happy */ 5826beeca19cSTrond Myklebust mutex_unlock(&stp->st_mutex); 5827beeca19cSTrond Myklebust stp = retstp; 5828beeca19cSTrond Myklebust } 5829beeca19cSTrond Myklebust return stp; 58301da177e4SLinus Torvalds } 58311da177e4SLinus Torvalds 5832c53530daSJeff Layton static struct nfs4_ol_stateid * 5833356a95ecSJeff Layton find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, 5834356a95ecSJeff Layton struct inode *inode, struct nfs4_ol_stateid *ost, 5835356a95ecSJeff Layton bool *new) 5836356a95ecSJeff Layton { 5837356a95ecSJeff Layton struct nfs4_stid *ns = NULL; 5838356a95ecSJeff Layton struct nfs4_ol_stateid *lst; 5839356a95ecSJeff Layton struct nfs4_openowner *oo = openowner(ost->st_stateowner); 5840356a95ecSJeff Layton struct nfs4_client *clp = oo->oo_owner.so_client; 5841356a95ecSJeff Layton 5842beeca19cSTrond Myklebust *new = false; 5843356a95ecSJeff Layton spin_lock(&clp->cl_lock); 5844356a95ecSJeff Layton lst = find_lock_stateid(lo, fi); 5845356a95ecSJeff Layton spin_unlock(&clp->cl_lock); 5846beeca19cSTrond Myklebust if (lst != NULL) { 5847beeca19cSTrond Myklebust if (nfsd4_lock_ol_stateid(lst) == nfs_ok) 5848beeca19cSTrond Myklebust goto out; 5849beeca19cSTrond Myklebust nfs4_put_stid(&lst->st_stid); 5850beeca19cSTrond Myklebust } 5851d19fb70dSKinglong Mee ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); 5852356a95ecSJeff Layton if (ns == NULL) 5853356a95ecSJeff Layton return NULL; 5854356a95ecSJeff Layton 5855beeca19cSTrond Myklebust lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); 5856beeca19cSTrond Myklebust if (lst == openlockstateid(ns)) 5857356a95ecSJeff Layton *new = true; 5858beeca19cSTrond Myklebust else 5859356a95ecSJeff Layton nfs4_put_stid(ns); 5860beeca19cSTrond Myklebust out: 5861356a95ecSJeff Layton return lst; 5862356a95ecSJeff Layton } 5863c53530daSJeff Layton 5864fd39ca9aSNeilBrown static int 58651da177e4SLinus Torvalds check_lock_length(u64 offset, u64 length) 58661da177e4SLinus Torvalds { 586787df4de8SBenny Halevy return ((length == 0) || ((length != NFS4_MAX_UINT64) && 5868e7969315SKinglong Mee (length > ~offset))); 58691da177e4SLinus Torvalds } 58701da177e4SLinus Torvalds 5871dcef0413SJ. Bruce Fields static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) 58720997b173SJ. Bruce Fields { 587311b9164aSTrond Myklebust struct nfs4_file *fp = lock_stp->st_stid.sc_file; 58740997b173SJ. Bruce Fields 58757214e860SJeff Layton lockdep_assert_held(&fp->fi_lock); 58767214e860SJeff Layton 587782c5ff1bSJeff Layton if (test_access(access, lock_stp)) 58780997b173SJ. Bruce Fields return; 587912659651SJeff Layton __nfs4_file_get_access(fp, access); 588082c5ff1bSJeff Layton set_access(access, lock_stp); 58810997b173SJ. Bruce Fields } 58820997b173SJ. Bruce Fields 5883356a95ecSJeff Layton static __be32 5884356a95ecSJeff Layton lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, 5885356a95ecSJeff Layton struct nfs4_ol_stateid *ost, 5886356a95ecSJeff Layton struct nfsd4_lock *lock, 5887dd257933SJeff Layton struct nfs4_ol_stateid **plst, bool *new) 588864a284d0SJ. Bruce Fields { 58895db1c03fSJeff Layton __be32 status; 589011b9164aSTrond Myklebust struct nfs4_file *fi = ost->st_stid.sc_file; 589164a284d0SJ. Bruce Fields struct nfs4_openowner *oo = openowner(ost->st_stateowner); 589264a284d0SJ. Bruce Fields struct nfs4_client *cl = oo->oo_owner.so_client; 58932b0143b5SDavid Howells struct inode *inode = d_inode(cstate->current_fh.fh_dentry); 589464a284d0SJ. Bruce Fields struct nfs4_lockowner *lo; 5895dd257933SJeff Layton struct nfs4_ol_stateid *lst; 589664a284d0SJ. Bruce Fields unsigned int strhashval; 589764a284d0SJ. Bruce Fields 5898c8623999SKinglong Mee lo = find_lockowner_str(cl, &lock->lk_new_owner); 5899c53530daSJeff Layton if (!lo) { 590076f6c9e1SKinglong Mee strhashval = ownerstr_hashval(&lock->lk_new_owner); 590164a284d0SJ. Bruce Fields lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); 590264a284d0SJ. Bruce Fields if (lo == NULL) 590364a284d0SJ. Bruce Fields return nfserr_jukebox; 5904c53530daSJeff Layton } else { 5905c53530daSJeff Layton /* with an existing lockowner, seqids must be the same */ 59065db1c03fSJeff Layton status = nfserr_bad_seqid; 5907c53530daSJeff Layton if (!cstate->minorversion && 5908c53530daSJeff Layton lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) 59095db1c03fSJeff Layton goto out; 5910c53530daSJeff Layton } 5911c53530daSJeff Layton 5912dd257933SJeff Layton lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); 5913dd257933SJeff Layton if (lst == NULL) { 59145db1c03fSJeff Layton status = nfserr_jukebox; 59155db1c03fSJeff Layton goto out; 591664a284d0SJ. Bruce Fields } 5917dd257933SJeff Layton 59185db1c03fSJeff Layton status = nfs_ok; 5919dd257933SJeff Layton *plst = lst; 59205db1c03fSJeff Layton out: 59215db1c03fSJeff Layton nfs4_put_stateowner(&lo->lo_owner); 59225db1c03fSJeff Layton return status; 592364a284d0SJ. Bruce Fields } 592464a284d0SJ. Bruce Fields 59251da177e4SLinus Torvalds /* 59261da177e4SLinus Torvalds * LOCK operation 59271da177e4SLinus Torvalds */ 5928b37ad28bSAl Viro __be32 5929ca364317SJ.Bruce Fields nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 5930eb69853dSChristoph Hellwig union nfsd4_op_u *u) 59311da177e4SLinus Torvalds { 5932eb69853dSChristoph Hellwig struct nfsd4_lock *lock = &u->lock; 5933fe0750e5SJ. Bruce Fields struct nfs4_openowner *open_sop = NULL; 5934fe0750e5SJ. Bruce Fields struct nfs4_lockowner *lock_sop = NULL; 59353d0fabd5STrond Myklebust struct nfs4_ol_stateid *lock_stp = NULL; 59360667b1e9STrond Myklebust struct nfs4_ol_stateid *open_stp = NULL; 59377214e860SJeff Layton struct nfs4_file *fp; 59387d947842SJ. Bruce Fields struct file *filp = NULL; 593976d348faSJeff Layton struct nfsd4_blocked_lock *nbl = NULL; 594021179d81SJeff Layton struct file_lock *file_lock = NULL; 594121179d81SJeff Layton struct file_lock *conflock = NULL; 5942b37ad28bSAl Viro __be32 status = 0; 5943b34f27aaSJ. Bruce Fields int lkflg; 5944b8dd7b9aSAl Viro int err; 59455db1c03fSJeff Layton bool new = false; 594676d348faSJeff Layton unsigned char fl_type; 594776d348faSJeff Layton unsigned int fl_flags = FL_POSIX; 59483320fef1SStanislav Kinsbursky struct net *net = SVC_NET(rqstp); 59493320fef1SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 59501da177e4SLinus Torvalds 59511da177e4SLinus Torvalds dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", 59521da177e4SLinus Torvalds (long long) lock->lk_offset, 59531da177e4SLinus Torvalds (long long) lock->lk_length); 59541da177e4SLinus Torvalds 59551da177e4SLinus Torvalds if (check_lock_length(lock->lk_offset, lock->lk_length)) 59561da177e4SLinus Torvalds return nfserr_inval; 59571da177e4SLinus Torvalds 5958ca364317SJ.Bruce Fields if ((status = fh_verify(rqstp, &cstate->current_fh, 59598837abcaSMiklos Szeredi S_IFREG, NFSD_MAY_LOCK))) { 5960a6f6ef2fSAndy Adamson dprintk("NFSD: nfsd4_lock: permission denied!\n"); 5961a6f6ef2fSAndy Adamson return status; 5962a6f6ef2fSAndy Adamson } 5963a6f6ef2fSAndy Adamson 59641da177e4SLinus Torvalds if (lock->lk_is_new) { 5965684e5638SJ. Bruce Fields if (nfsd4_has_session(cstate)) 5966684e5638SJ. Bruce Fields /* See rfc 5661 18.10.3: given clientid is ignored: */ 596776f6c9e1SKinglong Mee memcpy(&lock->lk_new_clientid, 5968684e5638SJ. Bruce Fields &cstate->session->se_client->cl_clientid, 5969684e5638SJ. Bruce Fields sizeof(clientid_t)); 5970684e5638SJ. Bruce Fields 59711da177e4SLinus Torvalds status = nfserr_stale_clientid; 59722c142baaSStanislav Kinsbursky if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) 59731da177e4SLinus Torvalds goto out; 59741da177e4SLinus Torvalds 59751da177e4SLinus Torvalds /* validate and update open stateid and open seqid */ 5976c0a5d93eSJ. Bruce Fields status = nfs4_preprocess_confirmed_seqid_op(cstate, 59771da177e4SLinus Torvalds lock->lk_new_open_seqid, 59781da177e4SLinus Torvalds &lock->lk_new_open_stateid, 59793320fef1SStanislav Kinsbursky &open_stp, nn); 598037515177SNeilBrown if (status) 59811da177e4SLinus Torvalds goto out; 5982feb9dad5SOleg Drokin mutex_unlock(&open_stp->st_mutex); 5983fe0750e5SJ. Bruce Fields open_sop = openowner(open_stp->st_stateowner); 5984b34f27aaSJ. Bruce Fields status = nfserr_bad_stateid; 5985684e5638SJ. Bruce Fields if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, 598676f6c9e1SKinglong Mee &lock->lk_new_clientid)) 5987b34f27aaSJ. Bruce Fields goto out; 598864a284d0SJ. Bruce Fields status = lookup_or_create_lock_state(cstate, open_stp, lock, 59895db1c03fSJeff Layton &lock_stp, &new); 59903d0fabd5STrond Myklebust } else { 5991dd453dfdSBenny Halevy status = nfs4_preprocess_seqid_op(cstate, 59921da177e4SLinus Torvalds lock->lk_old_lock_seqid, 59931da177e4SLinus Torvalds &lock->lk_old_lock_stateid, 59943320fef1SStanislav Kinsbursky NFS4_LOCK_STID, &lock_stp, nn); 59953d0fabd5STrond Myklebust } 59961da177e4SLinus Torvalds if (status) 59971da177e4SLinus Torvalds goto out; 5998fe0750e5SJ. Bruce Fields lock_sop = lockowner(lock_stp->st_stateowner); 59991da177e4SLinus Torvalds 6000b34f27aaSJ. Bruce Fields lkflg = setlkflg(lock->lk_type); 6001b34f27aaSJ. Bruce Fields status = nfs4_check_openmode(lock_stp, lkflg); 6002b34f27aaSJ. Bruce Fields if (status) 6003b34f27aaSJ. Bruce Fields goto out; 6004b34f27aaSJ. Bruce Fields 60050dd395dcSNeilBrown status = nfserr_grace; 60063320fef1SStanislav Kinsbursky if (locks_in_grace(net) && !lock->lk_reclaim) 60070dd395dcSNeilBrown goto out; 60080dd395dcSNeilBrown status = nfserr_no_grace; 60093320fef1SStanislav Kinsbursky if (!locks_in_grace(net) && lock->lk_reclaim) 60100dd395dcSNeilBrown goto out; 60110dd395dcSNeilBrown 601211b9164aSTrond Myklebust fp = lock_stp->st_stid.sc_file; 60131da177e4SLinus Torvalds switch (lock->lk_type) { 60141da177e4SLinus Torvalds case NFS4_READW_LT: 601576d348faSJeff Layton if (nfsd4_has_session(cstate)) 601676d348faSJeff Layton fl_flags |= FL_SLEEP; 601776d348faSJeff Layton /* Fallthrough */ 601876d348faSJeff Layton case NFS4_READ_LT: 60197214e860SJeff Layton spin_lock(&fp->fi_lock); 60207214e860SJeff Layton filp = find_readable_file_locked(fp); 60210997b173SJ. Bruce Fields if (filp) 60220997b173SJ. Bruce Fields get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); 60237214e860SJeff Layton spin_unlock(&fp->fi_lock); 602476d348faSJeff Layton fl_type = F_RDLCK; 60251da177e4SLinus Torvalds break; 60261da177e4SLinus Torvalds case NFS4_WRITEW_LT: 602776d348faSJeff Layton if (nfsd4_has_session(cstate)) 602876d348faSJeff Layton fl_flags |= FL_SLEEP; 602976d348faSJeff Layton /* Fallthrough */ 603076d348faSJeff Layton case NFS4_WRITE_LT: 60317214e860SJeff Layton spin_lock(&fp->fi_lock); 60327214e860SJeff Layton filp = find_writeable_file_locked(fp); 60330997b173SJ. Bruce Fields if (filp) 60340997b173SJ. Bruce Fields get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); 60357214e860SJeff Layton spin_unlock(&fp->fi_lock); 603676d348faSJeff Layton fl_type = F_WRLCK; 60371da177e4SLinus Torvalds break; 60381da177e4SLinus Torvalds default: 60391da177e4SLinus Torvalds status = nfserr_inval; 60401da177e4SLinus Torvalds goto out; 60411da177e4SLinus Torvalds } 604276d348faSJeff Layton 6043f9d7562fSJ. Bruce Fields if (!filp) { 6044f9d7562fSJ. Bruce Fields status = nfserr_openmode; 6045f9d7562fSJ. Bruce Fields goto out; 6046f9d7562fSJ. Bruce Fields } 6047aef9583bSKinglong Mee 604876d348faSJeff Layton nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); 604976d348faSJeff Layton if (!nbl) { 605076d348faSJeff Layton dprintk("NFSD: %s: unable to allocate block!\n", __func__); 605176d348faSJeff Layton status = nfserr_jukebox; 605276d348faSJeff Layton goto out; 605376d348faSJeff Layton } 605476d348faSJeff Layton 605576d348faSJeff Layton file_lock = &nbl->nbl_lock; 605676d348faSJeff Layton file_lock->fl_type = fl_type; 6057aef9583bSKinglong Mee file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); 605821179d81SJeff Layton file_lock->fl_pid = current->tgid; 605921179d81SJeff Layton file_lock->fl_file = filp; 606076d348faSJeff Layton file_lock->fl_flags = fl_flags; 606121179d81SJeff Layton file_lock->fl_lmops = &nfsd_posix_mng_ops; 606221179d81SJeff Layton file_lock->fl_start = lock->lk_offset; 606321179d81SJeff Layton file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); 606421179d81SJeff Layton nfs4_transform_lock_offset(file_lock); 60651da177e4SLinus Torvalds 606621179d81SJeff Layton conflock = locks_alloc_lock(); 606721179d81SJeff Layton if (!conflock) { 606821179d81SJeff Layton dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 606921179d81SJeff Layton status = nfserr_jukebox; 607021179d81SJeff Layton goto out; 607121179d81SJeff Layton } 60721da177e4SLinus Torvalds 607376d348faSJeff Layton if (fl_flags & FL_SLEEP) { 60747919d0a2SJeff Layton nbl->nbl_time = jiffies; 60750cc11a61SJeff Layton spin_lock(&nn->blocked_locks_lock); 607676d348faSJeff Layton list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); 60777919d0a2SJeff Layton list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); 60780cc11a61SJeff Layton spin_unlock(&nn->blocked_locks_lock); 607976d348faSJeff Layton } 608076d348faSJeff Layton 608121179d81SJeff Layton err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); 608276d348faSJeff Layton switch (err) { 60831da177e4SLinus Torvalds case 0: /* success! */ 60849767feb2SJeff Layton nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); 6085b8dd7b9aSAl Viro status = 0; 608603f318caSJ. Bruce Fields if (lock->lk_reclaim) 608703f318caSJ. Bruce Fields nn->somebody_reclaimed = true; 6088eb76b3fdSAndy Adamson break; 608976d348faSJeff Layton case FILE_LOCK_DEFERRED: 609076d348faSJeff Layton nbl = NULL; 609176d348faSJeff Layton /* Fallthrough */ 609276d348faSJeff Layton case -EAGAIN: /* conflock holds conflicting lock */ 6093eb76b3fdSAndy Adamson status = nfserr_denied; 6094eb76b3fdSAndy Adamson dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); 609521179d81SJeff Layton nfs4_set_lock_denied(conflock, &lock->lk_denied); 6096eb76b3fdSAndy Adamson break; 609776d348faSJeff Layton case -EDEADLK: 60981da177e4SLinus Torvalds status = nfserr_deadlock; 6099eb76b3fdSAndy Adamson break; 61001da177e4SLinus Torvalds default: 6101fd85b817SMarc Eshel dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); 61023e772463SJ. Bruce Fields status = nfserrno(err); 6103eb76b3fdSAndy Adamson break; 61041da177e4SLinus Torvalds } 61051da177e4SLinus Torvalds out: 610676d348faSJeff Layton if (nbl) { 610776d348faSJeff Layton /* dequeue it if we queued it before */ 610876d348faSJeff Layton if (fl_flags & FL_SLEEP) { 61090cc11a61SJeff Layton spin_lock(&nn->blocked_locks_lock); 611076d348faSJeff Layton list_del_init(&nbl->nbl_list); 61117919d0a2SJeff Layton list_del_init(&nbl->nbl_lru); 61120cc11a61SJeff Layton spin_unlock(&nn->blocked_locks_lock); 611376d348faSJeff Layton } 611476d348faSJeff Layton free_blocked_lock(nbl); 611576d348faSJeff Layton } 6116de18643dSTrond Myklebust if (filp) 6117de18643dSTrond Myklebust fput(filp); 61185db1c03fSJeff Layton if (lock_stp) { 61195db1c03fSJeff Layton /* Bump seqid manually if the 4.0 replay owner is openowner */ 61205db1c03fSJeff Layton if (cstate->replay_owner && 61215db1c03fSJeff Layton cstate->replay_owner != &lock_sop->lo_owner && 61225db1c03fSJeff Layton seqid_mutating_err(ntohl(status))) 61235db1c03fSJeff Layton lock_sop->lo_owner.so_seqid++; 61245db1c03fSJeff Layton 61255db1c03fSJeff Layton /* 61265db1c03fSJeff Layton * If this is a new, never-before-used stateid, and we are 61275db1c03fSJeff Layton * returning an error, then just go ahead and release it. 61285db1c03fSJeff Layton */ 612925020720SJ. Bruce Fields if (status && new) 61305db1c03fSJeff Layton release_lock_stateid(lock_stp); 6131beeca19cSTrond Myklebust 6132beeca19cSTrond Myklebust mutex_unlock(&lock_stp->st_mutex); 61335db1c03fSJeff Layton 61343d0fabd5STrond Myklebust nfs4_put_stid(&lock_stp->st_stid); 61355db1c03fSJeff Layton } 61360667b1e9STrond Myklebust if (open_stp) 61370667b1e9STrond Myklebust nfs4_put_stid(&open_stp->st_stid); 61389411b1d4SJ. Bruce Fields nfsd4_bump_seqid(cstate, status); 613921179d81SJeff Layton if (conflock) 614021179d81SJeff Layton locks_free_lock(conflock); 61411da177e4SLinus Torvalds return status; 61421da177e4SLinus Torvalds } 61431da177e4SLinus Torvalds 61441da177e4SLinus Torvalds /* 614555ef1274SJ. Bruce Fields * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, 614655ef1274SJ. Bruce Fields * so we do a temporary open here just to get an open file to pass to 614755ef1274SJ. Bruce Fields * vfs_test_lock. (Arguably perhaps test_lock should be done with an 614855ef1274SJ. Bruce Fields * inode operation.) 614955ef1274SJ. Bruce Fields */ 615004da6e9dSAl Viro static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) 615155ef1274SJ. Bruce Fields { 615255ef1274SJ. Bruce Fields struct file *file; 615304da6e9dSAl Viro __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); 615404da6e9dSAl Viro if (!err) { 615504da6e9dSAl Viro err = nfserrno(vfs_test_lock(file, lock)); 6156fd891454SChristoph Hellwig fput(file); 615704da6e9dSAl Viro } 615855ef1274SJ. Bruce Fields return err; 615955ef1274SJ. Bruce Fields } 616055ef1274SJ. Bruce Fields 616155ef1274SJ. Bruce Fields /* 61621da177e4SLinus Torvalds * LOCKT operation 61631da177e4SLinus Torvalds */ 6164b37ad28bSAl Viro __be32 6165ca364317SJ.Bruce Fields nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 6166eb69853dSChristoph Hellwig union nfsd4_op_u *u) 61671da177e4SLinus Torvalds { 6168eb69853dSChristoph Hellwig struct nfsd4_lockt *lockt = &u->lockt; 616921179d81SJeff Layton struct file_lock *file_lock = NULL; 61705db1c03fSJeff Layton struct nfs4_lockowner *lo = NULL; 6171b37ad28bSAl Viro __be32 status; 61727f2210faSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 61731da177e4SLinus Torvalds 61745ccb0066SStanislav Kinsbursky if (locks_in_grace(SVC_NET(rqstp))) 61751da177e4SLinus Torvalds return nfserr_grace; 61761da177e4SLinus Torvalds 61771da177e4SLinus Torvalds if (check_lock_length(lockt->lt_offset, lockt->lt_length)) 61781da177e4SLinus Torvalds return nfserr_inval; 61791da177e4SLinus Torvalds 61809b2ef62bSJ. Bruce Fields if (!nfsd4_has_session(cstate)) { 61814b24ca7dSJeff Layton status = lookup_clientid(&lockt->lt_clientid, cstate, nn); 61829b2ef62bSJ. Bruce Fields if (status) 61831da177e4SLinus Torvalds goto out; 61849b2ef62bSJ. Bruce Fields } 61851da177e4SLinus Torvalds 618675c096f7SJ. Bruce Fields if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) 61871da177e4SLinus Torvalds goto out; 61881da177e4SLinus Torvalds 618921179d81SJeff Layton file_lock = locks_alloc_lock(); 619021179d81SJeff Layton if (!file_lock) { 619121179d81SJeff Layton dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 619221179d81SJeff Layton status = nfserr_jukebox; 619321179d81SJeff Layton goto out; 619421179d81SJeff Layton } 61956cd90662SKinglong Mee 61961da177e4SLinus Torvalds switch (lockt->lt_type) { 61971da177e4SLinus Torvalds case NFS4_READ_LT: 61981da177e4SLinus Torvalds case NFS4_READW_LT: 619921179d81SJeff Layton file_lock->fl_type = F_RDLCK; 62001da177e4SLinus Torvalds break; 62011da177e4SLinus Torvalds case NFS4_WRITE_LT: 62021da177e4SLinus Torvalds case NFS4_WRITEW_LT: 620321179d81SJeff Layton file_lock->fl_type = F_WRLCK; 62041da177e4SLinus Torvalds break; 62051da177e4SLinus Torvalds default: 62062fdada03SJ. Bruce Fields dprintk("NFSD: nfs4_lockt: bad lock type!\n"); 62071da177e4SLinus Torvalds status = nfserr_inval; 62081da177e4SLinus Torvalds goto out; 62091da177e4SLinus Torvalds } 62101da177e4SLinus Torvalds 6211c8623999SKinglong Mee lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); 6212fe0750e5SJ. Bruce Fields if (lo) 621321179d81SJeff Layton file_lock->fl_owner = (fl_owner_t)lo; 621421179d81SJeff Layton file_lock->fl_pid = current->tgid; 621521179d81SJeff Layton file_lock->fl_flags = FL_POSIX; 62161da177e4SLinus Torvalds 621721179d81SJeff Layton file_lock->fl_start = lockt->lt_offset; 621821179d81SJeff Layton file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); 62191da177e4SLinus Torvalds 622021179d81SJeff Layton nfs4_transform_lock_offset(file_lock); 62211da177e4SLinus Torvalds 622221179d81SJeff Layton status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); 622304da6e9dSAl Viro if (status) 6224fd85b817SMarc Eshel goto out; 622504da6e9dSAl Viro 622621179d81SJeff Layton if (file_lock->fl_type != F_UNLCK) { 62271da177e4SLinus Torvalds status = nfserr_denied; 622821179d81SJeff Layton nfs4_set_lock_denied(file_lock, &lockt->lt_denied); 62291da177e4SLinus Torvalds } 62301da177e4SLinus Torvalds out: 62315db1c03fSJeff Layton if (lo) 62325db1c03fSJeff Layton nfs4_put_stateowner(&lo->lo_owner); 623321179d81SJeff Layton if (file_lock) 623421179d81SJeff Layton locks_free_lock(file_lock); 62351da177e4SLinus Torvalds return status; 62361da177e4SLinus Torvalds } 62371da177e4SLinus Torvalds 6238b37ad28bSAl Viro __be32 6239ca364317SJ.Bruce Fields nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, 6240eb69853dSChristoph Hellwig union nfsd4_op_u *u) 62411da177e4SLinus Torvalds { 6242eb69853dSChristoph Hellwig struct nfsd4_locku *locku = &u->locku; 6243dcef0413SJ. Bruce Fields struct nfs4_ol_stateid *stp; 62441da177e4SLinus Torvalds struct file *filp = NULL; 624521179d81SJeff Layton struct file_lock *file_lock = NULL; 6246b37ad28bSAl Viro __be32 status; 6247b8dd7b9aSAl Viro int err; 62483320fef1SStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 62491da177e4SLinus Torvalds 62501da177e4SLinus Torvalds dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", 62511da177e4SLinus Torvalds (long long) locku->lu_offset, 62521da177e4SLinus Torvalds (long long) locku->lu_length); 62531da177e4SLinus Torvalds 62541da177e4SLinus Torvalds if (check_lock_length(locku->lu_offset, locku->lu_length)) 62551da177e4SLinus Torvalds return nfserr_inval; 62561da177e4SLinus Torvalds 62579072d5c6SJ. Bruce Fields status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, 62583320fef1SStanislav Kinsbursky &locku->lu_stateid, NFS4_LOCK_STID, 62593320fef1SStanislav Kinsbursky &stp, nn); 62609072d5c6SJ. Bruce Fields if (status) 62611da177e4SLinus Torvalds goto out; 626211b9164aSTrond Myklebust filp = find_any_file(stp->st_stid.sc_file); 6263f9d7562fSJ. Bruce Fields if (!filp) { 6264f9d7562fSJ. Bruce Fields status = nfserr_lock_range; 6265858cc573STrond Myklebust goto put_stateid; 6266f9d7562fSJ. Bruce Fields } 626721179d81SJeff Layton file_lock = locks_alloc_lock(); 626821179d81SJeff Layton if (!file_lock) { 626921179d81SJeff Layton dprintk("NFSD: %s: unable to allocate lock!\n", __func__); 627021179d81SJeff Layton status = nfserr_jukebox; 6271de18643dSTrond Myklebust goto fput; 627221179d81SJeff Layton } 62736cd90662SKinglong Mee 627421179d81SJeff Layton file_lock->fl_type = F_UNLCK; 6275aef9583bSKinglong Mee file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); 627621179d81SJeff Layton file_lock->fl_pid = current->tgid; 627721179d81SJeff Layton file_lock->fl_file = filp; 627821179d81SJeff Layton file_lock->fl_flags = FL_POSIX; 627921179d81SJeff Layton file_lock->fl_lmops = &nfsd_posix_mng_ops; 628021179d81SJeff Layton file_lock->fl_start = locku->lu_offset; 62811da177e4SLinus Torvalds 628221179d81SJeff Layton file_lock->fl_end = last_byte_offset(locku->lu_offset, 628321179d81SJeff Layton locku->lu_length); 628421179d81SJeff Layton nfs4_transform_lock_offset(file_lock); 62851da177e4SLinus Torvalds 628621179d81SJeff Layton err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); 6287b8dd7b9aSAl Viro if (err) { 6288fd85b817SMarc Eshel dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); 62891da177e4SLinus Torvalds goto out_nfserr; 62901da177e4SLinus Torvalds } 62919767feb2SJeff Layton nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); 6292de18643dSTrond Myklebust fput: 6293de18643dSTrond Myklebust fput(filp); 6294858cc573STrond Myklebust put_stateid: 6295feb9dad5SOleg Drokin mutex_unlock(&stp->st_mutex); 6296858cc573STrond Myklebust nfs4_put_stid(&stp->st_stid); 62971da177e4SLinus Torvalds out: 62989411b1d4SJ. Bruce Fields nfsd4_bump_seqid(cstate, status); 629921179d81SJeff Layton if (file_lock) 630021179d81SJeff Layton locks_free_lock(file_lock); 63011da177e4SLinus Torvalds return status; 63021da177e4SLinus Torvalds 63031da177e4SLinus Torvalds out_nfserr: 6304b8dd7b9aSAl Viro status = nfserrno(err); 6305de18643dSTrond Myklebust goto fput; 63061da177e4SLinus Torvalds } 63071da177e4SLinus Torvalds 63081da177e4SLinus Torvalds /* 63091da177e4SLinus Torvalds * returns 6310f9c00c3aSJeff Layton * true: locks held by lockowner 6311f9c00c3aSJeff Layton * false: no locks held by lockowner 63121da177e4SLinus Torvalds */ 6313f9c00c3aSJeff Layton static bool 6314f9c00c3aSJeff Layton check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) 63151da177e4SLinus Torvalds { 6316bd61e0a9SJeff Layton struct file_lock *fl; 6317f9c00c3aSJeff Layton int status = false; 6318f9c00c3aSJeff Layton struct file *filp = find_any_file(fp); 6319f9c00c3aSJeff Layton struct inode *inode; 6320bd61e0a9SJeff Layton struct file_lock_context *flctx; 6321f9c00c3aSJeff Layton 6322f9c00c3aSJeff Layton if (!filp) { 6323f9c00c3aSJeff Layton /* Any valid lock stateid should have some sort of access */ 6324f9c00c3aSJeff Layton WARN_ON_ONCE(1); 6325f9c00c3aSJeff Layton return status; 6326f9c00c3aSJeff Layton } 6327f9c00c3aSJeff Layton 632864bed6cbSAmir Goldstein inode = locks_inode(filp); 6329bd61e0a9SJeff Layton flctx = inode->i_flctx; 63301da177e4SLinus Torvalds 6331bd61e0a9SJeff Layton if (flctx && !list_empty_careful(&flctx->flc_posix)) { 63326109c850SJeff Layton spin_lock(&flctx->flc_lock); 6333bd61e0a9SJeff Layton list_for_each_entry(fl, &flctx->flc_posix, fl_list) { 6334bd61e0a9SJeff Layton if (fl->fl_owner == (fl_owner_t)lowner) { 6335f9c00c3aSJeff Layton status = true; 6336f9c00c3aSJeff Layton break; 63371da177e4SLinus Torvalds } 6338796dadfdSJ. Bruce Fields } 63396109c850SJeff Layton spin_unlock(&flctx->flc_lock); 6340bd61e0a9SJeff Layton } 6341f9c00c3aSJeff Layton fput(filp); 63421da177e4SLinus Torvalds return status; 63431da177e4SLinus Torvalds } 63441da177e4SLinus Torvalds 6345b37ad28bSAl Viro __be32 6346b591480bSJ.Bruce Fields nfsd4_release_lockowner(struct svc_rqst *rqstp, 6347b591480bSJ.Bruce Fields struct nfsd4_compound_state *cstate, 6348eb69853dSChristoph Hellwig union nfsd4_op_u *u) 63491da177e4SLinus Torvalds { 6350eb69853dSChristoph Hellwig struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; 63511da177e4SLinus Torvalds clientid_t *clid = &rlockowner->rl_clientid; 6352882e9d25SJeff Layton struct nfs4_stateowner *sop; 6353882e9d25SJeff Layton struct nfs4_lockowner *lo = NULL; 6354dcef0413SJ. Bruce Fields struct nfs4_ol_stateid *stp; 63551da177e4SLinus Torvalds struct xdr_netobj *owner = &rlockowner->rl_owner; 6356d4f0489fSTrond Myklebust unsigned int hashval = ownerstr_hashval(owner); 6357b37ad28bSAl Viro __be32 status; 63587f2210faSStanislav Kinsbursky struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 6359c58c6610STrond Myklebust struct nfs4_client *clp; 636088584818SChuck Lever LIST_HEAD (reaplist); 63611da177e4SLinus Torvalds 63621da177e4SLinus Torvalds dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", 63631da177e4SLinus Torvalds clid->cl_boot, clid->cl_id); 63641da177e4SLinus Torvalds 63654b24ca7dSJeff Layton status = lookup_clientid(clid, cstate, nn); 63669b2ef62bSJ. Bruce Fields if (status) 636751f5e783STrond Myklebust return status; 63689b2ef62bSJ. Bruce Fields 6369d4f0489fSTrond Myklebust clp = cstate->clp; 6370fd44907cSJeff Layton /* Find the matching lock stateowner */ 6371d4f0489fSTrond Myklebust spin_lock(&clp->cl_lock); 6372882e9d25SJeff Layton list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], 6373d4f0489fSTrond Myklebust so_strhash) { 6374882e9d25SJeff Layton 6375882e9d25SJeff Layton if (sop->so_is_open_owner || !same_owner_str(sop, owner)) 637616bfdaafSJ. Bruce Fields continue; 6377882e9d25SJeff Layton 6378882e9d25SJeff Layton /* see if there are still any locks associated with it */ 6379882e9d25SJeff Layton lo = lockowner(sop); 6380882e9d25SJeff Layton list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { 6381882e9d25SJeff Layton if (check_for_locks(stp->st_stid.sc_file, lo)) { 6382882e9d25SJeff Layton status = nfserr_locks_held; 6383882e9d25SJeff Layton spin_unlock(&clp->cl_lock); 638451f5e783STrond Myklebust return status; 6385882e9d25SJeff Layton } 6386882e9d25SJeff Layton } 6387882e9d25SJeff Layton 6388b5971afaSKinglong Mee nfs4_get_stateowner(sop); 6389fd44907cSJeff Layton break; 6390fd44907cSJeff Layton } 639188584818SChuck Lever if (!lo) { 6392d4f0489fSTrond Myklebust spin_unlock(&clp->cl_lock); 639388584818SChuck Lever return status; 639488584818SChuck Lever } 639588584818SChuck Lever 639688584818SChuck Lever unhash_lockowner_locked(lo); 639788584818SChuck Lever while (!list_empty(&lo->lo_owner.so_stateids)) { 639888584818SChuck Lever stp = list_first_entry(&lo->lo_owner.so_stateids, 639988584818SChuck Lever struct nfs4_ol_stateid, 640088584818SChuck Lever st_perstateowner); 640188584818SChuck Lever WARN_ON(!unhash_lock_stateid(stp)); 640288584818SChuck Lever put_ol_stateid_locked(stp, &reaplist); 640388584818SChuck Lever } 640488584818SChuck Lever spin_unlock(&clp->cl_lock); 640588584818SChuck Lever free_ol_stateid_reaplist(&reaplist); 640668ef3bc3SJeff Layton remove_blocked_locks(lo); 640788584818SChuck Lever nfs4_put_stateowner(&lo->lo_owner); 640888584818SChuck Lever 64091da177e4SLinus Torvalds return status; 64101da177e4SLinus Torvalds } 64111da177e4SLinus Torvalds 64121da177e4SLinus Torvalds static inline struct nfs4_client_reclaim * 6413a55370a3SNeilBrown alloc_reclaim(void) 64141da177e4SLinus Torvalds { 6415a55370a3SNeilBrown return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); 64161da177e4SLinus Torvalds } 64171da177e4SLinus Torvalds 64180ce0c2b5SJeff Layton bool 641952e19c09SStanislav Kinsbursky nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) 6420c7b9a459SNeilBrown { 64210ce0c2b5SJeff Layton struct nfs4_client_reclaim *crp; 6422c7b9a459SNeilBrown 642352e19c09SStanislav Kinsbursky crp = nfsd4_find_reclaim_client(name, nn); 64240ce0c2b5SJeff Layton return (crp && crp->cr_clp); 6425c7b9a459SNeilBrown } 6426c7b9a459SNeilBrown 64271da177e4SLinus Torvalds /* 64281da177e4SLinus Torvalds * failure => all reset bets are off, nfserr_no_grace... 64291da177e4SLinus Torvalds */ 6430772a9bbbSJeff Layton struct nfs4_client_reclaim * 643152e19c09SStanislav Kinsbursky nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) 64321da177e4SLinus Torvalds { 64331da177e4SLinus Torvalds unsigned int strhashval; 6434772a9bbbSJeff Layton struct nfs4_client_reclaim *crp; 64351da177e4SLinus Torvalds 6436a55370a3SNeilBrown dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); 6437a55370a3SNeilBrown crp = alloc_reclaim(); 6438772a9bbbSJeff Layton if (crp) { 6439a55370a3SNeilBrown strhashval = clientstr_hashval(name); 64401da177e4SLinus Torvalds INIT_LIST_HEAD(&crp->cr_strhash); 644152e19c09SStanislav Kinsbursky list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); 6442a55370a3SNeilBrown memcpy(crp->cr_recdir, name, HEXDIR_LEN); 64430ce0c2b5SJeff Layton crp->cr_clp = NULL; 644452e19c09SStanislav Kinsbursky nn->reclaim_str_hashtbl_size++; 6445772a9bbbSJeff Layton } 6446772a9bbbSJeff Layton return crp; 64471da177e4SLinus Torvalds } 64481da177e4SLinus Torvalds 64492a4317c5SJeff Layton void 645052e19c09SStanislav Kinsbursky nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) 6451ce30e539SJeff Layton { 6452ce30e539SJeff Layton list_del(&crp->cr_strhash); 6453ce30e539SJeff Layton kfree(crp); 645452e19c09SStanislav Kinsbursky nn->reclaim_str_hashtbl_size--; 6455ce30e539SJeff Layton } 6456ce30e539SJeff Layton 6457ce30e539SJeff Layton void 645852e19c09SStanislav Kinsbursky nfs4_release_reclaim(struct nfsd_net *nn) 64591da177e4SLinus Torvalds { 64601da177e4SLinus Torvalds struct nfs4_client_reclaim *crp = NULL; 64611da177e4SLinus Torvalds int i; 64621da177e4SLinus Torvalds 64631da177e4SLinus Torvalds for (i = 0; i < CLIENT_HASH_SIZE; i++) { 646452e19c09SStanislav Kinsbursky while (!list_empty(&nn->reclaim_str_hashtbl[i])) { 646552e19c09SStanislav Kinsbursky crp = list_entry(nn->reclaim_str_hashtbl[i].next, 64661da177e4SLinus Torvalds struct nfs4_client_reclaim, cr_strhash); 646752e19c09SStanislav Kinsbursky nfs4_remove_reclaim_record(crp, nn); 64681da177e4SLinus Torvalds } 64691da177e4SLinus Torvalds } 6470063b0fb9SJ. Bruce Fields WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); 64711da177e4SLinus Torvalds } 64721da177e4SLinus Torvalds 64731da177e4SLinus Torvalds /* 64741da177e4SLinus Torvalds * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ 64752a4317c5SJeff Layton struct nfs4_client_reclaim * 647652e19c09SStanislav Kinsbursky nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) 64771da177e4SLinus Torvalds { 64781da177e4SLinus Torvalds unsigned int strhashval; 64791da177e4SLinus Torvalds struct nfs4_client_reclaim *crp = NULL; 64801da177e4SLinus Torvalds 6481278c931cSJeff Layton dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); 64821da177e4SLinus Torvalds 6483278c931cSJeff Layton strhashval = clientstr_hashval(recdir); 648452e19c09SStanislav Kinsbursky list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { 6485278c931cSJeff Layton if (same_name(crp->cr_recdir, recdir)) { 64861da177e4SLinus Torvalds return crp; 64871da177e4SLinus Torvalds } 64881da177e4SLinus Torvalds } 64891da177e4SLinus Torvalds return NULL; 64901da177e4SLinus Torvalds } 64911da177e4SLinus Torvalds 64921da177e4SLinus Torvalds /* 64931da177e4SLinus Torvalds * Called from OPEN. Look for clientid in reclaim list. 64941da177e4SLinus Torvalds */ 6495b37ad28bSAl Viro __be32 64960fe492dbSTrond Myklebust nfs4_check_open_reclaim(clientid_t *clid, 64970fe492dbSTrond Myklebust struct nfsd4_compound_state *cstate, 64980fe492dbSTrond Myklebust struct nfsd_net *nn) 64991da177e4SLinus Torvalds { 65000fe492dbSTrond Myklebust __be32 status; 6501a52d726bSJeff Layton 6502a52d726bSJeff Layton /* find clientid in conf_id_hashtbl */ 65030fe492dbSTrond Myklebust status = lookup_clientid(clid, cstate, nn); 65040fe492dbSTrond Myklebust if (status) 6505a52d726bSJeff Layton return nfserr_reclaim_bad; 6506a52d726bSJeff Layton 65073b3e7b72SJeff Layton if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) 65083b3e7b72SJeff Layton return nfserr_no_grace; 65093b3e7b72SJeff Layton 65100fe492dbSTrond Myklebust if (nfsd4_client_record_check(cstate->clp)) 65110fe492dbSTrond Myklebust return nfserr_reclaim_bad; 65120fe492dbSTrond Myklebust 65130fe492dbSTrond Myklebust return nfs_ok; 65141da177e4SLinus Torvalds } 65151da177e4SLinus Torvalds 651665178db4SBryan Schumaker #ifdef CONFIG_NFSD_FAULT_INJECTION 6517016200c3SJeff Layton static inline void 6518016200c3SJeff Layton put_client(struct nfs4_client *clp) 6519016200c3SJeff Layton { 6520016200c3SJeff Layton atomic_dec(&clp->cl_refcount); 6521016200c3SJeff Layton } 6522016200c3SJeff Layton 6523285abdeeSJeff Layton static struct nfs4_client * 6524285abdeeSJeff Layton nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) 6525285abdeeSJeff Layton { 6526285abdeeSJeff Layton struct nfs4_client *clp; 6527285abdeeSJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 6528285abdeeSJeff Layton nfsd_net_id); 6529285abdeeSJeff Layton 6530285abdeeSJeff Layton if (!nfsd_netns_ready(nn)) 6531285abdeeSJeff Layton return NULL; 6532285abdeeSJeff Layton 6533285abdeeSJeff Layton list_for_each_entry(clp, &nn->client_lru, cl_lru) { 6534285abdeeSJeff Layton if (memcmp(&clp->cl_addr, addr, addr_size) == 0) 6535285abdeeSJeff Layton return clp; 6536285abdeeSJeff Layton } 6537285abdeeSJeff Layton return NULL; 6538285abdeeSJeff Layton } 6539285abdeeSJeff Layton 65407ec0e36fSJeff Layton u64 6541285abdeeSJeff Layton nfsd_inject_print_clients(void) 65427ec0e36fSJeff Layton { 65437ec0e36fSJeff Layton struct nfs4_client *clp; 65447ec0e36fSJeff Layton u64 count = 0; 65457ec0e36fSJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 65467ec0e36fSJeff Layton nfsd_net_id); 65477ec0e36fSJeff Layton char buf[INET6_ADDRSTRLEN]; 65487ec0e36fSJeff Layton 65497ec0e36fSJeff Layton if (!nfsd_netns_ready(nn)) 65507ec0e36fSJeff Layton return 0; 65517ec0e36fSJeff Layton 65527ec0e36fSJeff Layton spin_lock(&nn->client_lock); 65537ec0e36fSJeff Layton list_for_each_entry(clp, &nn->client_lru, cl_lru) { 65547ec0e36fSJeff Layton rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); 65557ec0e36fSJeff Layton pr_info("NFS Client: %s\n", buf); 65567ec0e36fSJeff Layton ++count; 65577ec0e36fSJeff Layton } 65587ec0e36fSJeff Layton spin_unlock(&nn->client_lock); 65597ec0e36fSJeff Layton 65607ec0e36fSJeff Layton return count; 65617ec0e36fSJeff Layton } 656265178db4SBryan Schumaker 6563a0926d15SJeff Layton u64 6564285abdeeSJeff Layton nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) 6565a0926d15SJeff Layton { 6566a0926d15SJeff Layton u64 count = 0; 6567a0926d15SJeff Layton struct nfs4_client *clp; 6568a0926d15SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 6569a0926d15SJeff Layton nfsd_net_id); 6570a0926d15SJeff Layton 6571a0926d15SJeff Layton if (!nfsd_netns_ready(nn)) 6572a0926d15SJeff Layton return count; 6573a0926d15SJeff Layton 6574a0926d15SJeff Layton spin_lock(&nn->client_lock); 6575a0926d15SJeff Layton clp = nfsd_find_client(addr, addr_size); 6576a0926d15SJeff Layton if (clp) { 6577a0926d15SJeff Layton if (mark_client_expired_locked(clp) == nfs_ok) 6578a0926d15SJeff Layton ++count; 6579a0926d15SJeff Layton else 6580a0926d15SJeff Layton clp = NULL; 6581a0926d15SJeff Layton } 6582a0926d15SJeff Layton spin_unlock(&nn->client_lock); 6583a0926d15SJeff Layton 6584a0926d15SJeff Layton if (clp) 6585a0926d15SJeff Layton expire_client(clp); 6586a0926d15SJeff Layton 6587a0926d15SJeff Layton return count; 6588a0926d15SJeff Layton } 6589a0926d15SJeff Layton 659069fc9edfSJeff Layton u64 6591285abdeeSJeff Layton nfsd_inject_forget_clients(u64 max) 659269fc9edfSJeff Layton { 659369fc9edfSJeff Layton u64 count = 0; 659469fc9edfSJeff Layton struct nfs4_client *clp, *next; 659569fc9edfSJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 659669fc9edfSJeff Layton nfsd_net_id); 659769fc9edfSJeff Layton LIST_HEAD(reaplist); 659869fc9edfSJeff Layton 659969fc9edfSJeff Layton if (!nfsd_netns_ready(nn)) 660069fc9edfSJeff Layton return count; 660169fc9edfSJeff Layton 660269fc9edfSJeff Layton spin_lock(&nn->client_lock); 660369fc9edfSJeff Layton list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { 660469fc9edfSJeff Layton if (mark_client_expired_locked(clp) == nfs_ok) { 660569fc9edfSJeff Layton list_add(&clp->cl_lru, &reaplist); 660669fc9edfSJeff Layton if (max != 0 && ++count >= max) 660769fc9edfSJeff Layton break; 660869fc9edfSJeff Layton } 660969fc9edfSJeff Layton } 661069fc9edfSJeff Layton spin_unlock(&nn->client_lock); 661169fc9edfSJeff Layton 661269fc9edfSJeff Layton list_for_each_entry_safe(clp, next, &reaplist, cl_lru) 661369fc9edfSJeff Layton expire_client(clp); 661469fc9edfSJeff Layton 661569fc9edfSJeff Layton return count; 661669fc9edfSJeff Layton } 661769fc9edfSJeff Layton 6618184c1847SBryan Schumaker static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, 6619184c1847SBryan Schumaker const char *type) 6620184c1847SBryan Schumaker { 6621184c1847SBryan Schumaker char buf[INET6_ADDRSTRLEN]; 66220a5c33e2SBryan Schumaker rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); 6623184c1847SBryan Schumaker printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); 6624184c1847SBryan Schumaker } 6625184c1847SBryan Schumaker 6626016200c3SJeff Layton static void 6627016200c3SJeff Layton nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, 6628016200c3SJeff Layton struct list_head *collect) 6629016200c3SJeff Layton { 6630016200c3SJeff Layton struct nfs4_client *clp = lst->st_stid.sc_client; 6631016200c3SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 6632016200c3SJeff Layton nfsd_net_id); 6633016200c3SJeff Layton 6634016200c3SJeff Layton if (!collect) 6635016200c3SJeff Layton return; 6636016200c3SJeff Layton 6637016200c3SJeff Layton lockdep_assert_held(&nn->client_lock); 6638016200c3SJeff Layton atomic_inc(&clp->cl_refcount); 6639016200c3SJeff Layton list_add(&lst->st_locks, collect); 6640016200c3SJeff Layton } 6641016200c3SJeff Layton 66423c87b9b7STrond Myklebust static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, 66433738d50eSJeff Layton struct list_head *collect, 6644e8568739SJeff Layton bool (*func)(struct nfs4_ol_stateid *)) 6645fc29171fSBryan Schumaker { 6646fc29171fSBryan Schumaker struct nfs4_openowner *oop; 6647fc29171fSBryan Schumaker struct nfs4_ol_stateid *stp, *st_next; 66483c87b9b7STrond Myklebust struct nfs4_ol_stateid *lst, *lst_next; 6649fc29171fSBryan Schumaker u64 count = 0; 6650fc29171fSBryan Schumaker 6651016200c3SJeff Layton spin_lock(&clp->cl_lock); 6652fc29171fSBryan Schumaker list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { 66533c87b9b7STrond Myklebust list_for_each_entry_safe(stp, st_next, 66543c87b9b7STrond Myklebust &oop->oo_owner.so_stateids, st_perstateowner) { 66553c87b9b7STrond Myklebust list_for_each_entry_safe(lst, lst_next, 66563c87b9b7STrond Myklebust &stp->st_locks, st_locks) { 66573738d50eSJeff Layton if (func) { 6658e8568739SJeff Layton if (func(lst)) 6659016200c3SJeff Layton nfsd_inject_add_lock_to_list(lst, 66603738d50eSJeff Layton collect); 66613738d50eSJeff Layton } 6662016200c3SJeff Layton ++count; 6663016200c3SJeff Layton /* 6664016200c3SJeff Layton * Despite the fact that these functions deal 6665016200c3SJeff Layton * with 64-bit integers for "count", we must 6666016200c3SJeff Layton * ensure that it doesn't blow up the 6667016200c3SJeff Layton * clp->cl_refcount. Throw a warning if we 6668016200c3SJeff Layton * start to approach INT_MAX here. 6669016200c3SJeff Layton */ 6670016200c3SJeff Layton WARN_ON_ONCE(count == (INT_MAX / 2)); 6671016200c3SJeff Layton if (count == max) 6672016200c3SJeff Layton goto out; 6673fc29171fSBryan Schumaker } 6674fc29171fSBryan Schumaker } 6675fc29171fSBryan Schumaker } 6676016200c3SJeff Layton out: 6677016200c3SJeff Layton spin_unlock(&clp->cl_lock); 6678fc29171fSBryan Schumaker 6679fc29171fSBryan Schumaker return count; 6680fc29171fSBryan Schumaker } 6681fc29171fSBryan Schumaker 6682016200c3SJeff Layton static u64 6683016200c3SJeff Layton nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, 6684016200c3SJeff Layton u64 max) 6685fc29171fSBryan Schumaker { 6686016200c3SJeff Layton return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); 6687fc29171fSBryan Schumaker } 6688fc29171fSBryan Schumaker 6689016200c3SJeff Layton static u64 6690016200c3SJeff Layton nfsd_print_client_locks(struct nfs4_client *clp) 6691184c1847SBryan Schumaker { 6692016200c3SJeff Layton u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); 6693184c1847SBryan Schumaker nfsd_print_count(clp, count, "locked files"); 6694184c1847SBryan Schumaker return count; 6695184c1847SBryan Schumaker } 6696184c1847SBryan Schumaker 6697016200c3SJeff Layton u64 6698285abdeeSJeff Layton nfsd_inject_print_locks(void) 6699016200c3SJeff Layton { 6700016200c3SJeff Layton struct nfs4_client *clp; 6701016200c3SJeff Layton u64 count = 0; 6702016200c3SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 6703016200c3SJeff Layton nfsd_net_id); 6704016200c3SJeff Layton 6705016200c3SJeff Layton if (!nfsd_netns_ready(nn)) 6706016200c3SJeff Layton return 0; 6707016200c3SJeff Layton 6708016200c3SJeff Layton spin_lock(&nn->client_lock); 6709016200c3SJeff Layton list_for_each_entry(clp, &nn->client_lru, cl_lru) 6710016200c3SJeff Layton count += nfsd_print_client_locks(clp); 6711016200c3SJeff Layton spin_unlock(&nn->client_lock); 6712016200c3SJeff Layton 6713016200c3SJeff Layton return count; 6714016200c3SJeff Layton } 6715016200c3SJeff Layton 6716016200c3SJeff Layton static void 6717016200c3SJeff Layton nfsd_reap_locks(struct list_head *reaplist) 6718016200c3SJeff Layton { 6719016200c3SJeff Layton struct nfs4_client *clp; 6720016200c3SJeff Layton struct nfs4_ol_stateid *stp, *next; 6721016200c3SJeff Layton 6722016200c3SJeff Layton list_for_each_entry_safe(stp, next, reaplist, st_locks) { 6723016200c3SJeff Layton list_del_init(&stp->st_locks); 6724016200c3SJeff Layton clp = stp->st_stid.sc_client; 6725016200c3SJeff Layton nfs4_put_stid(&stp->st_stid); 6726016200c3SJeff Layton put_client(clp); 6727016200c3SJeff Layton } 6728016200c3SJeff Layton } 6729016200c3SJeff Layton 6730016200c3SJeff Layton u64 6731285abdeeSJeff Layton nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size) 6732016200c3SJeff Layton { 6733016200c3SJeff Layton unsigned int count = 0; 6734016200c3SJeff Layton struct nfs4_client *clp; 6735016200c3SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 6736016200c3SJeff Layton nfsd_net_id); 6737016200c3SJeff Layton LIST_HEAD(reaplist); 6738016200c3SJeff Layton 6739016200c3SJeff Layton if (!nfsd_netns_ready(nn)) 6740016200c3SJeff Layton return count; 6741016200c3SJeff Layton 6742016200c3SJeff Layton spin_lock(&nn->client_lock); 6743016200c3SJeff Layton clp = nfsd_find_client(addr, addr_size); 6744016200c3SJeff Layton if (clp) 6745016200c3SJeff Layton count = nfsd_collect_client_locks(clp, &reaplist, 0); 6746016200c3SJeff Layton spin_unlock(&nn->client_lock); 6747016200c3SJeff Layton nfsd_reap_locks(&reaplist); 6748016200c3SJeff Layton return count; 6749016200c3SJeff Layton } 6750016200c3SJeff Layton 6751016200c3SJeff Layton u64 6752285abdeeSJeff Layton nfsd_inject_forget_locks(u64 max) 6753016200c3SJeff Layton { 6754016200c3SJeff Layton u64 count = 0; 6755016200c3SJeff Layton struct nfs4_client *clp; 6756016200c3SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 6757016200c3SJeff Layton nfsd_net_id); 6758016200c3SJeff Layton LIST_HEAD(reaplist); 6759016200c3SJeff Layton 6760016200c3SJeff Layton if (!nfsd_netns_ready(nn)) 6761016200c3SJeff Layton return count; 6762016200c3SJeff Layton 6763016200c3SJeff Layton spin_lock(&nn->client_lock); 6764016200c3SJeff Layton list_for_each_entry(clp, &nn->client_lru, cl_lru) { 6765016200c3SJeff Layton count += nfsd_collect_client_locks(clp, &reaplist, max - count); 6766016200c3SJeff Layton if (max != 0 && count >= max) 6767016200c3SJeff Layton break; 6768016200c3SJeff Layton } 6769016200c3SJeff Layton spin_unlock(&nn->client_lock); 6770016200c3SJeff Layton nfsd_reap_locks(&reaplist); 6771016200c3SJeff Layton return count; 6772016200c3SJeff Layton } 6773016200c3SJeff Layton 677482e05efaSJeff Layton static u64 677582e05efaSJeff Layton nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, 677682e05efaSJeff Layton struct list_head *collect, 677782e05efaSJeff Layton void (*func)(struct nfs4_openowner *)) 67784dbdbda8SBryan Schumaker { 67794dbdbda8SBryan Schumaker struct nfs4_openowner *oop, *next; 678082e05efaSJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 678182e05efaSJeff Layton nfsd_net_id); 67824dbdbda8SBryan Schumaker u64 count = 0; 67834dbdbda8SBryan Schumaker 678482e05efaSJeff Layton lockdep_assert_held(&nn->client_lock); 678582e05efaSJeff Layton 678682e05efaSJeff Layton spin_lock(&clp->cl_lock); 67874dbdbda8SBryan Schumaker list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { 678882e05efaSJeff Layton if (func) { 67894dbdbda8SBryan Schumaker func(oop); 679082e05efaSJeff Layton if (collect) { 679182e05efaSJeff Layton atomic_inc(&clp->cl_refcount); 679282e05efaSJeff Layton list_add(&oop->oo_perclient, collect); 679382e05efaSJeff Layton } 679482e05efaSJeff Layton } 679582e05efaSJeff Layton ++count; 679682e05efaSJeff Layton /* 679782e05efaSJeff Layton * Despite the fact that these functions deal with 679882e05efaSJeff Layton * 64-bit integers for "count", we must ensure that 679982e05efaSJeff Layton * it doesn't blow up the clp->cl_refcount. Throw a 680082e05efaSJeff Layton * warning if we start to approach INT_MAX here. 680182e05efaSJeff Layton */ 680282e05efaSJeff Layton WARN_ON_ONCE(count == (INT_MAX / 2)); 680382e05efaSJeff Layton if (count == max) 68044dbdbda8SBryan Schumaker break; 68054dbdbda8SBryan Schumaker } 680682e05efaSJeff Layton spin_unlock(&clp->cl_lock); 68074dbdbda8SBryan Schumaker 68084dbdbda8SBryan Schumaker return count; 68094dbdbda8SBryan Schumaker } 68104dbdbda8SBryan Schumaker 681182e05efaSJeff Layton static u64 681282e05efaSJeff Layton nfsd_print_client_openowners(struct nfs4_client *clp) 68134dbdbda8SBryan Schumaker { 681482e05efaSJeff Layton u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); 681582e05efaSJeff Layton 681682e05efaSJeff Layton nfsd_print_count(clp, count, "openowners"); 681782e05efaSJeff Layton return count; 68184dbdbda8SBryan Schumaker } 68194dbdbda8SBryan Schumaker 682082e05efaSJeff Layton static u64 682182e05efaSJeff Layton nfsd_collect_client_openowners(struct nfs4_client *clp, 682282e05efaSJeff Layton struct list_head *collect, u64 max) 6823184c1847SBryan Schumaker { 682482e05efaSJeff Layton return nfsd_foreach_client_openowner(clp, max, collect, 682582e05efaSJeff Layton unhash_openowner_locked); 682682e05efaSJeff Layton } 682782e05efaSJeff Layton 682882e05efaSJeff Layton u64 6829285abdeeSJeff Layton nfsd_inject_print_openowners(void) 683082e05efaSJeff Layton { 683182e05efaSJeff Layton struct nfs4_client *clp; 683282e05efaSJeff Layton u64 count = 0; 683382e05efaSJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 683482e05efaSJeff Layton nfsd_net_id); 683582e05efaSJeff Layton 683682e05efaSJeff Layton if (!nfsd_netns_ready(nn)) 683782e05efaSJeff Layton return 0; 683882e05efaSJeff Layton 683982e05efaSJeff Layton spin_lock(&nn->client_lock); 684082e05efaSJeff Layton list_for_each_entry(clp, &nn->client_lru, cl_lru) 684182e05efaSJeff Layton count += nfsd_print_client_openowners(clp); 684282e05efaSJeff Layton spin_unlock(&nn->client_lock); 684382e05efaSJeff Layton 684482e05efaSJeff Layton return count; 684582e05efaSJeff Layton } 684682e05efaSJeff Layton 684782e05efaSJeff Layton static void 684882e05efaSJeff Layton nfsd_reap_openowners(struct list_head *reaplist) 684982e05efaSJeff Layton { 685082e05efaSJeff Layton struct nfs4_client *clp; 685182e05efaSJeff Layton struct nfs4_openowner *oop, *next; 685282e05efaSJeff Layton 685382e05efaSJeff Layton list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { 685482e05efaSJeff Layton list_del_init(&oop->oo_perclient); 685582e05efaSJeff Layton clp = oop->oo_owner.so_client; 685682e05efaSJeff Layton release_openowner(oop); 685782e05efaSJeff Layton put_client(clp); 685882e05efaSJeff Layton } 685982e05efaSJeff Layton } 686082e05efaSJeff Layton 686182e05efaSJeff Layton u64 6862285abdeeSJeff Layton nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, 6863285abdeeSJeff Layton size_t addr_size) 686482e05efaSJeff Layton { 686582e05efaSJeff Layton unsigned int count = 0; 686682e05efaSJeff Layton struct nfs4_client *clp; 686782e05efaSJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 686882e05efaSJeff Layton nfsd_net_id); 686982e05efaSJeff Layton LIST_HEAD(reaplist); 687082e05efaSJeff Layton 687182e05efaSJeff Layton if (!nfsd_netns_ready(nn)) 687282e05efaSJeff Layton return count; 687382e05efaSJeff Layton 687482e05efaSJeff Layton spin_lock(&nn->client_lock); 687582e05efaSJeff Layton clp = nfsd_find_client(addr, addr_size); 687682e05efaSJeff Layton if (clp) 687782e05efaSJeff Layton count = nfsd_collect_client_openowners(clp, &reaplist, 0); 687882e05efaSJeff Layton spin_unlock(&nn->client_lock); 687982e05efaSJeff Layton nfsd_reap_openowners(&reaplist); 688082e05efaSJeff Layton return count; 688182e05efaSJeff Layton } 688282e05efaSJeff Layton 688382e05efaSJeff Layton u64 6884285abdeeSJeff Layton nfsd_inject_forget_openowners(u64 max) 688582e05efaSJeff Layton { 688682e05efaSJeff Layton u64 count = 0; 688782e05efaSJeff Layton struct nfs4_client *clp; 688882e05efaSJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 688982e05efaSJeff Layton nfsd_net_id); 689082e05efaSJeff Layton LIST_HEAD(reaplist); 689182e05efaSJeff Layton 689282e05efaSJeff Layton if (!nfsd_netns_ready(nn)) 689382e05efaSJeff Layton return count; 689482e05efaSJeff Layton 689582e05efaSJeff Layton spin_lock(&nn->client_lock); 689682e05efaSJeff Layton list_for_each_entry(clp, &nn->client_lru, cl_lru) { 689782e05efaSJeff Layton count += nfsd_collect_client_openowners(clp, &reaplist, 689882e05efaSJeff Layton max - count); 689982e05efaSJeff Layton if (max != 0 && count >= max) 690082e05efaSJeff Layton break; 690182e05efaSJeff Layton } 690282e05efaSJeff Layton spin_unlock(&nn->client_lock); 690382e05efaSJeff Layton nfsd_reap_openowners(&reaplist); 6904184c1847SBryan Schumaker return count; 6905184c1847SBryan Schumaker } 6906184c1847SBryan Schumaker 6907269de30fSBryan Schumaker static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, 6908269de30fSBryan Schumaker struct list_head *victims) 6909269de30fSBryan Schumaker { 6910269de30fSBryan Schumaker struct nfs4_delegation *dp, *next; 691198d5c7c5SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 691298d5c7c5SJeff Layton nfsd_net_id); 6913269de30fSBryan Schumaker u64 count = 0; 6914269de30fSBryan Schumaker 691598d5c7c5SJeff Layton lockdep_assert_held(&nn->client_lock); 691698d5c7c5SJeff Layton 691798d5c7c5SJeff Layton spin_lock(&state_lock); 6918269de30fSBryan Schumaker list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { 6919dff1399fSJeff Layton if (victims) { 6920dff1399fSJeff Layton /* 6921dff1399fSJeff Layton * It's not safe to mess with delegations that have a 6922dff1399fSJeff Layton * non-zero dl_time. They might have already been broken 6923dff1399fSJeff Layton * and could be processed by the laundromat outside of 6924dff1399fSJeff Layton * the state_lock. Just leave them be. 6925dff1399fSJeff Layton */ 6926dff1399fSJeff Layton if (dp->dl_time != 0) 6927dff1399fSJeff Layton continue; 6928dff1399fSJeff Layton 692998d5c7c5SJeff Layton atomic_inc(&clp->cl_refcount); 69303fcbbd24SJeff Layton WARN_ON(!unhash_delegation_locked(dp)); 693142690676SJeff Layton list_add(&dp->dl_recall_lru, victims); 6932dff1399fSJeff Layton } 693398d5c7c5SJeff Layton ++count; 693498d5c7c5SJeff Layton /* 693598d5c7c5SJeff Layton * Despite the fact that these functions deal with 693698d5c7c5SJeff Layton * 64-bit integers for "count", we must ensure that 693798d5c7c5SJeff Layton * it doesn't blow up the clp->cl_refcount. Throw a 693898d5c7c5SJeff Layton * warning if we start to approach INT_MAX here. 693998d5c7c5SJeff Layton */ 694098d5c7c5SJeff Layton WARN_ON_ONCE(count == (INT_MAX / 2)); 694198d5c7c5SJeff Layton if (count == max) 6942269de30fSBryan Schumaker break; 6943269de30fSBryan Schumaker } 694498d5c7c5SJeff Layton spin_unlock(&state_lock); 6945269de30fSBryan Schumaker return count; 6946269de30fSBryan Schumaker } 6947269de30fSBryan Schumaker 694898d5c7c5SJeff Layton static u64 694998d5c7c5SJeff Layton nfsd_print_client_delegations(struct nfs4_client *clp) 6950269de30fSBryan Schumaker { 695198d5c7c5SJeff Layton u64 count = nfsd_find_all_delegations(clp, 0, NULL); 6952184c1847SBryan Schumaker 6953184c1847SBryan Schumaker nfsd_print_count(clp, count, "delegations"); 6954184c1847SBryan Schumaker return count; 6955184c1847SBryan Schumaker } 6956184c1847SBryan Schumaker 695798d5c7c5SJeff Layton u64 6958285abdeeSJeff Layton nfsd_inject_print_delegations(void) 695998d5c7c5SJeff Layton { 696098d5c7c5SJeff Layton struct nfs4_client *clp; 696198d5c7c5SJeff Layton u64 count = 0; 696298d5c7c5SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 696398d5c7c5SJeff Layton nfsd_net_id); 696498d5c7c5SJeff Layton 696598d5c7c5SJeff Layton if (!nfsd_netns_ready(nn)) 696698d5c7c5SJeff Layton return 0; 696798d5c7c5SJeff Layton 696898d5c7c5SJeff Layton spin_lock(&nn->client_lock); 696998d5c7c5SJeff Layton list_for_each_entry(clp, &nn->client_lru, cl_lru) 697098d5c7c5SJeff Layton count += nfsd_print_client_delegations(clp); 697198d5c7c5SJeff Layton spin_unlock(&nn->client_lock); 697298d5c7c5SJeff Layton 697398d5c7c5SJeff Layton return count; 697498d5c7c5SJeff Layton } 697598d5c7c5SJeff Layton 697698d5c7c5SJeff Layton static void 697798d5c7c5SJeff Layton nfsd_forget_delegations(struct list_head *reaplist) 697898d5c7c5SJeff Layton { 697998d5c7c5SJeff Layton struct nfs4_client *clp; 698098d5c7c5SJeff Layton struct nfs4_delegation *dp, *next; 698198d5c7c5SJeff Layton 698298d5c7c5SJeff Layton list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { 698398d5c7c5SJeff Layton list_del_init(&dp->dl_recall_lru); 698498d5c7c5SJeff Layton clp = dp->dl_stid.sc_client; 698598d5c7c5SJeff Layton revoke_delegation(dp); 698698d5c7c5SJeff Layton put_client(clp); 698798d5c7c5SJeff Layton } 698898d5c7c5SJeff Layton } 698998d5c7c5SJeff Layton 699098d5c7c5SJeff Layton u64 6991285abdeeSJeff Layton nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, 6992285abdeeSJeff Layton size_t addr_size) 699398d5c7c5SJeff Layton { 699498d5c7c5SJeff Layton u64 count = 0; 699598d5c7c5SJeff Layton struct nfs4_client *clp; 699698d5c7c5SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 699798d5c7c5SJeff Layton nfsd_net_id); 699898d5c7c5SJeff Layton LIST_HEAD(reaplist); 699998d5c7c5SJeff Layton 700098d5c7c5SJeff Layton if (!nfsd_netns_ready(nn)) 700198d5c7c5SJeff Layton return count; 700298d5c7c5SJeff Layton 700398d5c7c5SJeff Layton spin_lock(&nn->client_lock); 700498d5c7c5SJeff Layton clp = nfsd_find_client(addr, addr_size); 700598d5c7c5SJeff Layton if (clp) 700698d5c7c5SJeff Layton count = nfsd_find_all_delegations(clp, 0, &reaplist); 700798d5c7c5SJeff Layton spin_unlock(&nn->client_lock); 700898d5c7c5SJeff Layton 700998d5c7c5SJeff Layton nfsd_forget_delegations(&reaplist); 701098d5c7c5SJeff Layton return count; 701198d5c7c5SJeff Layton } 701298d5c7c5SJeff Layton 701398d5c7c5SJeff Layton u64 7014285abdeeSJeff Layton nfsd_inject_forget_delegations(u64 max) 701598d5c7c5SJeff Layton { 701698d5c7c5SJeff Layton u64 count = 0; 701798d5c7c5SJeff Layton struct nfs4_client *clp; 701898d5c7c5SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 701998d5c7c5SJeff Layton nfsd_net_id); 702098d5c7c5SJeff Layton LIST_HEAD(reaplist); 702198d5c7c5SJeff Layton 702298d5c7c5SJeff Layton if (!nfsd_netns_ready(nn)) 702398d5c7c5SJeff Layton return count; 702498d5c7c5SJeff Layton 702598d5c7c5SJeff Layton spin_lock(&nn->client_lock); 702698d5c7c5SJeff Layton list_for_each_entry(clp, &nn->client_lru, cl_lru) { 702798d5c7c5SJeff Layton count += nfsd_find_all_delegations(clp, max - count, &reaplist); 702898d5c7c5SJeff Layton if (max != 0 && count >= max) 702998d5c7c5SJeff Layton break; 703098d5c7c5SJeff Layton } 703198d5c7c5SJeff Layton spin_unlock(&nn->client_lock); 703298d5c7c5SJeff Layton nfsd_forget_delegations(&reaplist); 703398d5c7c5SJeff Layton return count; 703498d5c7c5SJeff Layton } 703598d5c7c5SJeff Layton 703698d5c7c5SJeff Layton static void 703798d5c7c5SJeff Layton nfsd_recall_delegations(struct list_head *reaplist) 703898d5c7c5SJeff Layton { 703998d5c7c5SJeff Layton struct nfs4_client *clp; 704098d5c7c5SJeff Layton struct nfs4_delegation *dp, *next; 704198d5c7c5SJeff Layton 704298d5c7c5SJeff Layton list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { 704398d5c7c5SJeff Layton list_del_init(&dp->dl_recall_lru); 704498d5c7c5SJeff Layton clp = dp->dl_stid.sc_client; 704598d5c7c5SJeff Layton /* 704698d5c7c5SJeff Layton * We skipped all entries that had a zero dl_time before, 704798d5c7c5SJeff Layton * so we can now reset the dl_time back to 0. If a delegation 704898d5c7c5SJeff Layton * break comes in now, then it won't make any difference since 704998d5c7c5SJeff Layton * we're recalling it either way. 705098d5c7c5SJeff Layton */ 705198d5c7c5SJeff Layton spin_lock(&state_lock); 705298d5c7c5SJeff Layton dp->dl_time = 0; 705398d5c7c5SJeff Layton spin_unlock(&state_lock); 705498d5c7c5SJeff Layton nfsd_break_one_deleg(dp); 705598d5c7c5SJeff Layton put_client(clp); 705698d5c7c5SJeff Layton } 705798d5c7c5SJeff Layton } 705898d5c7c5SJeff Layton 705998d5c7c5SJeff Layton u64 7060285abdeeSJeff Layton nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, 706198d5c7c5SJeff Layton size_t addr_size) 706298d5c7c5SJeff Layton { 706398d5c7c5SJeff Layton u64 count = 0; 706498d5c7c5SJeff Layton struct nfs4_client *clp; 706598d5c7c5SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 706698d5c7c5SJeff Layton nfsd_net_id); 706798d5c7c5SJeff Layton LIST_HEAD(reaplist); 706898d5c7c5SJeff Layton 706998d5c7c5SJeff Layton if (!nfsd_netns_ready(nn)) 707098d5c7c5SJeff Layton return count; 707198d5c7c5SJeff Layton 707298d5c7c5SJeff Layton spin_lock(&nn->client_lock); 707398d5c7c5SJeff Layton clp = nfsd_find_client(addr, addr_size); 707498d5c7c5SJeff Layton if (clp) 707598d5c7c5SJeff Layton count = nfsd_find_all_delegations(clp, 0, &reaplist); 707698d5c7c5SJeff Layton spin_unlock(&nn->client_lock); 707798d5c7c5SJeff Layton 707898d5c7c5SJeff Layton nfsd_recall_delegations(&reaplist); 707998d5c7c5SJeff Layton return count; 708098d5c7c5SJeff Layton } 708198d5c7c5SJeff Layton 708298d5c7c5SJeff Layton u64 7083285abdeeSJeff Layton nfsd_inject_recall_delegations(u64 max) 708498d5c7c5SJeff Layton { 708598d5c7c5SJeff Layton u64 count = 0; 708698d5c7c5SJeff Layton struct nfs4_client *clp, *next; 708798d5c7c5SJeff Layton struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 708898d5c7c5SJeff Layton nfsd_net_id); 708998d5c7c5SJeff Layton LIST_HEAD(reaplist); 709098d5c7c5SJeff Layton 709198d5c7c5SJeff Layton if (!nfsd_netns_ready(nn)) 709298d5c7c5SJeff Layton return count; 709398d5c7c5SJeff Layton 709498d5c7c5SJeff Layton spin_lock(&nn->client_lock); 709598d5c7c5SJeff Layton list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { 709698d5c7c5SJeff Layton count += nfsd_find_all_delegations(clp, max - count, &reaplist); 709798d5c7c5SJeff Layton if (max != 0 && ++count >= max) 709898d5c7c5SJeff Layton break; 709998d5c7c5SJeff Layton } 710098d5c7c5SJeff Layton spin_unlock(&nn->client_lock); 710198d5c7c5SJeff Layton nfsd_recall_delegations(&reaplist); 710298d5c7c5SJeff Layton return count; 710398d5c7c5SJeff Layton } 710465178db4SBryan Schumaker #endif /* CONFIG_NFSD_FAULT_INJECTION */ 710565178db4SBryan Schumaker 7106c2f1a551SMeelap Shah /* 7107c2f1a551SMeelap Shah * Since the lifetime of a delegation isn't limited to that of an open, a 7108c2f1a551SMeelap Shah * client may quite reasonably hang on to a delegation as long as it has 7109c2f1a551SMeelap Shah * the inode cached. This becomes an obvious problem the first time a 7110c2f1a551SMeelap Shah * client's inode cache approaches the size of the server's total memory. 7111c2f1a551SMeelap Shah * 7112c2f1a551SMeelap Shah * For now we avoid this problem by imposing a hard limit on the number 7113c2f1a551SMeelap Shah * of delegations, which varies according to the server's memory size. 7114c2f1a551SMeelap Shah */ 7115c2f1a551SMeelap Shah static void 7116c2f1a551SMeelap Shah set_max_delegations(void) 7117c2f1a551SMeelap Shah { 7118c2f1a551SMeelap Shah /* 7119c2f1a551SMeelap Shah * Allow at most 4 delegations per megabyte of RAM. Quick 7120c2f1a551SMeelap Shah * estimates suggest that in the worst case (where every delegation 7121c2f1a551SMeelap Shah * is for a different inode), a delegation could take about 1.5K, 7122c2f1a551SMeelap Shah * giving a worst case usage of about 6% of memory. 7123c2f1a551SMeelap Shah */ 7124c2f1a551SMeelap Shah max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); 7125c2f1a551SMeelap Shah } 7126c2f1a551SMeelap Shah 7127d85ed443SStanislav Kinsbursky static int nfs4_state_create_net(struct net *net) 71288daae4dcSStanislav Kinsbursky { 71298daae4dcSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 71308daae4dcSStanislav Kinsbursky int i; 71318daae4dcSStanislav Kinsbursky 71326da2ec56SKees Cook nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 71336da2ec56SKees Cook sizeof(struct list_head), 71346da2ec56SKees Cook GFP_KERNEL); 71358daae4dcSStanislav Kinsbursky if (!nn->conf_id_hashtbl) 7136382a62e7SStanislav Kinsbursky goto err; 71376da2ec56SKees Cook nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 71386da2ec56SKees Cook sizeof(struct list_head), 71396da2ec56SKees Cook GFP_KERNEL); 71400a7ec377SStanislav Kinsbursky if (!nn->unconf_id_hashtbl) 71410a7ec377SStanislav Kinsbursky goto err_unconf_id; 71426da2ec56SKees Cook nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, 71436da2ec56SKees Cook sizeof(struct list_head), 71446da2ec56SKees Cook GFP_KERNEL); 71451872de0eSStanislav Kinsbursky if (!nn->sessionid_hashtbl) 71461872de0eSStanislav Kinsbursky goto err_sessionid; 71478daae4dcSStanislav Kinsbursky 7148382a62e7SStanislav Kinsbursky for (i = 0; i < CLIENT_HASH_SIZE; i++) { 71498daae4dcSStanislav Kinsbursky INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); 71500a7ec377SStanislav Kinsbursky INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); 7151382a62e7SStanislav Kinsbursky } 71521872de0eSStanislav Kinsbursky for (i = 0; i < SESSION_HASH_SIZE; i++) 71531872de0eSStanislav Kinsbursky INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); 7154382a62e7SStanislav Kinsbursky nn->conf_name_tree = RB_ROOT; 7155a99454aaSStanislav Kinsbursky nn->unconf_name_tree = RB_ROOT; 715681833de1SVasily Averin nn->boot_time = get_seconds(); 715781833de1SVasily Averin nn->grace_ended = false; 715881833de1SVasily Averin nn->nfsd4_manager.block_opens = true; 715981833de1SVasily Averin INIT_LIST_HEAD(&nn->nfsd4_manager.list); 71605ed58bb2SStanislav Kinsbursky INIT_LIST_HEAD(&nn->client_lru); 716173758fedSStanislav Kinsbursky INIT_LIST_HEAD(&nn->close_lru); 7162e8c69d17SJ. Bruce Fields INIT_LIST_HEAD(&nn->del_recall_lru); 7163c9a49628SStanislav Kinsbursky spin_lock_init(&nn->client_lock); 71648daae4dcSStanislav Kinsbursky 71650cc11a61SJeff Layton spin_lock_init(&nn->blocked_locks_lock); 71660cc11a61SJeff Layton INIT_LIST_HEAD(&nn->blocked_locks_lru); 71670cc11a61SJeff Layton 716809121281SStanislav Kinsbursky INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); 7169d85ed443SStanislav Kinsbursky get_net(net); 717009121281SStanislav Kinsbursky 71718daae4dcSStanislav Kinsbursky return 0; 7172382a62e7SStanislav Kinsbursky 71731872de0eSStanislav Kinsbursky err_sessionid: 71749b531137SStanislav Kinsbursky kfree(nn->unconf_id_hashtbl); 71750a7ec377SStanislav Kinsbursky err_unconf_id: 71760a7ec377SStanislav Kinsbursky kfree(nn->conf_id_hashtbl); 7177382a62e7SStanislav Kinsbursky err: 7178382a62e7SStanislav Kinsbursky return -ENOMEM; 71798daae4dcSStanislav Kinsbursky } 71808daae4dcSStanislav Kinsbursky 71818daae4dcSStanislav Kinsbursky static void 71824dce0ac9SStanislav Kinsbursky nfs4_state_destroy_net(struct net *net) 71838daae4dcSStanislav Kinsbursky { 71848daae4dcSStanislav Kinsbursky int i; 71858daae4dcSStanislav Kinsbursky struct nfs4_client *clp = NULL; 71868daae4dcSStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 71878daae4dcSStanislav Kinsbursky 71888daae4dcSStanislav Kinsbursky for (i = 0; i < CLIENT_HASH_SIZE; i++) { 71898daae4dcSStanislav Kinsbursky while (!list_empty(&nn->conf_id_hashtbl[i])) { 71908daae4dcSStanislav Kinsbursky clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); 71918daae4dcSStanislav Kinsbursky destroy_client(clp); 71928daae4dcSStanislav Kinsbursky } 71938daae4dcSStanislav Kinsbursky } 7194a99454aaSStanislav Kinsbursky 719568ef3bc3SJeff Layton WARN_ON(!list_empty(&nn->blocked_locks_lru)); 719668ef3bc3SJeff Layton 71972b905635SKinglong Mee for (i = 0; i < CLIENT_HASH_SIZE; i++) { 71982b905635SKinglong Mee while (!list_empty(&nn->unconf_id_hashtbl[i])) { 71992b905635SKinglong Mee clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); 7200a99454aaSStanislav Kinsbursky destroy_client(clp); 7201a99454aaSStanislav Kinsbursky } 72022b905635SKinglong Mee } 7203a99454aaSStanislav Kinsbursky 72041872de0eSStanislav Kinsbursky kfree(nn->sessionid_hashtbl); 72050a7ec377SStanislav Kinsbursky kfree(nn->unconf_id_hashtbl); 72068daae4dcSStanislav Kinsbursky kfree(nn->conf_id_hashtbl); 72074dce0ac9SStanislav Kinsbursky put_net(net); 72088daae4dcSStanislav Kinsbursky } 72098daae4dcSStanislav Kinsbursky 7210f252bc68SStanislav Kinsbursky int 7211d85ed443SStanislav Kinsbursky nfs4_state_start_net(struct net *net) 7212ac4d8ff2SNeilBrown { 72135e1533c7SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 7214b5a1a81eSJ. Bruce Fields int ret; 7215b5a1a81eSJ. Bruce Fields 7216d85ed443SStanislav Kinsbursky ret = nfs4_state_create_net(net); 72178daae4dcSStanislav Kinsbursky if (ret) 72188daae4dcSStanislav Kinsbursky return ret; 7219d4318acdSJeff Layton locks_start_grace(net, &nn->nfsd4_manager); 7220d4318acdSJeff Layton nfsd4_client_tracking_init(net); 72217e981a8aSVasily Averin printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n", 72227e981a8aSVasily Averin nn->nfsd4_grace, net->ns.inum); 72235284b44eSStanislav Kinsbursky queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); 7224d85ed443SStanislav Kinsbursky return 0; 7225a6d6b781SJeff Layton } 7226d85ed443SStanislav Kinsbursky 7227d85ed443SStanislav Kinsbursky /* initialization to perform when the nfsd service is started: */ 7228d85ed443SStanislav Kinsbursky 7229d85ed443SStanislav Kinsbursky int 7230d85ed443SStanislav Kinsbursky nfs4_state_start(void) 7231d85ed443SStanislav Kinsbursky { 7232d85ed443SStanislav Kinsbursky int ret; 7233d85ed443SStanislav Kinsbursky 723451a54568SJeff Layton laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); 7235a6d6b781SJeff Layton if (laundry_wq == NULL) { 7236a6d6b781SJeff Layton ret = -ENOMEM; 7237a26dd64fSChuck Lever goto out; 7238a6d6b781SJeff Layton } 7239b5a1a81eSJ. Bruce Fields ret = nfsd4_create_callback_queue(); 7240b5a1a81eSJ. Bruce Fields if (ret) 7241b5a1a81eSJ. Bruce Fields goto out_free_laundry; 724209121281SStanislav Kinsbursky 7243c2f1a551SMeelap Shah set_max_delegations(); 7244b5a1a81eSJ. Bruce Fields return 0; 7245d85ed443SStanislav Kinsbursky 7246b5a1a81eSJ. Bruce Fields out_free_laundry: 7247b5a1a81eSJ. Bruce Fields destroy_workqueue(laundry_wq); 7248a26dd64fSChuck Lever out: 7249b5a1a81eSJ. Bruce Fields return ret; 72501da177e4SLinus Torvalds } 72511da177e4SLinus Torvalds 7252f252bc68SStanislav Kinsbursky void 72534dce0ac9SStanislav Kinsbursky nfs4_state_shutdown_net(struct net *net) 72541da177e4SLinus Torvalds { 72551da177e4SLinus Torvalds struct nfs4_delegation *dp = NULL; 72561da177e4SLinus Torvalds struct list_head *pos, *next, reaplist; 72574dce0ac9SStanislav Kinsbursky struct nfsd_net *nn = net_generic(net, nfsd_net_id); 72581da177e4SLinus Torvalds 72594dce0ac9SStanislav Kinsbursky cancel_delayed_work_sync(&nn->laundromat_work); 72604dce0ac9SStanislav Kinsbursky locks_end_grace(&nn->nfsd4_manager); 7261ac55fdc4SJeff Layton 72621da177e4SLinus Torvalds INIT_LIST_HEAD(&reaplist); 7263cdc97505SBenny Halevy spin_lock(&state_lock); 7264e8c69d17SJ. Bruce Fields list_for_each_safe(pos, next, &nn->del_recall_lru) { 72651da177e4SLinus Torvalds dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 72663fcbbd24SJeff Layton WARN_ON(!unhash_delegation_locked(dp)); 726742690676SJeff Layton list_add(&dp->dl_recall_lru, &reaplist); 72681da177e4SLinus Torvalds } 7269cdc97505SBenny Halevy spin_unlock(&state_lock); 72701da177e4SLinus Torvalds list_for_each_safe(pos, next, &reaplist) { 72711da177e4SLinus Torvalds dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); 727242690676SJeff Layton list_del_init(&dp->dl_recall_lru); 72730af6e690SJ. Bruce Fields destroy_unhashed_deleg(dp); 72741da177e4SLinus Torvalds } 72751da177e4SLinus Torvalds 72763320fef1SStanislav Kinsbursky nfsd4_client_tracking_exit(net); 72774dce0ac9SStanislav Kinsbursky nfs4_state_destroy_net(net); 72781da177e4SLinus Torvalds } 72791da177e4SLinus Torvalds 72801da177e4SLinus Torvalds void 72811da177e4SLinus Torvalds nfs4_state_shutdown(void) 72821da177e4SLinus Torvalds { 72835e8d5c29SNeilBrown destroy_workqueue(laundry_wq); 7284c3935e30SJ. Bruce Fields nfsd4_destroy_callback_queue(); 72851da177e4SLinus Torvalds } 72868b70484cSTigran Mkrtchyan 72878b70484cSTigran Mkrtchyan static void 72888b70484cSTigran Mkrtchyan get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) 72898b70484cSTigran Mkrtchyan { 729037c593c5STigran Mkrtchyan if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) 729137c593c5STigran Mkrtchyan memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); 72928b70484cSTigran Mkrtchyan } 72938b70484cSTigran Mkrtchyan 72948b70484cSTigran Mkrtchyan static void 72958b70484cSTigran Mkrtchyan put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) 72968b70484cSTigran Mkrtchyan { 729737c593c5STigran Mkrtchyan if (cstate->minorversion) { 729837c593c5STigran Mkrtchyan memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); 729937c593c5STigran Mkrtchyan SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); 730037c593c5STigran Mkrtchyan } 730137c593c5STigran Mkrtchyan } 730237c593c5STigran Mkrtchyan 730337c593c5STigran Mkrtchyan void 730437c593c5STigran Mkrtchyan clear_current_stateid(struct nfsd4_compound_state *cstate) 730537c593c5STigran Mkrtchyan { 730637c593c5STigran Mkrtchyan CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); 73078b70484cSTigran Mkrtchyan } 73088b70484cSTigran Mkrtchyan 730962cd4a59STigran Mkrtchyan /* 731062cd4a59STigran Mkrtchyan * functions to set current state id 731162cd4a59STigran Mkrtchyan */ 73128b70484cSTigran Mkrtchyan void 7313b60e9859SChristoph Hellwig nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, 7314b60e9859SChristoph Hellwig union nfsd4_op_u *u) 73159428fe1aSTigran Mkrtchyan { 7316b60e9859SChristoph Hellwig put_stateid(cstate, &u->open_downgrade.od_stateid); 73179428fe1aSTigran Mkrtchyan } 73189428fe1aSTigran Mkrtchyan 73199428fe1aSTigran Mkrtchyan void 7320b60e9859SChristoph Hellwig nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, 7321b60e9859SChristoph Hellwig union nfsd4_op_u *u) 73228b70484cSTigran Mkrtchyan { 7323b60e9859SChristoph Hellwig put_stateid(cstate, &u->open.op_stateid); 73248b70484cSTigran Mkrtchyan } 73258b70484cSTigran Mkrtchyan 73268b70484cSTigran Mkrtchyan void 7327b60e9859SChristoph Hellwig nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, 7328b60e9859SChristoph Hellwig union nfsd4_op_u *u) 732962cd4a59STigran Mkrtchyan { 7330b60e9859SChristoph Hellwig put_stateid(cstate, &u->close.cl_stateid); 733162cd4a59STigran Mkrtchyan } 733262cd4a59STigran Mkrtchyan 733362cd4a59STigran Mkrtchyan void 7334b60e9859SChristoph Hellwig nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, 7335b60e9859SChristoph Hellwig union nfsd4_op_u *u) 733662cd4a59STigran Mkrtchyan { 7337b60e9859SChristoph Hellwig put_stateid(cstate, &u->lock.lk_resp_stateid); 733862cd4a59STigran Mkrtchyan } 733962cd4a59STigran Mkrtchyan 734062cd4a59STigran Mkrtchyan /* 734162cd4a59STigran Mkrtchyan * functions to consume current state id 734262cd4a59STigran Mkrtchyan */ 73431e97b519STigran Mkrtchyan 73441e97b519STigran Mkrtchyan void 734557832e7bSChristoph Hellwig nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, 734657832e7bSChristoph Hellwig union nfsd4_op_u *u) 73479428fe1aSTigran Mkrtchyan { 734857832e7bSChristoph Hellwig get_stateid(cstate, &u->open_downgrade.od_stateid); 73499428fe1aSTigran Mkrtchyan } 73509428fe1aSTigran Mkrtchyan 73519428fe1aSTigran Mkrtchyan void 735257832e7bSChristoph Hellwig nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, 735357832e7bSChristoph Hellwig union nfsd4_op_u *u) 73549428fe1aSTigran Mkrtchyan { 735557832e7bSChristoph Hellwig get_stateid(cstate, &u->delegreturn.dr_stateid); 73569428fe1aSTigran Mkrtchyan } 73579428fe1aSTigran Mkrtchyan 73589428fe1aSTigran Mkrtchyan void 735957832e7bSChristoph Hellwig nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, 736057832e7bSChristoph Hellwig union nfsd4_op_u *u) 73611e97b519STigran Mkrtchyan { 736257832e7bSChristoph Hellwig get_stateid(cstate, &u->free_stateid.fr_stateid); 73631e97b519STigran Mkrtchyan } 73641e97b519STigran Mkrtchyan 73651e97b519STigran Mkrtchyan void 736657832e7bSChristoph Hellwig nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, 736757832e7bSChristoph Hellwig union nfsd4_op_u *u) 73681e97b519STigran Mkrtchyan { 736957832e7bSChristoph Hellwig get_stateid(cstate, &u->setattr.sa_stateid); 73701e97b519STigran Mkrtchyan } 73711e97b519STigran Mkrtchyan 737262cd4a59STigran Mkrtchyan void 737357832e7bSChristoph Hellwig nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, 737457832e7bSChristoph Hellwig union nfsd4_op_u *u) 73758b70484cSTigran Mkrtchyan { 737657832e7bSChristoph Hellwig get_stateid(cstate, &u->close.cl_stateid); 73778b70484cSTigran Mkrtchyan } 73788b70484cSTigran Mkrtchyan 73798b70484cSTigran Mkrtchyan void 738057832e7bSChristoph Hellwig nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, 738157832e7bSChristoph Hellwig union nfsd4_op_u *u) 73828b70484cSTigran Mkrtchyan { 738357832e7bSChristoph Hellwig get_stateid(cstate, &u->locku.lu_stateid); 73848b70484cSTigran Mkrtchyan } 738530813e27STigran Mkrtchyan 738630813e27STigran Mkrtchyan void 738757832e7bSChristoph Hellwig nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, 738857832e7bSChristoph Hellwig union nfsd4_op_u *u) 738930813e27STigran Mkrtchyan { 739057832e7bSChristoph Hellwig get_stateid(cstate, &u->read.rd_stateid); 739130813e27STigran Mkrtchyan } 739230813e27STigran Mkrtchyan 739330813e27STigran Mkrtchyan void 739457832e7bSChristoph Hellwig nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, 739557832e7bSChristoph Hellwig union nfsd4_op_u *u) 739630813e27STigran Mkrtchyan { 739757832e7bSChristoph Hellwig get_stateid(cstate, &u->write.wr_stateid); 739830813e27STigran Mkrtchyan } 7399