Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * linux/include/linux/lockd/lockd.h
0004  *
0005  * General-purpose lockd include file.
0006  *
0007  * Copyright (C) 1996 Olaf Kirch <okir@monad.swb.de>
0008  */
0009 
0010 #ifndef LINUX_LOCKD_LOCKD_H
0011 #define LINUX_LOCKD_LOCKD_H
0012 
0013 /* XXX: a lot of this should really be under fs/lockd. */
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  * Version string
0032  */
0033 #define LOCKD_VERSION       "0.5"
0034 
0035 /*
0036  * Default timeout for RPC calls (seconds)
0037  */
0038 #define LOCKD_DFLT_TIMEO    10
0039 
0040 /*
0041  * Lockd host handle (used both by the client and server personality).
0042  */
0043 struct nlm_host {
0044     struct hlist_node   h_hash;     /* doubly linked list */
0045     struct sockaddr_storage h_addr;     /* peer address */
0046     size_t          h_addrlen;
0047     struct sockaddr_storage h_srcaddr;  /* our address (optional) */
0048     size_t          h_srcaddrlen;
0049     struct rpc_clnt     *h_rpcclnt; /* RPC client to talk to peer */
0050     char            *h_name;        /* remote hostname */
0051     u32         h_version;  /* interface version */
0052     unsigned short      h_proto;    /* transport proto */
0053     unsigned short      h_reclaiming : 1,
0054                 h_server     : 1, /* server side, not client side */
0055                 h_noresvport : 1,
0056                 h_inuse      : 1;
0057     wait_queue_head_t   h_gracewait;    /* wait while reclaiming */
0058     struct rw_semaphore h_rwsem;    /* Reboot recovery lock */
0059     u32         h_state;    /* pseudo-state counter */
0060     u32         h_nsmstate; /* true remote NSM state */
0061     u32         h_pidcount; /* Pseudopids */
0062     refcount_t      h_count;    /* reference count */
0063     struct mutex        h_mutex;    /* mutex for pmap binding */
0064     unsigned long       h_nextrebind;   /* next portmap call */
0065     unsigned long       h_expires;  /* eligible for GC */
0066     struct list_head    h_lockowners;   /* Lockowners for the client */
0067     spinlock_t      h_lock;
0068     struct list_head    h_granted;  /* Locks in GRANTED state */
0069     struct list_head    h_reclaim;  /* Locks in RECLAIM state */
0070     struct nsm_handle   *h_nsmhandle;   /* NSM status handle */
0071     char            *h_addrbuf; /* address eyecatcher */
0072     struct net      *net;       /* host net */
0073     const struct cred   *h_cred;
0074     char            nodename[UNX_MAXNODENAME + 1];
0075     const struct nlmclnt_operations *h_nlmclnt_ops; /* Callback ops for NLM users */
0076 };
0077 
0078 /*
0079  * The largest string sm_addrbuf should hold is a full-size IPv6 address
0080  * (no "::" anywhere) with a scope ID.  The buffer size is computed to
0081  * hold eight groups of colon-separated four-hex-digit numbers, a
0082  * percent sign, a scope id (at most 32 bits, in decimal), and NUL.
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;  /* don't unmonitor */
0095     struct nsm_private  sm_priv;
0096     char            sm_addrbuf[NSM_ADDRBUF];
0097 };
0098 
0099 /*
0100  * Rigorous type checking on sockaddr type conversions
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  * Map an fl_owner_t into a unique 32-bit "pid"
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  * Memory chunk for NLM client RPC request.
0138  */
0139 #define NLMCLNT_OHSIZE      ((__NEW_UTS_LEN) + 10u)
0140 struct nlm_rqst {
0141     refcount_t      a_count;
0142     unsigned int        a_flags;    /* initial RPC task flags */
0143     struct nlm_host *   a_host;     /* host handle */
0144     struct nlm_args     a_args;     /* arguments */
0145     struct nlm_res      a_res;      /* result */
0146     struct nlm_block *  a_block;
0147     unsigned int        a_retries;  /* Retry count */
0148     u8          a_owner[NLMCLNT_OHSIZE];
0149     void *  a_callback_data; /* sent to nlmclnt_operations callbacks */
0150 };
0151 
0152 /*
0153  * This struct describes a file held open by lockd on behalf of
0154  * an NFS client.
0155  */
0156 struct nlm_file {
0157     struct hlist_node   f_list;     /* linked list */
0158     struct nfs_fh       f_handle;   /* NFS file handle */
0159     struct file *       f_file[2];  /* VFS file pointers,
0160                            indexed by O_ flags */
0161     struct nlm_share *  f_shares;   /* DOS shares */
0162     struct list_head    f_blocks;   /* blocked locks */
0163     unsigned int        f_locks;    /* guesstimate # of locks */
0164     unsigned int        f_count;    /* reference count */
0165     struct mutex        f_mutex;    /* avoid concurrent access */
0166 };
0167 
0168 /*
0169  * This is a server block (i.e. a lock requested by some client which
0170  * couldn't be granted because of a conflicting lock).
0171  */
0172 #define NLM_NEVER       (~(unsigned long) 0)
0173 /* timeout on non-blocking call: */
0174 #define NLM_TIMEOUT     (7 * HZ)
0175 
0176 struct nlm_block {
0177     struct kref     b_count;    /* Reference count */
0178     struct list_head    b_list;     /* linked list of all blocks */
0179     struct list_head    b_flist;    /* linked list (per file) */
0180     struct nlm_rqst *   b_call;     /* RPC args & callback info */
0181     struct svc_serv *   b_daemon;   /* NLM service */
0182     struct nlm_host *   b_host;     /* host handle for RPC clnt */
0183     unsigned long       b_when;     /* next re-xmit */
0184     unsigned int        b_id;       /* block id */
0185     unsigned char       b_granted;  /* VFS granted lock */
0186     struct nlm_file *   b_file;     /* file in question */
0187     struct cache_req *  b_cache_req;    /* deferred request handling */
0188     struct cache_deferred_req * b_deferred_req;
0189     unsigned int        b_flags;    /* block flags */
0190 #define B_QUEUED        1   /* lock queued */
0191 #define B_GOT_CALLBACK      2   /* got lock or conflicting lock */
0192 #define B_TIMED_OUT     4   /* filesystem too slow to respond */
0193 };
0194 
0195 /*
0196  * Global variables
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  * Lockd client functions
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  * Host cache
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  * Host monitoring
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  * This is used in garbage collection and resource reclaim
0266  * A return value != 0 means destroy the lock/block/share
0267  */
0268 typedef int   (*nlm_host_match_fn_t)(void *cur, struct nlm_host *ref);
0269 
0270 /*
0271  * Server-side lock handling
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  * File handling for the server personality
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  * Cluster failover support
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   /* IS_ENABLED(CONFIG_IPV6) */
0342 static inline int __nlm_privileged_request6(const struct sockaddr *sap)
0343 {
0344     return 0;
0345 }
0346 #endif  /* IS_ENABLED(CONFIG_IPV6) */
0347 
0348 /*
0349  * Ensure incoming requests are from local privileged callers.
0350  *
0351  * Return TRUE if sender is local and is connecting via a privileged port;
0352  * otherwise return FALSE.
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  * Compare two NLM locks.
0370  * When the second lock is of type F_UNLCK, this acts like a wildcard.
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 /* LINUX_LOCKD_LOCKD_H */