0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef LINUX_LOCKD_LOCKD_H
0011 #define LINUX_LOCKD_LOCKD_H
0012
0013
0014
0015 #include <linux/in.h>
0016 #include <linux/in6.h>
0017 #include <net/ipv6.h>
0018 #include <linux/fs.h>
0019 #include <linux/kref.h>
0020 #include <linux/refcount.h>
0021 #include <linux/utsname.h>
0022 #include <linux/lockd/bind.h>
0023 #include <linux/lockd/xdr.h>
0024 #ifdef CONFIG_LOCKD_V4
0025 #include <linux/lockd/xdr4.h>
0026 #endif
0027 #include <linux/lockd/debug.h>
0028 #include <linux/sunrpc/svc.h>
0029
0030
0031
0032
0033 #define LOCKD_VERSION "0.5"
0034
0035
0036
0037
0038 #define LOCKD_DFLT_TIMEO 10
0039
0040
0041
0042
0043 struct nlm_host {
0044 struct hlist_node h_hash;
0045 struct sockaddr_storage h_addr;
0046 size_t h_addrlen;
0047 struct sockaddr_storage h_srcaddr;
0048 size_t h_srcaddrlen;
0049 struct rpc_clnt *h_rpcclnt;
0050 char *h_name;
0051 u32 h_version;
0052 unsigned short h_proto;
0053 unsigned short h_reclaiming : 1,
0054 h_server : 1,
0055 h_noresvport : 1,
0056 h_inuse : 1;
0057 wait_queue_head_t h_gracewait;
0058 struct rw_semaphore h_rwsem;
0059 u32 h_state;
0060 u32 h_nsmstate;
0061 u32 h_pidcount;
0062 refcount_t h_count;
0063 struct mutex h_mutex;
0064 unsigned long h_nextrebind;
0065 unsigned long h_expires;
0066 struct list_head h_lockowners;
0067 spinlock_t h_lock;
0068 struct list_head h_granted;
0069 struct list_head h_reclaim;
0070 struct nsm_handle *h_nsmhandle;
0071 char *h_addrbuf;
0072 struct net *net;
0073 const struct cred *h_cred;
0074 char nodename[UNX_MAXNODENAME + 1];
0075 const struct nlmclnt_operations *h_nlmclnt_ops;
0076 };
0077
0078
0079
0080
0081
0082
0083
0084 #define NSM_ADDRBUF ((8 * 4 + 7) + (1 + 10) + 1)
0085
0086 struct nsm_handle {
0087 struct list_head sm_link;
0088 refcount_t sm_count;
0089 char *sm_mon_name;
0090 char *sm_name;
0091 struct sockaddr_storage sm_addr;
0092 size_t sm_addrlen;
0093 unsigned int sm_monitored : 1,
0094 sm_sticky : 1;
0095 struct nsm_private sm_priv;
0096 char sm_addrbuf[NSM_ADDRBUF];
0097 };
0098
0099
0100
0101
0102 static inline struct sockaddr_in *nlm_addr_in(const struct nlm_host *host)
0103 {
0104 return (struct sockaddr_in *)&host->h_addr;
0105 }
0106
0107 static inline struct sockaddr *nlm_addr(const struct nlm_host *host)
0108 {
0109 return (struct sockaddr *)&host->h_addr;
0110 }
0111
0112 static inline struct sockaddr_in *nlm_srcaddr_in(const struct nlm_host *host)
0113 {
0114 return (struct sockaddr_in *)&host->h_srcaddr;
0115 }
0116
0117 static inline struct sockaddr *nlm_srcaddr(const struct nlm_host *host)
0118 {
0119 return (struct sockaddr *)&host->h_srcaddr;
0120 }
0121
0122
0123
0124
0125 struct nlm_lockowner {
0126 struct list_head list;
0127 refcount_t count;
0128
0129 struct nlm_host *host;
0130 fl_owner_t owner;
0131 uint32_t pid;
0132 };
0133
0134 struct nlm_wait;
0135
0136
0137
0138
0139 #define NLMCLNT_OHSIZE ((__NEW_UTS_LEN) + 10u)
0140 struct nlm_rqst {
0141 refcount_t a_count;
0142 unsigned int a_flags;
0143 struct nlm_host * a_host;
0144 struct nlm_args a_args;
0145 struct nlm_res a_res;
0146 struct nlm_block * a_block;
0147 unsigned int a_retries;
0148 u8 a_owner[NLMCLNT_OHSIZE];
0149 void * a_callback_data;
0150 };
0151
0152
0153
0154
0155
0156 struct nlm_file {
0157 struct hlist_node f_list;
0158 struct nfs_fh f_handle;
0159 struct file * f_file[2];
0160
0161 struct nlm_share * f_shares;
0162 struct list_head f_blocks;
0163 unsigned int f_locks;
0164 unsigned int f_count;
0165 struct mutex f_mutex;
0166 };
0167
0168
0169
0170
0171
0172 #define NLM_NEVER (~(unsigned long) 0)
0173
0174 #define NLM_TIMEOUT (7 * HZ)
0175
0176 struct nlm_block {
0177 struct kref b_count;
0178 struct list_head b_list;
0179 struct list_head b_flist;
0180 struct nlm_rqst * b_call;
0181 struct svc_serv * b_daemon;
0182 struct nlm_host * b_host;
0183 unsigned long b_when;
0184 unsigned int b_id;
0185 unsigned char b_granted;
0186 struct nlm_file * b_file;
0187 struct cache_req * b_cache_req;
0188 struct cache_deferred_req * b_deferred_req;
0189 unsigned int b_flags;
0190 #define B_QUEUED 1
0191 #define B_GOT_CALLBACK 2
0192 #define B_TIMED_OUT 4
0193 };
0194
0195
0196
0197
0198 extern const struct rpc_program nlm_program;
0199 extern const struct svc_procedure nlmsvc_procedures[];
0200 #ifdef CONFIG_LOCKD_V4
0201 extern const struct svc_procedure nlmsvc_procedures4[];
0202 #endif
0203 extern int nlmsvc_grace_period;
0204 extern unsigned long nlmsvc_timeout;
0205 extern bool nsm_use_hostnames;
0206 extern u32 nsm_local_state;
0207
0208
0209
0210
0211 struct nlm_rqst * nlm_alloc_call(struct nlm_host *host);
0212 int nlm_async_call(struct nlm_rqst *, u32, const struct rpc_call_ops *);
0213 int nlm_async_reply(struct nlm_rqst *, u32, const struct rpc_call_ops *);
0214 void nlmclnt_release_call(struct nlm_rqst *);
0215 struct nlm_wait * nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl);
0216 void nlmclnt_finish_block(struct nlm_wait *block);
0217 int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout);
0218 __be32 nlmclnt_grant(const struct sockaddr *addr,
0219 const struct nlm_lock *lock);
0220 void nlmclnt_recovery(struct nlm_host *);
0221 int nlmclnt_reclaim(struct nlm_host *, struct file_lock *,
0222 struct nlm_rqst *);
0223 void nlmclnt_next_cookie(struct nlm_cookie *);
0224
0225
0226
0227
0228 struct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
0229 const size_t salen,
0230 const unsigned short protocol,
0231 const u32 version,
0232 const char *hostname,
0233 int noresvport,
0234 struct net *net,
0235 const struct cred *cred);
0236 void nlmclnt_release_host(struct nlm_host *);
0237 struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
0238 const char *hostname,
0239 const size_t hostname_len);
0240 void nlmsvc_release_host(struct nlm_host *);
0241 struct rpc_clnt * nlm_bind_host(struct nlm_host *);
0242 void nlm_rebind_host(struct nlm_host *);
0243 struct nlm_host * nlm_get_host(struct nlm_host *);
0244 void nlm_shutdown_hosts(void);
0245 void nlm_shutdown_hosts_net(struct net *net);
0246 void nlm_host_rebooted(const struct net *net,
0247 const struct nlm_reboot *);
0248
0249
0250
0251
0252 int nsm_monitor(const struct nlm_host *host);
0253 void nsm_unmonitor(const struct nlm_host *host);
0254
0255 struct nsm_handle *nsm_get_handle(const struct net *net,
0256 const struct sockaddr *sap,
0257 const size_t salen,
0258 const char *hostname,
0259 const size_t hostname_len);
0260 struct nsm_handle *nsm_reboot_lookup(const struct net *net,
0261 const struct nlm_reboot *info);
0262 void nsm_release(struct nsm_handle *nsm);
0263
0264
0265
0266
0267
0268 typedef int (*nlm_host_match_fn_t)(void *cur, struct nlm_host *ref);
0269
0270
0271
0272
0273 int lock_to_openmode(struct file_lock *);
0274 __be32 nlmsvc_lock(struct svc_rqst *, struct nlm_file *,
0275 struct nlm_host *, struct nlm_lock *, int,
0276 struct nlm_cookie *, int);
0277 __be32 nlmsvc_unlock(struct net *net, struct nlm_file *, struct nlm_lock *);
0278 __be32 nlmsvc_testlock(struct svc_rqst *, struct nlm_file *,
0279 struct nlm_host *, struct nlm_lock *,
0280 struct nlm_lock *, struct nlm_cookie *);
0281 __be32 nlmsvc_cancel_blocked(struct net *net, struct nlm_file *, struct nlm_lock *);
0282 unsigned long nlmsvc_retry_blocked(void);
0283 void nlmsvc_traverse_blocks(struct nlm_host *, struct nlm_file *,
0284 nlm_host_match_fn_t match);
0285 void nlmsvc_grant_reply(struct nlm_cookie *, __be32);
0286 void nlmsvc_release_call(struct nlm_rqst *);
0287 void nlmsvc_locks_init_private(struct file_lock *, struct nlm_host *, pid_t);
0288
0289
0290
0291
0292 __be32 nlm_lookup_file(struct svc_rqst *, struct nlm_file **,
0293 struct nlm_lock *);
0294 void nlm_release_file(struct nlm_file *);
0295 void nlmsvc_put_lockowner(struct nlm_lockowner *);
0296 void nlmsvc_release_lockowner(struct nlm_lock *);
0297 void nlmsvc_mark_resources(struct net *);
0298 void nlmsvc_free_host_resources(struct nlm_host *);
0299 void nlmsvc_invalidate_all(void);
0300
0301
0302
0303
0304 int nlmsvc_unlock_all_by_sb(struct super_block *sb);
0305 int nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr);
0306
0307 static inline struct file *nlmsvc_file_file(struct nlm_file *file)
0308 {
0309 return file->f_file[O_RDONLY] ?
0310 file->f_file[O_RDONLY] : file->f_file[O_WRONLY];
0311 }
0312
0313 static inline struct inode *nlmsvc_file_inode(struct nlm_file *file)
0314 {
0315 return locks_inode(nlmsvc_file_file(file));
0316 }
0317
0318 static inline int __nlm_privileged_request4(const struct sockaddr *sap)
0319 {
0320 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
0321
0322 if (ntohs(sin->sin_port) > 1023)
0323 return 0;
0324
0325 return ipv4_is_loopback(sin->sin_addr.s_addr);
0326 }
0327
0328 #if IS_ENABLED(CONFIG_IPV6)
0329 static inline int __nlm_privileged_request6(const struct sockaddr *sap)
0330 {
0331 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
0332
0333 if (ntohs(sin6->sin6_port) > 1023)
0334 return 0;
0335
0336 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_MAPPED)
0337 return ipv4_is_loopback(sin6->sin6_addr.s6_addr32[3]);
0338
0339 return ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LOOPBACK;
0340 }
0341 #else
0342 static inline int __nlm_privileged_request6(const struct sockaddr *sap)
0343 {
0344 return 0;
0345 }
0346 #endif
0347
0348
0349
0350
0351
0352
0353
0354 static inline int nlm_privileged_requester(const struct svc_rqst *rqstp)
0355 {
0356 const struct sockaddr *sap = svc_addr(rqstp);
0357
0358 switch (sap->sa_family) {
0359 case AF_INET:
0360 return __nlm_privileged_request4(sap);
0361 case AF_INET6:
0362 return __nlm_privileged_request6(sap);
0363 default:
0364 return 0;
0365 }
0366 }
0367
0368
0369
0370
0371
0372 static inline int nlm_compare_locks(const struct file_lock *fl1,
0373 const struct file_lock *fl2)
0374 {
0375 return locks_inode(fl1->fl_file) == locks_inode(fl2->fl_file)
0376 && fl1->fl_pid == fl2->fl_pid
0377 && fl1->fl_owner == fl2->fl_owner
0378 && fl1->fl_start == fl2->fl_start
0379 && fl1->fl_end == fl2->fl_end
0380 &&(fl1->fl_type == fl2->fl_type || fl2->fl_type == F_UNLCK);
0381 }
0382
0383 extern const struct lock_manager_operations nlmsvc_lock_operations;
0384
0385 #endif