lockd.h 12 KB

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