1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 23d14c5d2SYehuda Sadeh #ifndef _FS_CEPH_LIBCEPH_H 33d14c5d2SYehuda Sadeh #define _FS_CEPH_LIBCEPH_H 43d14c5d2SYehuda Sadeh 5a1ce3928SDavid Howells #include <linux/ceph/ceph_debug.h> 63d14c5d2SYehuda Sadeh 73d14c5d2SYehuda Sadeh #include <asm/unaligned.h> 83d14c5d2SYehuda Sadeh #include <linux/backing-dev.h> 93d14c5d2SYehuda Sadeh #include <linux/completion.h> 103d14c5d2SYehuda Sadeh #include <linux/exportfs.h> 11187f1882SPaul Gortmaker #include <linux/bug.h> 123d14c5d2SYehuda Sadeh #include <linux/fs.h> 133d14c5d2SYehuda Sadeh #include <linux/mempool.h> 143d14c5d2SYehuda Sadeh #include <linux/pagemap.h> 153d14c5d2SYehuda Sadeh #include <linux/wait.h> 163d14c5d2SYehuda Sadeh #include <linux/writeback.h> 173d14c5d2SYehuda Sadeh #include <linux/slab.h> 1806dfa963SElena Reshetova #include <linux/refcount.h> 193d14c5d2SYehuda Sadeh 20a1ce3928SDavid Howells #include <linux/ceph/types.h> 21a1ce3928SDavid Howells #include <linux/ceph/messenger.h> 22a1ce3928SDavid Howells #include <linux/ceph/msgpool.h> 23a1ce3928SDavid Howells #include <linux/ceph/mon_client.h> 24a1ce3928SDavid Howells #include <linux/ceph/osd_client.h> 25a1ce3928SDavid Howells #include <linux/ceph/ceph_fs.h> 2651e92737SYan, Zheng #include <linux/ceph/string_table.h> 273d14c5d2SYehuda Sadeh 283d14c5d2SYehuda Sadeh /* 293d14c5d2SYehuda Sadeh * mount options 303d14c5d2SYehuda Sadeh */ 313d14c5d2SYehuda Sadeh #define CEPH_OPT_FSID (1<<0) 323d14c5d2SYehuda Sadeh #define CEPH_OPT_NOSHARE (1<<1) /* don't share client with other sbs */ 333d14c5d2SYehuda Sadeh #define CEPH_OPT_MYIP (1<<2) /* specified my ip */ 343d14c5d2SYehuda Sadeh #define CEPH_OPT_NOCRC (1<<3) /* no data crc on writes */ 35a51983e4SIlya Dryomov #define CEPH_OPT_NOMSGAUTH (1<<4) /* don't require msg signing feat */ 36ba988f87SChaitanya Huilgol #define CEPH_OPT_TCP_NODELAY (1<<5) /* TCP_NODELAY on TCP sockets */ 37a51983e4SIlya Dryomov #define CEPH_OPT_NOMSGSIGN (1<<6) /* don't sign msgs */ 3802b2f549SDongsheng Yang #define CEPH_OPT_ABORT_ON_FULL (1<<7) /* abort w/ ENOSPC when full */ 393d14c5d2SYehuda Sadeh 40ba988f87SChaitanya Huilgol #define CEPH_OPT_DEFAULT (CEPH_OPT_TCP_NODELAY) 413d14c5d2SYehuda Sadeh 423d14c5d2SYehuda Sadeh #define ceph_set_opt(client, opt) \ 433d14c5d2SYehuda Sadeh (client)->options->flags |= CEPH_OPT_##opt; 443d14c5d2SYehuda Sadeh #define ceph_test_opt(client, opt) \ 453d14c5d2SYehuda Sadeh (!!((client)->options->flags & CEPH_OPT_##opt)) 463d14c5d2SYehuda Sadeh 473d14c5d2SYehuda Sadeh struct ceph_options { 483d14c5d2SYehuda Sadeh int flags; 493d14c5d2SYehuda Sadeh struct ceph_fsid fsid; 503d14c5d2SYehuda Sadeh struct ceph_entity_addr my_addr; 51a319bf56SIlya Dryomov unsigned long mount_timeout; /* jiffies */ 52a319bf56SIlya Dryomov unsigned long osd_idle_ttl; /* jiffies */ 53a319bf56SIlya Dryomov unsigned long osd_keepalive_timeout; /* jiffies */ 547cc5e38fSIlya Dryomov unsigned long osd_request_timeout; /* jiffies */ 553d14c5d2SYehuda Sadeh 563d14c5d2SYehuda Sadeh /* 5702b2f549SDongsheng Yang * any type that can't be simply compared or doesn't need 583d14c5d2SYehuda Sadeh * to be compared should go beyond this point, 593d14c5d2SYehuda Sadeh * ceph_compare_options() should be updated accordingly 603d14c5d2SYehuda Sadeh */ 613d14c5d2SYehuda Sadeh 623d14c5d2SYehuda Sadeh struct ceph_entity_addr *mon_addr; /* should be the first 633d14c5d2SYehuda Sadeh pointer type of args */ 643d14c5d2SYehuda Sadeh int num_mon; 653d14c5d2SYehuda Sadeh char *name; 668323c3aaSTommi Virtanen struct ceph_crypto_key *key; 673d14c5d2SYehuda Sadeh }; 683d14c5d2SYehuda Sadeh 693d14c5d2SYehuda Sadeh /* 703d14c5d2SYehuda Sadeh * defaults 713d14c5d2SYehuda Sadeh */ 72a319bf56SIlya Dryomov #define CEPH_MOUNT_TIMEOUT_DEFAULT msecs_to_jiffies(60 * 1000) 73a319bf56SIlya Dryomov #define CEPH_OSD_KEEPALIVE_DEFAULT msecs_to_jiffies(5 * 1000) 74a319bf56SIlya Dryomov #define CEPH_OSD_IDLE_TTL_DEFAULT msecs_to_jiffies(60 * 1000) 757cc5e38fSIlya Dryomov #define CEPH_OSD_REQUEST_TIMEOUT_DEFAULT 0 /* no timeout */ 7658d81b12SIlya Dryomov 77168b9090SIlya Dryomov #define CEPH_MONC_HUNT_INTERVAL msecs_to_jiffies(3 * 1000) 7858d81b12SIlya Dryomov #define CEPH_MONC_PING_INTERVAL msecs_to_jiffies(10 * 1000) 7958d81b12SIlya Dryomov #define CEPH_MONC_PING_TIMEOUT msecs_to_jiffies(30 * 1000) 80168b9090SIlya Dryomov #define CEPH_MONC_HUNT_BACKOFF 2 81168b9090SIlya Dryomov #define CEPH_MONC_HUNT_MAX_MULT 10 823d14c5d2SYehuda Sadeh 833d14c5d2SYehuda Sadeh #define CEPH_MSG_MAX_FRONT_LEN (16*1024*1024) 847b11ba37SAlex Elder #define CEPH_MSG_MAX_MIDDLE_LEN (16*1024*1024) 8594e6992bSIlya Dryomov 8694e6992bSIlya Dryomov /* 8794e6992bSIlya Dryomov * Handle the largest possible rbd object in one message. 8894e6992bSIlya Dryomov * There is no limit on the size of cephfs objects, but it has to obey 8994e6992bSIlya Dryomov * rsize and wsize mount options anyway. 9094e6992bSIlya Dryomov */ 9194e6992bSIlya Dryomov #define CEPH_MSG_MAX_DATA_LEN (32*1024*1024) 923d14c5d2SYehuda Sadeh 933d14c5d2SYehuda Sadeh #define CEPH_AUTH_NAME_DEFAULT "guest" 943d14c5d2SYehuda Sadeh 953d14c5d2SYehuda Sadeh /* mount state */ 963d14c5d2SYehuda Sadeh enum { 973d14c5d2SYehuda Sadeh CEPH_MOUNT_MOUNTING, 983d14c5d2SYehuda Sadeh CEPH_MOUNT_MOUNTED, 993d14c5d2SYehuda Sadeh CEPH_MOUNT_UNMOUNTING, 1003d14c5d2SYehuda Sadeh CEPH_MOUNT_UNMOUNTED, 1013d14c5d2SYehuda Sadeh CEPH_MOUNT_SHUTDOWN, 1023d14c5d2SYehuda Sadeh }; 1033d14c5d2SYehuda Sadeh 104a319bf56SIlya Dryomov static inline unsigned long ceph_timeout_jiffies(unsigned long timeout) 105a319bf56SIlya Dryomov { 106a319bf56SIlya Dryomov return timeout ?: MAX_SCHEDULE_TIMEOUT; 107a319bf56SIlya Dryomov } 108a319bf56SIlya Dryomov 1093d14c5d2SYehuda Sadeh struct ceph_mds_client; 1103d14c5d2SYehuda Sadeh 1113d14c5d2SYehuda Sadeh /* 1123d14c5d2SYehuda Sadeh * per client state 1133d14c5d2SYehuda Sadeh * 1143d14c5d2SYehuda Sadeh * possibly shared by multiple mount points, if they are 1153d14c5d2SYehuda Sadeh * mounting the same ceph filesystem/cluster. 1163d14c5d2SYehuda Sadeh */ 1173d14c5d2SYehuda Sadeh struct ceph_client { 1183d14c5d2SYehuda Sadeh struct ceph_fsid fsid; 1193d14c5d2SYehuda Sadeh bool have_fsid; 1203d14c5d2SYehuda Sadeh 1213d14c5d2SYehuda Sadeh void *private; 1223d14c5d2SYehuda Sadeh 1233d14c5d2SYehuda Sadeh struct ceph_options *options; 1243d14c5d2SYehuda Sadeh 1253d14c5d2SYehuda Sadeh struct mutex mount_mutex; /* serialize mount attempts */ 1263d14c5d2SYehuda Sadeh wait_queue_head_t auth_wq; 1273d14c5d2SYehuda Sadeh int auth_err; 1283d14c5d2SYehuda Sadeh 1293d14c5d2SYehuda Sadeh int (*extra_mon_dispatch)(struct ceph_client *, struct ceph_msg *); 1303d14c5d2SYehuda Sadeh 13112b4629aSIlya Dryomov u64 supported_features; 13212b4629aSIlya Dryomov u64 required_features; 1333d14c5d2SYehuda Sadeh 13415d9882cSAlex Elder struct ceph_messenger msgr; /* messenger instance */ 1353d14c5d2SYehuda Sadeh struct ceph_mon_client monc; 1363d14c5d2SYehuda Sadeh struct ceph_osd_client osdc; 1373d14c5d2SYehuda Sadeh 1383d14c5d2SYehuda Sadeh #ifdef CONFIG_DEBUG_FS 1393d14c5d2SYehuda Sadeh struct dentry *debugfs_dir; 1403d14c5d2SYehuda Sadeh struct dentry *debugfs_monmap; 1413d14c5d2SYehuda Sadeh struct dentry *debugfs_osdmap; 1425cf7bd30SIlya Dryomov struct dentry *debugfs_options; 1433d14c5d2SYehuda Sadeh #endif 1443d14c5d2SYehuda Sadeh }; 1453d14c5d2SYehuda Sadeh 146859bff51SIlya Dryomov #define from_msgr(ms) container_of(ms, struct ceph_client, msgr) 1473d14c5d2SYehuda Sadeh 1483d14c5d2SYehuda Sadeh 1493d14c5d2SYehuda Sadeh /* 1503d14c5d2SYehuda Sadeh * snapshots 1513d14c5d2SYehuda Sadeh */ 1523d14c5d2SYehuda Sadeh 1533d14c5d2SYehuda Sadeh /* 1543d14c5d2SYehuda Sadeh * A "snap context" is the set of existing snapshots when we 1553d14c5d2SYehuda Sadeh * write data. It is used by the OSD to guide its COW behavior. 1563d14c5d2SYehuda Sadeh * 1573d14c5d2SYehuda Sadeh * The ceph_snap_context is refcounted, and attached to each dirty 1583d14c5d2SYehuda Sadeh * page, indicating which context the dirty data belonged when it was 1593d14c5d2SYehuda Sadeh * dirtied. 1603d14c5d2SYehuda Sadeh */ 1613d14c5d2SYehuda Sadeh struct ceph_snap_context { 16206dfa963SElena Reshetova refcount_t nref; 1633d14c5d2SYehuda Sadeh u64 seq; 164aa711ee3SAlex Elder u32 num_snaps; 1653d14c5d2SYehuda Sadeh u64 snaps[]; 1663d14c5d2SYehuda Sadeh }; 1673d14c5d2SYehuda Sadeh 1684f0dcb10SAlex Elder extern struct ceph_snap_context *ceph_create_snap_context(u32 snap_count, 1694f0dcb10SAlex Elder gfp_t gfp_flags); 1704f0dcb10SAlex Elder extern struct ceph_snap_context *ceph_get_snap_context( 1714f0dcb10SAlex Elder struct ceph_snap_context *sc); 1724f0dcb10SAlex Elder extern void ceph_put_snap_context(struct ceph_snap_context *sc); 1733d14c5d2SYehuda Sadeh 1743d14c5d2SYehuda Sadeh /* 1753d14c5d2SYehuda Sadeh * calculate the number of pages a given length and offset map onto, 1763d14c5d2SYehuda Sadeh * if we align the data. 1773d14c5d2SYehuda Sadeh */ 1783d14c5d2SYehuda Sadeh static inline int calc_pages_for(u64 off, u64 len) 1793d14c5d2SYehuda Sadeh { 18009cbfeafSKirill A. Shutemov return ((off+len+PAGE_SIZE-1) >> PAGE_SHIFT) - 18109cbfeafSKirill A. Shutemov (off >> PAGE_SHIFT); 1823d14c5d2SYehuda Sadeh } 1833d14c5d2SYehuda Sadeh 18476f827a7SIlya Dryomov #define RB_BYVAL(a) (a) 18576f827a7SIlya Dryomov #define RB_BYPTR(a) (&(a)) 18676f827a7SIlya Dryomov #define RB_CMP3WAY(a, b) ((a) < (b) ? -1 : (a) > (b)) 18776f827a7SIlya Dryomov 18876f827a7SIlya Dryomov #define DEFINE_RB_INSDEL_FUNCS2(name, type, keyfld, cmpexp, keyexp, nodefld) \ 189fcd00b68SIlya Dryomov static void insert_##name(struct rb_root *root, type *t) \ 190fcd00b68SIlya Dryomov { \ 191fcd00b68SIlya Dryomov struct rb_node **n = &root->rb_node; \ 192fcd00b68SIlya Dryomov struct rb_node *parent = NULL; \ 193fcd00b68SIlya Dryomov \ 194fcd00b68SIlya Dryomov BUG_ON(!RB_EMPTY_NODE(&t->nodefld)); \ 195fcd00b68SIlya Dryomov \ 196fcd00b68SIlya Dryomov while (*n) { \ 197fcd00b68SIlya Dryomov type *cur = rb_entry(*n, type, nodefld); \ 19876f827a7SIlya Dryomov int cmp; \ 199fcd00b68SIlya Dryomov \ 200fcd00b68SIlya Dryomov parent = *n; \ 20176f827a7SIlya Dryomov cmp = cmpexp(keyexp(t->keyfld), keyexp(cur->keyfld)); \ 20276f827a7SIlya Dryomov if (cmp < 0) \ 203fcd00b68SIlya Dryomov n = &(*n)->rb_left; \ 20476f827a7SIlya Dryomov else if (cmp > 0) \ 205fcd00b68SIlya Dryomov n = &(*n)->rb_right; \ 206fcd00b68SIlya Dryomov else \ 207fcd00b68SIlya Dryomov BUG(); \ 208fcd00b68SIlya Dryomov } \ 209fcd00b68SIlya Dryomov \ 210fcd00b68SIlya Dryomov rb_link_node(&t->nodefld, parent, n); \ 211fcd00b68SIlya Dryomov rb_insert_color(&t->nodefld, root); \ 212fcd00b68SIlya Dryomov } \ 213fcd00b68SIlya Dryomov static void erase_##name(struct rb_root *root, type *t) \ 214fcd00b68SIlya Dryomov { \ 215fcd00b68SIlya Dryomov BUG_ON(RB_EMPTY_NODE(&t->nodefld)); \ 216fcd00b68SIlya Dryomov rb_erase(&t->nodefld, root); \ 217fcd00b68SIlya Dryomov RB_CLEAR_NODE(&t->nodefld); \ 218fcd00b68SIlya Dryomov } 219fcd00b68SIlya Dryomov 22076f827a7SIlya Dryomov /* 22176f827a7SIlya Dryomov * @lookup_param_type is a parameter and not constructed from (@type, 22276f827a7SIlya Dryomov * @keyfld) with typeof() because adding const is too unwieldy. 22376f827a7SIlya Dryomov */ 22476f827a7SIlya Dryomov #define DEFINE_RB_LOOKUP_FUNC2(name, type, keyfld, cmpexp, keyexp, \ 22576f827a7SIlya Dryomov lookup_param_type, nodefld) \ 22676f827a7SIlya Dryomov static type *lookup_##name(struct rb_root *root, lookup_param_type key) \ 227fcd00b68SIlya Dryomov { \ 228fcd00b68SIlya Dryomov struct rb_node *n = root->rb_node; \ 229fcd00b68SIlya Dryomov \ 230fcd00b68SIlya Dryomov while (n) { \ 231fcd00b68SIlya Dryomov type *cur = rb_entry(n, type, nodefld); \ 23276f827a7SIlya Dryomov int cmp; \ 233fcd00b68SIlya Dryomov \ 23476f827a7SIlya Dryomov cmp = cmpexp(key, keyexp(cur->keyfld)); \ 23576f827a7SIlya Dryomov if (cmp < 0) \ 236fcd00b68SIlya Dryomov n = n->rb_left; \ 23776f827a7SIlya Dryomov else if (cmp > 0) \ 238fcd00b68SIlya Dryomov n = n->rb_right; \ 239fcd00b68SIlya Dryomov else \ 240fcd00b68SIlya Dryomov return cur; \ 241fcd00b68SIlya Dryomov } \ 242fcd00b68SIlya Dryomov \ 243fcd00b68SIlya Dryomov return NULL; \ 244fcd00b68SIlya Dryomov } 245fcd00b68SIlya Dryomov 24676f827a7SIlya Dryomov #define DEFINE_RB_FUNCS2(name, type, keyfld, cmpexp, keyexp, \ 24776f827a7SIlya Dryomov lookup_param_type, nodefld) \ 24876f827a7SIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(name, type, keyfld, cmpexp, keyexp, nodefld) \ 24976f827a7SIlya Dryomov DEFINE_RB_LOOKUP_FUNC2(name, type, keyfld, cmpexp, keyexp, \ 25076f827a7SIlya Dryomov lookup_param_type, nodefld) 25176f827a7SIlya Dryomov 25276f827a7SIlya Dryomov /* 25376f827a7SIlya Dryomov * Shorthands for integer keys. 25476f827a7SIlya Dryomov */ 25576f827a7SIlya Dryomov #define DEFINE_RB_INSDEL_FUNCS(name, type, keyfld, nodefld) \ 25676f827a7SIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(name, type, keyfld, RB_CMP3WAY, RB_BYVAL, nodefld) 25776f827a7SIlya Dryomov 25876f827a7SIlya Dryomov #define DEFINE_RB_LOOKUP_FUNC(name, type, keyfld, nodefld) \ 25976f827a7SIlya Dryomov extern type __lookup_##name##_key; \ 26076f827a7SIlya Dryomov DEFINE_RB_LOOKUP_FUNC2(name, type, keyfld, RB_CMP3WAY, RB_BYVAL, \ 26176f827a7SIlya Dryomov typeof(__lookup_##name##_key.keyfld), nodefld) 26276f827a7SIlya Dryomov 263fcd00b68SIlya Dryomov #define DEFINE_RB_FUNCS(name, type, keyfld, nodefld) \ 264fcd00b68SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(name, type, keyfld, nodefld) \ 265fcd00b68SIlya Dryomov DEFINE_RB_LOOKUP_FUNC(name, type, keyfld, nodefld) 266fcd00b68SIlya Dryomov 267eeb0bed5SIlya Dryomov extern struct kmem_cache *ceph_inode_cachep; 268eeb0bed5SIlya Dryomov extern struct kmem_cache *ceph_cap_cachep; 269f66fd9f0SYan, Zheng extern struct kmem_cache *ceph_cap_flush_cachep; 270eeb0bed5SIlya Dryomov extern struct kmem_cache *ceph_dentry_cachep; 271eeb0bed5SIlya Dryomov extern struct kmem_cache *ceph_file_cachep; 272bb48bd4dSChengguang Xu extern struct kmem_cache *ceph_dir_file_cachep; 273eeb0bed5SIlya Dryomov 2743d14c5d2SYehuda Sadeh /* ceph_common.c */ 27572fe25e3SAlex Elder extern bool libceph_compatible(void *data); 27672fe25e3SAlex Elder 2773d14c5d2SYehuda Sadeh extern const char *ceph_msg_type_name(int type); 2783d14c5d2SYehuda Sadeh extern int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid); 279eeb0bed5SIlya Dryomov extern void *ceph_kvmalloc(size_t size, gfp_t flags); 2803d14c5d2SYehuda Sadeh 281ee57741cSAlex Elder extern struct ceph_options *ceph_parse_options(char *options, 2823d14c5d2SYehuda Sadeh const char *dev_name, const char *dev_name_end, 2833d14c5d2SYehuda Sadeh int (*parse_extra_token)(char *c, void *private), 2843d14c5d2SYehuda Sadeh void *private); 28502b2f549SDongsheng Yang int ceph_print_client_options(struct seq_file *m, struct ceph_client *client, 28602b2f549SDongsheng Yang bool show_all); 2873d14c5d2SYehuda Sadeh extern void ceph_destroy_options(struct ceph_options *opt); 2883d14c5d2SYehuda Sadeh extern int ceph_compare_options(struct ceph_options *new_opt, 2893d14c5d2SYehuda Sadeh struct ceph_client *client); 29074da4a0fSIlya Dryomov struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private); 291005a07bfSIlya Dryomov struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client); 292033268a5SIlya Dryomov u64 ceph_client_gid(struct ceph_client *client); 2933d14c5d2SYehuda Sadeh extern void ceph_destroy_client(struct ceph_client *client); 2943d14c5d2SYehuda Sadeh extern int __ceph_open_session(struct ceph_client *client, 2953d14c5d2SYehuda Sadeh unsigned long started); 2963d14c5d2SYehuda Sadeh extern int ceph_open_session(struct ceph_client *client); 297*bb229bbbSIlya Dryomov int ceph_wait_for_latest_osdmap(struct ceph_client *client, 298*bb229bbbSIlya Dryomov unsigned long timeout); 2993d14c5d2SYehuda Sadeh 3003d14c5d2SYehuda Sadeh /* pagevec.c */ 3013d14c5d2SYehuda Sadeh extern void ceph_release_page_vector(struct page **pages, int num_pages); 3023d14c5d2SYehuda Sadeh 303b324814eSAlex Elder extern struct page **ceph_get_direct_page_vector(const void __user *data, 304b6aa5901SHenry C Chang int num_pages, 305b6aa5901SHenry C Chang bool write_page); 306b6aa5901SHenry C Chang extern void ceph_put_page_vector(struct page **pages, int num_pages, 307b6aa5901SHenry C Chang bool dirty); 3083d14c5d2SYehuda Sadeh extern struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags); 3093d14c5d2SYehuda Sadeh extern int ceph_copy_user_to_page_vector(struct page **pages, 310b324814eSAlex Elder const void __user *data, 3113d14c5d2SYehuda Sadeh loff_t off, size_t len); 312903bb32eSAlex Elder extern void ceph_copy_to_page_vector(struct page **pages, 313b324814eSAlex Elder const void *data, 3143d14c5d2SYehuda Sadeh loff_t off, size_t len); 315903bb32eSAlex Elder extern void ceph_copy_from_page_vector(struct page **pages, 316b324814eSAlex Elder void *data, 3173d14c5d2SYehuda Sadeh loff_t off, size_t len); 3183d14c5d2SYehuda Sadeh extern void ceph_zero_page_vector_range(int off, int len, struct page **pages); 3193d14c5d2SYehuda Sadeh 3203d14c5d2SYehuda Sadeh 3213d14c5d2SYehuda Sadeh #endif /* _FS_CEPH_SUPER_H */ 322