Skip to content

Commit a7952f4

Browse files
chuckleverTrond Myklebust
authored andcommitted
lockd: Add nlm_alloc_host()
Refactor nlm_host allocation and initialization into a separate function. This will be the common piece of server and client nlm_host lookup logic after the nlm_host cache is split. Small change: use kmalloc() instead of kzalloc(), as we're overwriting almost all fields in the new nlm_host struct with non-zero values immediately after it is allocated. An added benefit is we now have an explicit reference to each field name where it is initialized (for all you cscope fans out there). Signed-off-by: Chuck Lever <chuck.lever@oracle.com> Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
1 parent b10e30f commit a7952f4

1 file changed

Lines changed: 65 additions & 45 deletions

File tree

‎fs/lockd/host.c‎

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,68 @@ static unsigned int nlm_hash_address(const struct sockaddr *sap)
9999
return hash & (NLM_HOST_NRHASH - 1);
100100
}
101101

102+
/*
103+
* Allocate and initialize an nlm_host. Common to both client and server.
104+
*/
105+
static struct nlm_host *nlm_alloc_host(struct nlm_lookup_host_info *ni,
106+
struct nsm_handle *nsm)
107+
{
108+
struct nlm_host *host = NULL;
109+
unsigned long now = jiffies;
110+
111+
if (nsm != NULL)
112+
atomic_inc(&nsm->sm_count);
113+
else {
114+
host = NULL;
115+
nsm = nsm_get_handle(ni->sap, ni->salen,
116+
ni->hostname, ni->hostname_len);
117+
if (unlikely(nsm == NULL)) {
118+
dprintk("lockd: %s failed; no nsm handle\n",
119+
__func__);
120+
goto out;
121+
}
122+
}
123+
124+
host = kmalloc(sizeof(*host), GFP_KERNEL);
125+
if (unlikely(host == NULL)) {
126+
dprintk("lockd: %s failed; no memory\n", __func__);
127+
nsm_release(nsm);
128+
goto out;
129+
}
130+
131+
memcpy(nlm_addr(host), ni->sap, ni->salen);
132+
host->h_addrlen = ni->salen;
133+
rpc_set_port(nlm_addr(host), 0);
134+
host->h_srcaddrlen = 0;
135+
136+
host->h_rpcclnt = NULL;
137+
host->h_name = nsm->sm_name;
138+
host->h_version = ni->version;
139+
host->h_proto = ni->protocol;
140+
host->h_reclaiming = 0;
141+
host->h_server = ni->server;
142+
host->h_noresvport = ni->noresvport;
143+
host->h_inuse = 0;
144+
init_waitqueue_head(&host->h_gracewait);
145+
init_rwsem(&host->h_rwsem);
146+
host->h_state = 0;
147+
host->h_nsmstate = 0;
148+
host->h_pidcount = 0;
149+
atomic_set(&host->h_count, 1);
150+
mutex_init(&host->h_mutex);
151+
host->h_nextrebind = now + NLM_HOST_REBIND;
152+
host->h_expires = now + NLM_HOST_EXPIRE;
153+
INIT_LIST_HEAD(&host->h_lockowners);
154+
spin_lock_init(&host->h_lock);
155+
INIT_LIST_HEAD(&host->h_granted);
156+
INIT_LIST_HEAD(&host->h_reclaim);
157+
host->h_nsmhandle = nsm;
158+
host->h_addrbuf = nsm->sm_addrbuf;
159+
160+
out:
161+
return host;
162+
}
163+
102164
/*
103165
* Common host lookup routine for server & client
104166
*/
@@ -150,55 +212,13 @@ static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
150212
goto out;
151213
}
152214

153-
/*
154-
* The host wasn't in our hash table. If we don't
155-
* have an NSM handle for it yet, create one.
156-
*/
157-
if (nsm)
158-
atomic_inc(&nsm->sm_count);
159-
else {
160-
host = NULL;
161-
nsm = nsm_get_handle(ni->sap, ni->salen,
162-
ni->hostname, ni->hostname_len);
163-
if (!nsm) {
164-
dprintk("lockd: nlm_lookup_host failed; "
165-
"no nsm handle\n");
166-
goto out;
167-
}
168-
}
169-
170-
host = kzalloc(sizeof(*host), GFP_KERNEL);
171-
if (!host) {
172-
nsm_release(nsm);
173-
dprintk("lockd: nlm_lookup_host failed; no memory\n");
215+
host = nlm_alloc_host(ni, nsm);
216+
if (unlikely(host == NULL))
174217
goto out;
175-
}
176-
host->h_name = nsm->sm_name;
177-
host->h_addrbuf = nsm->sm_addrbuf;
178-
memcpy(nlm_addr(host), ni->sap, ni->salen);
179-
host->h_addrlen = ni->salen;
180-
rpc_set_port(nlm_addr(host), 0);
218+
181219
memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
182220
host->h_srcaddrlen = ni->src_len;
183-
host->h_version = ni->version;
184-
host->h_proto = ni->protocol;
185-
host->h_rpcclnt = NULL;
186-
mutex_init(&host->h_mutex);
187-
host->h_nextrebind = jiffies + NLM_HOST_REBIND;
188-
host->h_expires = jiffies + NLM_HOST_EXPIRE;
189-
atomic_set(&host->h_count, 1);
190-
init_waitqueue_head(&host->h_gracewait);
191-
init_rwsem(&host->h_rwsem);
192-
host->h_state = 0; /* pseudo NSM state */
193-
host->h_nsmstate = 0; /* real NSM state */
194-
host->h_nsmhandle = nsm;
195-
host->h_server = ni->server;
196-
host->h_noresvport = ni->noresvport;
197221
hlist_add_head(&host->h_hash, chain);
198-
INIT_LIST_HEAD(&host->h_lockowners);
199-
spin_lock_init(&host->h_lock);
200-
INIT_LIST_HEAD(&host->h_granted);
201-
INIT_LIST_HEAD(&host->h_reclaim);
202222

203223
nrhosts++;
204224

0 commit comments

Comments
 (0)