orangefs-kernel.h 13 KB

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