1/*
2 * linux/include/linux/lockd/lockd.h
3 *
4 * General-purpose lockd include file.
5 *
6 * Copyright (C) 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
9#ifndef LINUX_LOCKD_LOCKD_H
10#define LINUX_LOCKD_LOCKD_H
11
12#ifdef __KERNEL__
13
14#include <linux/in.h>
15#include <linux/in6.h>
16#include <net/ipv6.h>
17#include <linux/fs.h>
18#include <linux/kref.h>
19#include <linux/utsname.h>
20#include <linux/lockd/bind.h>
21#include <linux/lockd/xdr.h>
22#ifdef CONFIG_LOCKD_V4
23#include <linux/lockd/xdr4.h>
24#endif
25#include <linux/lockd/debug.h>
26#include <linux/sunrpc/svc.h>
27
28/*
29 * Version string
30 */
31#define LOCKD_VERSION		"0.5"
32
33/*
34 * Default timeout for RPC calls (seconds)
35 */
36#define LOCKD_DFLT_TIMEO	10
37
38/*
39 * Lockd host handle (used both by the client and server personality).
40 */
41struct nlm_host {
42	struct hlist_node	h_hash;		/* doubly linked list */
43	struct sockaddr_storage	h_addr;		/* peer address */
44	size_t			h_addrlen;
45	struct sockaddr_storage	h_srcaddr;	/* our address (optional) */
46	size_t			h_srcaddrlen;
47	struct rpc_clnt		*h_rpcclnt;	/* RPC client to talk to peer */
48	char			*h_name;		/* remote hostname */
49	u32			h_version;	/* interface version */
50	unsigned short		h_proto;	/* transport proto */
51	unsigned short		h_reclaiming : 1,
52				h_server     : 1, /* server side, not client side */
53				h_noresvport : 1,
54				h_inuse      : 1;
55	wait_queue_head_t	h_gracewait;	/* wait while reclaiming */
56	struct rw_semaphore	h_rwsem;	/* Reboot recovery lock */
57	u32			h_state;	/* pseudo-state counter */
58	u32			h_nsmstate;	/* true remote NSM state */
59	u32			h_pidcount;	/* Pseudopids */
60	atomic_t		h_count;	/* reference count */
61	struct mutex		h_mutex;	/* mutex for pmap binding */
62	unsigned long		h_nextrebind;	/* next portmap call */
63	unsigned long		h_expires;	/* eligible for GC */
64	struct list_head	h_lockowners;	/* Lockowners for the client */
65	spinlock_t		h_lock;
66	struct list_head	h_granted;	/* Locks in GRANTED state */
67	struct list_head	h_reclaim;	/* Locks in RECLAIM state */
68	struct nsm_handle	*h_nsmhandle;	/* NSM status handle */
69	char			*h_addrbuf;	/* address eyecatcher */
70	struct net		*net;		/* host net */
71};
72
73/*
74 * The largest string sm_addrbuf should hold is a full-size IPv6 address
75 * (no "::" anywhere) with a scope ID.  The buffer size is computed to
76 * hold eight groups of colon-separated four-hex-digit numbers, a
77 * percent sign, a scope id (at most 32 bits, in decimal), and NUL.
78 */
79#define NSM_ADDRBUF		((8 * 4 + 7) + (1 + 10) + 1)
80
81struct nsm_handle {
82	struct list_head	sm_link;
83	atomic_t		sm_count;
84	char			*sm_mon_name;
85	char			*sm_name;
86	struct sockaddr_storage	sm_addr;
87	size_t			sm_addrlen;
88	unsigned int		sm_monitored : 1,
89				sm_sticky : 1;	/* don't unmonitor */
90	struct nsm_private	sm_priv;
91	char			sm_addrbuf[NSM_ADDRBUF];
92};
93
94/*
95 * Rigorous type checking on sockaddr type conversions
96 */
97static inline struct sockaddr_in *nlm_addr_in(const struct nlm_host *host)
98{
99	return (struct sockaddr_in *)&host->h_addr;
100}
101
102static inline struct sockaddr *nlm_addr(const struct nlm_host *host)
103{
104	return (struct sockaddr *)&host->h_addr;
105}
106
107static inline struct sockaddr_in *nlm_srcaddr_in(const struct nlm_host *host)
108{
109	return (struct sockaddr_in *)&host->h_srcaddr;
110}
111
112static inline struct sockaddr *nlm_srcaddr(const struct nlm_host *host)
113{
114	return (struct sockaddr *)&host->h_srcaddr;
115}
116
117/*
118 * Map an fl_owner_t into a unique 32-bit "pid"
119 */
120struct nlm_lockowner {
121	struct list_head list;
122	atomic_t count;
123
124	struct nlm_host *host;
125	fl_owner_t owner;
126	uint32_t pid;
127};
128
129struct nlm_wait;
130
131/*
132 * Memory chunk for NLM client RPC request.
133 */
134#define NLMCLNT_OHSIZE		((__NEW_UTS_LEN) + 10u)
135struct nlm_rqst {
136	atomic_t		a_count;
137	unsigned int		a_flags;	/* initial RPC task flags */
138	struct nlm_host *	a_host;		/* host handle */
139	struct nlm_args		a_args;		/* arguments */
140	struct nlm_res		a_res;		/* result */
141	struct nlm_block *	a_block;
142	unsigned int		a_retries;	/* Retry count */
143	u8			a_owner[NLMCLNT_OHSIZE];
144};
145
146/*
147 * This struct describes a file held open by lockd on behalf of
148 * an NFS client.
149 */
150struct nlm_file {
151	struct hlist_node	f_list;		/* linked list */
152	struct nfs_fh		f_handle;	/* NFS file handle */
153	struct file *		f_file;		/* VFS file pointer */
154	struct nlm_share *	f_shares;	/* DOS shares */
155	struct list_head	f_blocks;	/* blocked locks */
156	unsigned int		f_locks;	/* guesstimate # of locks */
157	unsigned int		f_count;	/* reference count */
158	struct mutex		f_mutex;	/* avoid concurrent access */
159};
160
161/*
162 * This is a server block (i.e. a lock requested by some client which
163 * couldn't be granted because of a conflicting lock).
164 */
165#define NLM_NEVER		(~(unsigned long) 0)
166/* timeout on non-blocking call: */
167#define NLM_TIMEOUT		(7 * HZ)
168
169struct nlm_block {
170	struct kref		b_count;	/* Reference count */
171	struct list_head	b_list;		/* linked list of all blocks */
172	struct list_head	b_flist;	/* linked list (per file) */
173	struct nlm_rqst	*	b_call;		/* RPC args & callback info */
174	struct svc_serv *	b_daemon;	/* NLM service */
175	struct nlm_host *	b_host;		/* host handle for RPC clnt */
176	unsigned long		b_when;		/* next re-xmit */
177	unsigned int		b_id;		/* block id */
178	unsigned char		b_granted;	/* VFS granted lock */
179	struct nlm_file *	b_file;		/* file in question */
180	struct cache_req *	b_cache_req;	/* deferred request handling */
181	struct cache_deferred_req * b_deferred_req;
182	unsigned int		b_flags;	/* block flags */
183#define B_QUEUED		1	/* lock queued */
184#define B_GOT_CALLBACK		2	/* got lock or conflicting lock */
185#define B_TIMED_OUT		4	/* filesystem too slow to respond */
186};
187
188/*
189 * Global variables
190 */
191extern const struct rpc_program	nlm_program;
192extern struct svc_procedure	nlmsvc_procedures[];
193#ifdef CONFIG_LOCKD_V4
194extern struct svc_procedure	nlmsvc_procedures4[];
195#endif
196extern int			nlmsvc_grace_period;
197extern unsigned long		nlmsvc_timeout;
198extern bool			nsm_use_hostnames;
199extern u32			nsm_local_state;
200
201/*
202 * Lockd client functions
203 */
204struct nlm_rqst * nlm_alloc_call(struct nlm_host *host);
205int		  nlm_async_call(struct nlm_rqst *, u32, const struct rpc_call_ops *);
206int		  nlm_async_reply(struct nlm_rqst *, u32, const struct rpc_call_ops *);
207void		  nlmclnt_release_call(struct nlm_rqst *);
208struct nlm_wait * nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl);
209void		  nlmclnt_finish_block(struct nlm_wait *block);
210int		  nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout);
211__be32		  nlmclnt_grant(const struct sockaddr *addr,
212				const struct nlm_lock *lock);
213void		  nlmclnt_recovery(struct nlm_host *);
214int		  nlmclnt_reclaim(struct nlm_host *, struct file_lock *,
215				  struct nlm_rqst *);
216void		  nlmclnt_next_cookie(struct nlm_cookie *);
217
218/*
219 * Host cache
220 */
221struct nlm_host  *nlmclnt_lookup_host(const struct sockaddr *sap,
222					const size_t salen,
223					const unsigned short protocol,
224					const u32 version,
225					const char *hostname,
226					int noresvport,
227					struct net *net);
228void		  nlmclnt_release_host(struct nlm_host *);
229struct nlm_host  *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
230					const char *hostname,
231					const size_t hostname_len);
232void		  nlmsvc_release_host(struct nlm_host *);
233struct rpc_clnt * nlm_bind_host(struct nlm_host *);
234void		  nlm_rebind_host(struct nlm_host *);
235struct nlm_host * nlm_get_host(struct nlm_host *);
236void		  nlm_shutdown_hosts(void);
237void		  nlm_shutdown_hosts_net(struct net *net);
238void		  nlm_host_rebooted(const struct nlm_reboot *);
239
240/*
241 * Host monitoring
242 */
243int		  nsm_monitor(const struct nlm_host *host);
244void		  nsm_unmonitor(const struct nlm_host *host);
245
246struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
247					const size_t salen,
248					const char *hostname,
249					const size_t hostname_len);
250struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info);
251void		  nsm_release(struct nsm_handle *nsm);
252
253/*
254 * This is used in garbage collection and resource reclaim
255 * A return value != 0 means destroy the lock/block/share
256 */
257typedef int	  (*nlm_host_match_fn_t)(void *cur, struct nlm_host *ref);
258
259/*
260 * Server-side lock handling
261 */
262__be32		  nlmsvc_lock(struct svc_rqst *, struct nlm_file *,
263			      struct nlm_host *, struct nlm_lock *, int,
264			      struct nlm_cookie *, int);
265__be32		  nlmsvc_unlock(struct net *net, struct nlm_file *, struct nlm_lock *);
266__be32		  nlmsvc_testlock(struct svc_rqst *, struct nlm_file *,
267			struct nlm_host *, struct nlm_lock *,
268			struct nlm_lock *, struct nlm_cookie *);
269__be32		  nlmsvc_cancel_blocked(struct net *net, struct nlm_file *, struct nlm_lock *);
270unsigned long	  nlmsvc_retry_blocked(void);
271void		  nlmsvc_traverse_blocks(struct nlm_host *, struct nlm_file *,
272					nlm_host_match_fn_t match);
273void		  nlmsvc_grant_reply(struct nlm_cookie *, __be32);
274void		  nlmsvc_release_call(struct nlm_rqst *);
275
276/*
277 * File handling for the server personality
278 */
279__be32		  nlm_lookup_file(struct svc_rqst *, struct nlm_file **,
280					struct nfs_fh *);
281void		  nlm_release_file(struct nlm_file *);
282void		  nlmsvc_mark_resources(struct net *);
283void		  nlmsvc_free_host_resources(struct nlm_host *);
284void		  nlmsvc_invalidate_all(void);
285
286/*
287 * Cluster failover support
288 */
289int           nlmsvc_unlock_all_by_sb(struct super_block *sb);
290int           nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr);
291
292static inline struct inode *nlmsvc_file_inode(struct nlm_file *file)
293{
294	return file_inode(file->f_file);
295}
296
297static inline int __nlm_privileged_request4(const struct sockaddr *sap)
298{
299	const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
300
301	if (ntohs(sin->sin_port) > 1023)
302		return 0;
303
304	return ipv4_is_loopback(sin->sin_addr.s_addr);
305}
306
307#if IS_ENABLED(CONFIG_IPV6)
308static inline int __nlm_privileged_request6(const struct sockaddr *sap)
309{
310	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
311
312	if (ntohs(sin6->sin6_port) > 1023)
313		return 0;
314
315	if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_MAPPED)
316		return ipv4_is_loopback(sin6->sin6_addr.s6_addr32[3]);
317
318	return ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LOOPBACK;
319}
320#else	/* IS_ENABLED(CONFIG_IPV6) */
321static inline int __nlm_privileged_request6(const struct sockaddr *sap)
322{
323	return 0;
324}
325#endif	/* IS_ENABLED(CONFIG_IPV6) */
326
327/*
328 * Ensure incoming requests are from local privileged callers.
329 *
330 * Return TRUE if sender is local and is connecting via a privileged port;
331 * otherwise return FALSE.
332 */
333static inline int nlm_privileged_requester(const struct svc_rqst *rqstp)
334{
335	const struct sockaddr *sap = svc_addr(rqstp);
336
337	switch (sap->sa_family) {
338	case AF_INET:
339		return __nlm_privileged_request4(sap);
340	case AF_INET6:
341		return __nlm_privileged_request6(sap);
342	default:
343		return 0;
344	}
345}
346
347/*
348 * Compare two NLM locks.
349 * When the second lock is of type F_UNLCK, this acts like a wildcard.
350 */
351static inline int nlm_compare_locks(const struct file_lock *fl1,
352				    const struct file_lock *fl2)
353{
354	return	fl1->fl_pid   == fl2->fl_pid
355	     && fl1->fl_owner == fl2->fl_owner
356	     && fl1->fl_start == fl2->fl_start
357	     && fl1->fl_end   == fl2->fl_end
358	     &&(fl1->fl_type  == fl2->fl_type || fl2->fl_type == F_UNLCK);
359}
360
361extern const struct lock_manager_operations nlmsvc_lock_operations;
362
363#endif /* __KERNEL__ */
364
365#endif /* LINUX_LOCKD_LOCKD_H */
366