1
2
3
4
5
6
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/nfsd/nfsfh.h>
21#include <linux/lockd/bind.h>
22#include <linux/lockd/xdr.h>
23#ifdef CONFIG_LOCKD_V4
24#include <linux/lockd/xdr4.h>
25#endif
26#include <linux/lockd/debug.h>
27
28
29
30
31#define LOCKD_VERSION "0.5"
32
33
34
35
36#define LOCKD_DFLT_TIMEO 10
37
38
39
40
41struct nlm_host {
42 struct hlist_node h_hash;
43 struct sockaddr_storage h_addr;
44 size_t h_addrlen;
45 struct sockaddr_storage h_srcaddr;
46 struct rpc_clnt *h_rpcclnt;
47 char *h_name;
48 u32 h_version;
49 unsigned short h_proto;
50 unsigned short h_reclaiming : 1,
51 h_server : 1,
52 h_noresvport : 1,
53 h_inuse : 1;
54 wait_queue_head_t h_gracewait;
55 struct rw_semaphore h_rwsem;
56 u32 h_state;
57 u32 h_nsmstate;
58 u32 h_pidcount;
59 atomic_t h_count;
60 struct mutex h_mutex;
61 unsigned long h_nextrebind;
62 unsigned long h_expires;
63 struct list_head h_lockowners;
64 spinlock_t h_lock;
65 struct list_head h_granted;
66 struct list_head h_reclaim;
67 struct nsm_handle *h_nsmhandle;
68 char *h_addrbuf;
69};
70
71
72
73
74
75
76
77#define NSM_ADDRBUF ((8 * 4 + 7) + (1 + 10) + 1)
78
79struct nsm_handle {
80 struct list_head sm_link;
81 atomic_t sm_count;
82 char *sm_mon_name;
83 char *sm_name;
84 struct sockaddr_storage sm_addr;
85 size_t sm_addrlen;
86 unsigned int sm_monitored : 1,
87 sm_sticky : 1;
88 struct nsm_private sm_priv;
89 char sm_addrbuf[NSM_ADDRBUF];
90};
91
92
93
94
95static inline struct sockaddr_in *nlm_addr_in(const struct nlm_host *host)
96{
97 return (struct sockaddr_in *)&host->h_addr;
98}
99
100static inline struct sockaddr *nlm_addr(const struct nlm_host *host)
101{
102 return (struct sockaddr *)&host->h_addr;
103}
104
105static inline struct sockaddr_in *nlm_srcaddr_in(const struct nlm_host *host)
106{
107 return (struct sockaddr_in *)&host->h_srcaddr;
108}
109
110static inline struct sockaddr *nlm_srcaddr(const struct nlm_host *host)
111{
112 return (struct sockaddr *)&host->h_srcaddr;
113}
114
115
116
117
118struct nlm_lockowner {
119 struct list_head list;
120 atomic_t count;
121
122 struct nlm_host *host;
123 fl_owner_t owner;
124 uint32_t pid;
125};
126
127struct nlm_wait;
128
129
130
131
132#define NLMCLNT_OHSIZE ((__NEW_UTS_LEN) + 10u)
133struct nlm_rqst {
134 atomic_t a_count;
135 unsigned int a_flags;
136 struct nlm_host * a_host;
137 struct nlm_args a_args;
138 struct nlm_res a_res;
139 struct nlm_block * a_block;
140 unsigned int a_retries;
141 u8 a_owner[NLMCLNT_OHSIZE];
142};
143
144
145
146
147
148struct nlm_file {
149 struct hlist_node f_list;
150 struct nfs_fh f_handle;
151 struct file * f_file;
152 struct nlm_share * f_shares;
153 struct list_head f_blocks;
154 unsigned int f_locks;
155 unsigned int f_count;
156 struct mutex f_mutex;
157};
158
159
160
161
162
163#define NLM_NEVER (~(unsigned long) 0)
164
165#define NLM_TIMEOUT (7 * HZ)
166
167struct nlm_block {
168 struct kref b_count;
169 struct list_head b_list;
170 struct list_head b_flist;
171 struct nlm_rqst * b_call;
172 struct svc_serv * b_daemon;
173 struct nlm_host * b_host;
174 unsigned long b_when;
175 unsigned int b_id;
176 unsigned char b_granted;
177 struct nlm_file * b_file;
178 struct cache_req * b_cache_req;
179 struct file_lock * b_fl;
180 struct cache_deferred_req * b_deferred_req;
181 unsigned int b_flags;
182#define B_QUEUED 1
183#define B_GOT_CALLBACK 2
184#define B_TIMED_OUT 4
185};
186
187
188
189
190extern struct rpc_program nlm_program;
191extern struct svc_procedure nlmsvc_procedures[];
192#ifdef CONFIG_LOCKD_V4
193extern struct svc_procedure nlmsvc_procedures4[];
194#endif
195extern int nlmsvc_grace_period;
196extern unsigned long nlmsvc_timeout;
197extern int nsm_use_hostnames;
198extern u32 nsm_local_state;
199
200
201
202
203struct nlm_rqst * nlm_alloc_call(struct nlm_host *host);
204void nlm_release_call(struct nlm_rqst *);
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 *);
207struct nlm_wait * nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl);
208void nlmclnt_finish_block(struct nlm_wait *block);
209int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout);
210__be32 nlmclnt_grant(const struct sockaddr *addr,
211 const struct nlm_lock *lock);
212void nlmclnt_recovery(struct nlm_host *);
213int nlmclnt_reclaim(struct nlm_host *, struct file_lock *);
214void nlmclnt_next_cookie(struct nlm_cookie *);
215
216
217
218
219struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
220 const size_t salen,
221 const unsigned short protocol,
222 const u32 version,
223 const char *hostname,
224 int noresvport);
225struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
226 const char *hostname,
227 const size_t hostname_len);
228struct rpc_clnt * nlm_bind_host(struct nlm_host *);
229void nlm_rebind_host(struct nlm_host *);
230struct nlm_host * nlm_get_host(struct nlm_host *);
231void nlm_release_host(struct nlm_host *);
232void nlm_shutdown_hosts(void);
233void nlm_host_rebooted(const struct nlm_reboot *);
234
235
236
237
238int nsm_monitor(const struct nlm_host *host);
239void nsm_unmonitor(const struct nlm_host *host);
240
241struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
242 const size_t salen,
243 const char *hostname,
244 const size_t hostname_len);
245struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info);
246void nsm_release(struct nsm_handle *nsm);
247
248
249
250
251
252typedef int (*nlm_host_match_fn_t)(void *cur, struct nlm_host *ref);
253
254
255
256
257__be32 nlmsvc_lock(struct svc_rqst *, struct nlm_file *,
258 struct nlm_host *, struct nlm_lock *, int,
259 struct nlm_cookie *, int);
260__be32 nlmsvc_unlock(struct nlm_file *, struct nlm_lock *);
261__be32 nlmsvc_testlock(struct svc_rqst *, struct nlm_file *,
262 struct nlm_host *, struct nlm_lock *,
263 struct nlm_lock *, struct nlm_cookie *);
264__be32 nlmsvc_cancel_blocked(struct nlm_file *, struct nlm_lock *);
265unsigned long nlmsvc_retry_blocked(void);
266void nlmsvc_traverse_blocks(struct nlm_host *, struct nlm_file *,
267 nlm_host_match_fn_t match);
268void nlmsvc_grant_reply(struct nlm_cookie *, __be32);
269
270
271
272
273__be32 nlm_lookup_file(struct svc_rqst *, struct nlm_file **,
274 struct nfs_fh *);
275void nlm_release_file(struct nlm_file *);
276void nlmsvc_mark_resources(void);
277void nlmsvc_free_host_resources(struct nlm_host *);
278void nlmsvc_invalidate_all(void);
279
280
281
282
283int nlmsvc_unlock_all_by_sb(struct super_block *sb);
284int nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr);
285
286static inline struct inode *nlmsvc_file_inode(struct nlm_file *file)
287{
288 return file->f_file->f_path.dentry->d_inode;
289}
290
291static inline int __nlm_privileged_request4(const struct sockaddr *sap)
292{
293 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
294
295 if (ntohs(sin->sin_port) > 1023)
296 return 0;
297
298 return ipv4_is_loopback(sin->sin_addr.s_addr);
299}
300
301#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
302static inline int __nlm_privileged_request6(const struct sockaddr *sap)
303{
304 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
305
306 if (ntohs(sin6->sin6_port) > 1023)
307 return 0;
308
309 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_MAPPED)
310 return ipv4_is_loopback(sin6->sin6_addr.s6_addr32[3]);
311
312 return ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LOOPBACK;
313}
314#else
315static inline int __nlm_privileged_request6(const struct sockaddr *sap)
316{
317 return 0;
318}
319#endif
320
321
322
323
324
325
326
327static inline int nlm_privileged_requester(const struct svc_rqst *rqstp)
328{
329 const struct sockaddr *sap = svc_addr(rqstp);
330
331 switch (sap->sa_family) {
332 case AF_INET:
333 return __nlm_privileged_request4(sap);
334 case AF_INET6:
335 return __nlm_privileged_request6(sap);
336 default:
337 return 0;
338 }
339}
340
341
342
343
344
345static inline int nlm_compare_locks(const struct file_lock *fl1,
346 const struct file_lock *fl2)
347{
348 return fl1->fl_pid == fl2->fl_pid
349 && fl1->fl_owner == fl2->fl_owner
350 && fl1->fl_start == fl2->fl_start
351 && fl1->fl_end == fl2->fl_end
352 &&(fl1->fl_type == fl2->fl_type || fl2->fl_type == F_UNLCK);
353}
354
355extern const struct lock_manager_operations nlmsvc_lock_operations;
356
357#endif
358
359#endif
360