1  /* SPDX-License-Identifier: GPL-2.0 */
2  /*
3   * (C) 2001 Clemson University and The University of Chicago
4   *
5   * See COPYING in top-level directory.
6   */
7  
8  /*
9   *  The ORANGEFS Linux kernel support allows ORANGEFS volumes to be mounted and
10   *  accessed through the Linux VFS (i.e. using standard I/O system calls).
11   *  This support is only needed on clients that wish to mount the file system.
12   *
13   */
14  
15  /*
16   *  Declarations and macros for the ORANGEFS Linux kernel support.
17   */
18  
19  #ifndef __ORANGEFSKERNEL_H
20  #define __ORANGEFSKERNEL_H
21  
22  #include <linux/kernel.h>
23  #include <linux/moduleparam.h>
24  #include <linux/statfs.h>
25  #include <linux/backing-dev.h>
26  #include <linux/device.h>
27  #include <linux/mpage.h>
28  #include <linux/namei.h>
29  #include <linux/errno.h>
30  #include <linux/init.h>
31  #include <linux/module.h>
32  #include <linux/slab.h>
33  #include <linux/types.h>
34  #include <linux/fs.h>
35  #include <linux/vmalloc.h>
36  
37  #include <linux/aio.h>
38  #include <linux/posix_acl.h>
39  #include <linux/posix_acl_xattr.h>
40  #include <linux/compat.h>
41  #include <linux/mount.h>
42  #include <linux/uaccess.h>
43  #include <linux/atomic.h>
44  #include <linux/uio.h>
45  #include <linux/sched/signal.h>
46  #include <linux/mm.h>
47  #include <linux/wait.h>
48  #include <linux/dcache.h>
49  #include <linux/pagemap.h>
50  #include <linux/poll.h>
51  #include <linux/rwsem.h>
52  #include <linux/xattr.h>
53  #include <linux/exportfs.h>
54  #include <linux/hashtable.h>
55  
56  #include <linux/unaligned.h>
57  
58  #include "orangefs-dev-proto.h"
59  
60  #define ORANGEFS_DEFAULT_OP_TIMEOUT_SECS       20
61  
62  #define ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS   30
63  
64  #define ORANGEFS_DEFAULT_SLOT_TIMEOUT_SECS     900	/* 15 minutes */
65  
66  #define ORANGEFS_REQDEVICE_NAME          "pvfs2-req"
67  
68  #define ORANGEFS_DEVREQ_MAGIC             0x20030529
69  #define ORANGEFS_PURGE_RETRY_COUNT     0x00000005
70  
71  #define MAX_DEV_REQ_UPSIZE (2 * sizeof(__s32) +   \
72  sizeof(__u64) + sizeof(struct orangefs_upcall_s))
73  #define MAX_DEV_REQ_DOWNSIZE (2 * sizeof(__s32) + \
74  sizeof(__u64) + sizeof(struct orangefs_downcall_s))
75  
76  /*
77   * valid orangefs kernel operation states
78   *
79   * unknown  - op was just initialized
80   * waiting  - op is on request_list (upward bound)
81   * inprogr  - op is in progress (waiting for downcall)
82   * serviced - op has matching downcall; ok
83   * purged   - op has to start a timer since client-core
84   *            exited uncleanly before servicing op
85   * given up - submitter has given up waiting for it
86   */
87  enum orangefs_vfs_op_states {
88  	OP_VFS_STATE_UNKNOWN = 0,
89  	OP_VFS_STATE_WAITING = 1,
90  	OP_VFS_STATE_INPROGR = 2,
91  	OP_VFS_STATE_SERVICED = 4,
92  	OP_VFS_STATE_PURGED = 8,
93  	OP_VFS_STATE_GIVEN_UP = 16,
94  };
95  
96  extern const struct xattr_handler * const orangefs_xattr_handlers[];
97  
98  extern struct posix_acl *orangefs_get_acl(struct inode *inode, int type, bool rcu);
99  extern int orangefs_set_acl(struct mnt_idmap *idmap,
100  			    struct dentry *dentry, struct posix_acl *acl,
101  			    int type);
102  int __orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
103  
104  /*
105   * orangefs data structures
106   */
107  struct orangefs_kernel_op_s {
108  	enum orangefs_vfs_op_states op_state;
109  	__u64 tag;
110  
111  	/*
112  	 * Set uses_shared_memory to non zero if this operation uses
113  	 * shared memory. If true, then a retry on the op must also
114  	 * get a new shared memory buffer and re-populate it.
115  	 * Cancels don't care - it only matters for service_operation()
116  	 * retry logics and cancels don't go through it anymore. It
117  	 * safely stays non-zero when we use it as slot_to_free.
118  	 */
119  	union {
120  		int uses_shared_memory;
121  		int slot_to_free;
122  	};
123  
124  	struct orangefs_upcall_s upcall;
125  	struct orangefs_downcall_s downcall;
126  
127  	struct completion waitq;
128  	spinlock_t lock;
129  
130  	int attempts;
131  
132  	struct list_head list;
133  };
134  
135  #define set_op_state_waiting(op)     ((op)->op_state = OP_VFS_STATE_WAITING)
136  #define set_op_state_inprogress(op)  ((op)->op_state = OP_VFS_STATE_INPROGR)
137  #define set_op_state_given_up(op)  ((op)->op_state = OP_VFS_STATE_GIVEN_UP)
set_op_state_serviced(struct orangefs_kernel_op_s * op)138  static inline void set_op_state_serviced(struct orangefs_kernel_op_s *op)
139  {
140  	op->op_state = OP_VFS_STATE_SERVICED;
141  	complete(&op->waitq);
142  }
143  
144  #define op_state_waiting(op)     ((op)->op_state & OP_VFS_STATE_WAITING)
145  #define op_state_in_progress(op) ((op)->op_state & OP_VFS_STATE_INPROGR)
146  #define op_state_serviced(op)    ((op)->op_state & OP_VFS_STATE_SERVICED)
147  #define op_state_purged(op)      ((op)->op_state & OP_VFS_STATE_PURGED)
148  #define op_state_given_up(op)    ((op)->op_state & OP_VFS_STATE_GIVEN_UP)
149  #define op_is_cancel(op)         ((op)->upcall.type == ORANGEFS_VFS_OP_CANCEL)
150  
151  void op_release(struct orangefs_kernel_op_s *op);
152  
153  extern void orangefs_bufmap_put(int);
put_cancel(struct orangefs_kernel_op_s * op)154  static inline void put_cancel(struct orangefs_kernel_op_s *op)
155  {
156  	orangefs_bufmap_put(op->slot_to_free);
157  	op_release(op);
158  }
159  
set_op_state_purged(struct orangefs_kernel_op_s * op)160  static inline void set_op_state_purged(struct orangefs_kernel_op_s *op)
161  {
162  	spin_lock(&op->lock);
163  	if (unlikely(op_is_cancel(op))) {
164  		list_del_init(&op->list);
165  		spin_unlock(&op->lock);
166  		put_cancel(op);
167  	} else {
168  		op->op_state |= OP_VFS_STATE_PURGED;
169  		complete(&op->waitq);
170  		spin_unlock(&op->lock);
171  	}
172  }
173  
174  /* per inode private orangefs info */
175  struct orangefs_inode_s {
176  	struct orangefs_object_kref refn;
177  	char link_target[ORANGEFS_NAME_MAX];
178  	/*
179  	 * Reading/Writing Extended attributes need to acquire the appropriate
180  	 * reader/writer semaphore on the orangefs_inode_s structure.
181  	 */
182  	struct rw_semaphore xattr_sem;
183  
184  	struct inode vfs_inode;
185  	sector_t last_failed_block_index_read;
186  
187  	unsigned long getattr_time;
188  	unsigned long mapping_time;
189  	int attr_valid;
190  	kuid_t attr_uid;
191  	kgid_t attr_gid;
192  	unsigned long bitlock;
193  
194  	DECLARE_HASHTABLE(xattr_cache, 4);
195  };
196  
197  /* per superblock private orangefs info */
198  struct orangefs_sb_info_s {
199  	struct orangefs_khandle root_khandle;
200  	__s32 fs_id;
201  	int id;
202  	int flags;
203  #define ORANGEFS_OPT_INTR	0x01
204  #define ORANGEFS_OPT_LOCAL_LOCK	0x02
205  	char devname[ORANGEFS_MAX_SERVER_ADDR_LEN];
206  	struct super_block *sb;
207  	int mount_pending;
208  	int no_list;
209  	struct list_head list;
210  };
211  
212  struct orangefs_stats {
213  	unsigned long cache_hits;
214  	unsigned long cache_misses;
215  	unsigned long reads;
216  	unsigned long writes;
217  };
218  
219  struct orangefs_cached_xattr {
220  	struct hlist_node node;
221  	char key[ORANGEFS_MAX_XATTR_NAMELEN];
222  	char val[ORANGEFS_MAX_XATTR_VALUELEN];
223  	ssize_t length;
224  	unsigned long timeout;
225  };
226  
227  struct orangefs_write_range {
228  	loff_t pos;
229  	size_t len;
230  	kuid_t uid;
231  	kgid_t gid;
232  };
233  
234  extern struct orangefs_stats orangefs_stats;
235  
236  /*
237   * NOTE: See Documentation/filesystems/porting.rst for information
238   * on implementing FOO_I and properly accessing fs private data
239   */
ORANGEFS_I(struct inode * inode)240  static inline struct orangefs_inode_s *ORANGEFS_I(struct inode *inode)
241  {
242  	return container_of(inode, struct orangefs_inode_s, vfs_inode);
243  }
244  
ORANGEFS_SB(struct super_block * sb)245  static inline struct orangefs_sb_info_s *ORANGEFS_SB(struct super_block *sb)
246  {
247  	return (struct orangefs_sb_info_s *) sb->s_fs_info;
248  }
249  
250  /* ino_t descends from "unsigned long", 8 bytes, 64 bits. */
orangefs_khandle_to_ino(struct orangefs_khandle * khandle)251  static inline ino_t orangefs_khandle_to_ino(struct orangefs_khandle *khandle)
252  {
253  	union {
254  		unsigned char u[8];
255  		__u64 ino;
256  	} ihandle;
257  
258  	ihandle.u[0] = khandle->u[0] ^ khandle->u[4];
259  	ihandle.u[1] = khandle->u[1] ^ khandle->u[5];
260  	ihandle.u[2] = khandle->u[2] ^ khandle->u[6];
261  	ihandle.u[3] = khandle->u[3] ^ khandle->u[7];
262  	ihandle.u[4] = khandle->u[12] ^ khandle->u[8];
263  	ihandle.u[5] = khandle->u[13] ^ khandle->u[9];
264  	ihandle.u[6] = khandle->u[14] ^ khandle->u[10];
265  	ihandle.u[7] = khandle->u[15] ^ khandle->u[11];
266  
267  	return ihandle.ino;
268  }
269  
get_khandle_from_ino(struct inode * inode)270  static inline struct orangefs_khandle *get_khandle_from_ino(struct inode *inode)
271  {
272  	return &(ORANGEFS_I(inode)->refn.khandle);
273  }
274  
is_root_handle(struct inode * inode)275  static inline int is_root_handle(struct inode *inode)
276  {
277  	gossip_debug(GOSSIP_DCACHE_DEBUG,
278  		     "%s: root handle: %pU, this handle: %pU:\n",
279  		     __func__,
280  		     &ORANGEFS_SB(inode->i_sb)->root_khandle,
281  		     get_khandle_from_ino(inode));
282  
283  	if (ORANGEFS_khandle_cmp(&(ORANGEFS_SB(inode->i_sb)->root_khandle),
284  			     get_khandle_from_ino(inode)))
285  		return 0;
286  	else
287  		return 1;
288  }
289  
match_handle(struct orangefs_khandle resp_handle,struct inode * inode)290  static inline int match_handle(struct orangefs_khandle resp_handle,
291  			       struct inode *inode)
292  {
293  	gossip_debug(GOSSIP_DCACHE_DEBUG,
294  		     "%s: one handle: %pU, another handle:%pU:\n",
295  		     __func__,
296  		     &resp_handle,
297  		     get_khandle_from_ino(inode));
298  
299  	if (ORANGEFS_khandle_cmp(&resp_handle, get_khandle_from_ino(inode)))
300  		return 0;
301  	else
302  		return 1;
303  }
304  
305  /*
306   * defined in orangefs-cache.c
307   */
308  int op_cache_initialize(void);
309  int op_cache_finalize(void);
310  struct orangefs_kernel_op_s *op_alloc(__s32 type);
311  void orangefs_new_tag(struct orangefs_kernel_op_s *op);
312  char *get_opname_string(struct orangefs_kernel_op_s *new_op);
313  
314  int orangefs_inode_cache_initialize(void);
315  int orangefs_inode_cache_finalize(void);
316  
317  /*
318   * defined in orangefs-mod.c
319   */
320  void purge_inprogress_ops(void);
321  
322  /*
323   * defined in waitqueue.c
324   */
325  void purge_waiting_ops(void);
326  
327  /*
328   * defined in super.c
329   */
330  extern uint64_t orangefs_features;
331  
332  struct dentry *orangefs_mount(struct file_system_type *fst,
333  			   int flags,
334  			   const char *devname,
335  			   void *data);
336  
337  void orangefs_kill_sb(struct super_block *sb);
338  int orangefs_remount(struct orangefs_sb_info_s *);
339  
340  int fsid_key_table_initialize(void);
341  void fsid_key_table_finalize(void);
342  
343  /*
344   * defined in inode.c
345   */
346  vm_fault_t orangefs_page_mkwrite(struct vm_fault *);
347  struct inode *orangefs_new_inode(struct super_block *sb,
348  			      struct inode *dir,
349  			      umode_t mode,
350  			      dev_t dev,
351  			      struct orangefs_object_kref *ref);
352  
353  int __orangefs_setattr(struct inode *, struct iattr *);
354  int __orangefs_setattr_mode(struct dentry *dentry, struct iattr *iattr);
355  int orangefs_setattr(struct mnt_idmap *, struct dentry *, struct iattr *);
356  
357  int orangefs_getattr(struct mnt_idmap *idmap, const struct path *path,
358  		     struct kstat *stat, u32 request_mask, unsigned int flags);
359  
360  int orangefs_permission(struct mnt_idmap *idmap,
361  			struct inode *inode, int mask);
362  
363  int orangefs_update_time(struct inode *, int);
364  
365  /*
366   * defined in xattr.c
367   */
368  ssize_t orangefs_listxattr(struct dentry *dentry, char *buffer, size_t size);
369  
370  /*
371   * defined in namei.c
372   */
373  struct inode *orangefs_iget(struct super_block *sb,
374  			 struct orangefs_object_kref *ref);
375  
376  /*
377   * defined in devorangefs-req.c
378   */
379  extern uint32_t orangefs_userspace_version;
380  
381  int orangefs_dev_init(void);
382  void orangefs_dev_cleanup(void);
383  int is_daemon_in_service(void);
384  bool __is_daemon_in_service(void);
385  
386  /*
387   * defined in file.c
388   */
389  int orangefs_revalidate_mapping(struct inode *);
390  ssize_t wait_for_direct_io(enum ORANGEFS_io_type, struct inode *, loff_t *,
391      struct iov_iter *, size_t, loff_t, struct orangefs_write_range *, int *,
392      struct file *);
393  ssize_t do_readv_writev(enum ORANGEFS_io_type, struct file *, loff_t *,
394      struct iov_iter *);
395  
396  /*
397   * defined in orangefs-utils.c
398   */
399  __s32 fsid_of_op(struct orangefs_kernel_op_s *op);
400  
401  ssize_t orangefs_inode_getxattr(struct inode *inode,
402  			     const char *name,
403  			     void *buffer,
404  			     size_t size);
405  
406  int orangefs_inode_setxattr(struct inode *inode,
407  			 const char *name,
408  			 const void *value,
409  			 size_t size,
410  			 int flags);
411  
412  #define ORANGEFS_GETATTR_NEW 1
413  #define ORANGEFS_GETATTR_SIZE 2
414  
415  int orangefs_inode_getattr(struct inode *, int);
416  
417  int orangefs_inode_check_changed(struct inode *inode);
418  
419  int orangefs_inode_setattr(struct inode *inode);
420  
421  bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op);
422  
423  int orangefs_normalize_to_errno(__s32 error_code);
424  
425  extern struct mutex orangefs_request_mutex;
426  extern int op_timeout_secs;
427  extern int slot_timeout_secs;
428  extern int orangefs_cache_timeout_msecs;
429  extern int orangefs_dcache_timeout_msecs;
430  extern int orangefs_getattr_timeout_msecs;
431  extern struct list_head orangefs_superblocks;
432  extern spinlock_t orangefs_superblocks_lock;
433  extern struct list_head orangefs_request_list;
434  extern spinlock_t orangefs_request_list_lock;
435  extern wait_queue_head_t orangefs_request_list_waitq;
436  extern struct list_head *orangefs_htable_ops_in_progress;
437  extern spinlock_t orangefs_htable_ops_in_progress_lock;
438  extern int hash_table_size;
439  
440  extern const struct file_operations orangefs_file_operations;
441  extern const struct inode_operations orangefs_symlink_inode_operations;
442  extern const struct inode_operations orangefs_dir_inode_operations;
443  extern const struct file_operations orangefs_dir_operations;
444  extern const struct dentry_operations orangefs_dentry_operations;
445  
446  /*
447   * misc convenience macros
448   */
449  
450  #define ORANGEFS_OP_INTERRUPTIBLE 1   /* service_operation() is interruptible */
451  #define ORANGEFS_OP_PRIORITY      2   /* service_operation() is high priority */
452  #define ORANGEFS_OP_CANCELLATION  4   /* this is a cancellation */
453  #define ORANGEFS_OP_NO_MUTEX      8   /* don't acquire request_mutex */
454  #define ORANGEFS_OP_ASYNC         16  /* Queue it, but don't wait */
455  #define ORANGEFS_OP_WRITEBACK     32
456  
457  int service_operation(struct orangefs_kernel_op_s *op,
458  		      const char *op_name,
459  		      int flags);
460  
461  #define get_interruptible_flag(inode) \
462  	((ORANGEFS_SB(inode->i_sb)->flags & ORANGEFS_OPT_INTR) ? \
463  		ORANGEFS_OP_INTERRUPTIBLE : 0)
464  
465  #define fill_default_sys_attrs(sys_attr, type, mode)			\
466  do {									\
467  	sys_attr.owner = from_kuid(&init_user_ns, current_fsuid()); \
468  	sys_attr.group = from_kgid(&init_user_ns, current_fsgid()); \
469  	sys_attr.perms = ORANGEFS_util_translate_mode(mode);		\
470  	sys_attr.mtime = 0;						\
471  	sys_attr.atime = 0;						\
472  	sys_attr.ctime = 0;						\
473  	sys_attr.mask = ORANGEFS_ATTR_SYS_ALL_SETABLE;			\
474  } while (0)
475  
orangefs_set_timeout(struct dentry * dentry)476  static inline void orangefs_set_timeout(struct dentry *dentry)
477  {
478  	unsigned long time = jiffies + orangefs_dcache_timeout_msecs*HZ/1000;
479  
480  	dentry->d_fsdata = (void *) time;
481  }
482  
483  #endif /* __ORANGEFSKERNEL_H */
484