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