mds_client.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _FS_CEPH_MDS_CLIENT_H
  3. #define _FS_CEPH_MDS_CLIENT_H
  4. #include <linux/completion.h>
  5. #include <linux/kref.h>
  6. #include <linux/list.h>
  7. #include <linux/mutex.h>
  8. #include <linux/rbtree.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/refcount.h>
  11. #include <linux/utsname.h>
  12. #include <linux/ceph/types.h>
  13. #include <linux/ceph/messenger.h>
  14. #include <linux/ceph/mdsmap.h>
  15. #include <linux/ceph/auth.h>
  16. /* The first 8 bits are reserved for old ceph releases */
  17. #define CEPHFS_FEATURE_MIMIC 8
  18. #define CEPHFS_FEATURES_ALL { \
  19. 0, 1, 2, 3, 4, 5, 6, 7, \
  20. CEPHFS_FEATURE_MIMIC, \
  21. }
  22. #define CEPHFS_FEATURES_CLIENT_SUPPORTED CEPHFS_FEATURES_ALL
  23. #define CEPHFS_FEATURES_CLIENT_REQUIRED {}
  24. /*
  25. * Some lock dependencies:
  26. *
  27. * session->s_mutex
  28. * mdsc->mutex
  29. *
  30. * mdsc->snap_rwsem
  31. *
  32. * ci->i_ceph_lock
  33. * mdsc->snap_flush_lock
  34. * mdsc->cap_delay_lock
  35. *
  36. */
  37. struct ceph_fs_client;
  38. struct ceph_cap;
  39. /*
  40. * parsed info about a single inode. pointers are into the encoded
  41. * on-wire structures within the mds reply message payload.
  42. */
  43. struct ceph_mds_reply_info_in {
  44. struct ceph_mds_reply_inode *in;
  45. struct ceph_dir_layout dir_layout;
  46. u32 symlink_len;
  47. char *symlink;
  48. u32 xattr_len;
  49. char *xattr_data;
  50. u64 inline_version;
  51. u32 inline_len;
  52. char *inline_data;
  53. u32 pool_ns_len;
  54. char *pool_ns_data;
  55. u64 max_bytes;
  56. u64 max_files;
  57. };
  58. struct ceph_mds_reply_dir_entry {
  59. char *name;
  60. u32 name_len;
  61. struct ceph_mds_reply_lease *lease;
  62. struct ceph_mds_reply_info_in inode;
  63. loff_t offset;
  64. };
  65. /*
  66. * parsed info about an mds reply, including information about
  67. * either: 1) the target inode and/or its parent directory and dentry,
  68. * and directory contents (for readdir results), or
  69. * 2) the file range lock info (for fcntl F_GETLK results).
  70. */
  71. struct ceph_mds_reply_info_parsed {
  72. struct ceph_mds_reply_head *head;
  73. /* trace */
  74. struct ceph_mds_reply_info_in diri, targeti;
  75. struct ceph_mds_reply_dirfrag *dirfrag;
  76. char *dname;
  77. u32 dname_len;
  78. struct ceph_mds_reply_lease *dlease;
  79. /* extra */
  80. union {
  81. /* for fcntl F_GETLK results */
  82. struct ceph_filelock *filelock_reply;
  83. /* for readdir results */
  84. struct {
  85. struct ceph_mds_reply_dirfrag *dir_dir;
  86. size_t dir_buf_size;
  87. int dir_nr;
  88. bool dir_end;
  89. bool dir_complete;
  90. bool hash_order;
  91. bool offset_hash;
  92. struct ceph_mds_reply_dir_entry *dir_entries;
  93. };
  94. /* for create results */
  95. struct {
  96. bool has_create_ino;
  97. u64 ino;
  98. };
  99. };
  100. /* encoded blob describing snapshot contexts for certain
  101. operations (e.g., open) */
  102. void *snapblob;
  103. int snapblob_len;
  104. };
  105. /*
  106. * cap releases are batched and sent to the MDS en masse.
  107. *
  108. * Account for per-message overhead of mds_cap_release header
  109. * and __le32 for osd epoch barrier trailing field.
  110. */
  111. #define CEPH_CAPS_PER_RELEASE ((PAGE_SIZE - sizeof(u32) - \
  112. sizeof(struct ceph_mds_cap_release)) / \
  113. sizeof(struct ceph_mds_cap_item))
  114. /*
  115. * state associated with each MDS<->client session
  116. */
  117. enum {
  118. CEPH_MDS_SESSION_NEW = 1,
  119. CEPH_MDS_SESSION_OPENING = 2,
  120. CEPH_MDS_SESSION_OPEN = 3,
  121. CEPH_MDS_SESSION_HUNG = 4,
  122. CEPH_MDS_SESSION_CLOSING = 5,
  123. CEPH_MDS_SESSION_RESTARTING = 6,
  124. CEPH_MDS_SESSION_RECONNECTING = 7,
  125. CEPH_MDS_SESSION_REJECTED = 8,
  126. };
  127. struct ceph_mds_session {
  128. struct ceph_mds_client *s_mdsc;
  129. int s_mds;
  130. int s_state;
  131. unsigned long s_ttl; /* time until mds kills us */
  132. u64 s_seq; /* incoming msg seq # */
  133. struct mutex s_mutex; /* serialize session messages */
  134. struct ceph_connection s_con;
  135. struct ceph_auth_handshake s_auth;
  136. /* protected by s_gen_ttl_lock */
  137. spinlock_t s_gen_ttl_lock;
  138. u32 s_cap_gen; /* inc each time we get mds stale msg */
  139. unsigned long s_cap_ttl; /* when session caps expire */
  140. /* protected by s_cap_lock */
  141. spinlock_t s_cap_lock;
  142. struct list_head s_caps; /* all caps issued by this session */
  143. int s_nr_caps, s_trim_caps;
  144. int s_num_cap_releases;
  145. int s_cap_reconnect;
  146. int s_readonly;
  147. struct list_head s_cap_releases; /* waiting cap_release messages */
  148. struct ceph_cap *s_cap_iterator;
  149. /* protected by mutex */
  150. struct list_head s_cap_flushing; /* inodes w/ flushing caps */
  151. unsigned long s_renew_requested; /* last time we sent a renew req */
  152. u64 s_renew_seq;
  153. refcount_t s_ref;
  154. struct list_head s_waiting; /* waiting requests */
  155. struct list_head s_unsafe; /* unsafe requests */
  156. };
  157. /*
  158. * modes of choosing which MDS to send a request to
  159. */
  160. enum {
  161. USE_ANY_MDS,
  162. USE_RANDOM_MDS,
  163. USE_AUTH_MDS, /* prefer authoritative mds for this metadata item */
  164. };
  165. struct ceph_mds_request;
  166. struct ceph_mds_client;
  167. /*
  168. * request completion callback
  169. */
  170. typedef void (*ceph_mds_request_callback_t) (struct ceph_mds_client *mdsc,
  171. struct ceph_mds_request *req);
  172. /*
  173. * wait for request completion callback
  174. */
  175. typedef int (*ceph_mds_request_wait_callback_t) (struct ceph_mds_client *mdsc,
  176. struct ceph_mds_request *req);
  177. /*
  178. * an in-flight mds request
  179. */
  180. struct ceph_mds_request {
  181. u64 r_tid; /* transaction id */
  182. struct rb_node r_node;
  183. struct ceph_mds_client *r_mdsc;
  184. int r_op; /* mds op code */
  185. /* operation on what? */
  186. struct inode *r_inode; /* arg1 */
  187. struct dentry *r_dentry; /* arg1 */
  188. struct dentry *r_old_dentry; /* arg2: rename from or link from */
  189. struct inode *r_old_dentry_dir; /* arg2: old dentry's parent dir */
  190. char *r_path1, *r_path2;
  191. struct ceph_vino r_ino1, r_ino2;
  192. struct inode *r_parent; /* parent dir inode */
  193. struct inode *r_target_inode; /* resulting inode */
  194. #define CEPH_MDS_R_DIRECT_IS_HASH (1) /* r_direct_hash is valid */
  195. #define CEPH_MDS_R_ABORTED (2) /* call was aborted */
  196. #define CEPH_MDS_R_GOT_UNSAFE (3) /* got an unsafe reply */
  197. #define CEPH_MDS_R_GOT_SAFE (4) /* got a safe reply */
  198. #define CEPH_MDS_R_GOT_RESULT (5) /* got a result */
  199. #define CEPH_MDS_R_DID_PREPOPULATE (6) /* prepopulated readdir */
  200. #define CEPH_MDS_R_PARENT_LOCKED (7) /* is r_parent->i_rwsem wlocked? */
  201. unsigned long r_req_flags;
  202. struct mutex r_fill_mutex;
  203. union ceph_mds_request_args r_args;
  204. int r_fmode; /* file mode, if expecting cap */
  205. kuid_t r_uid;
  206. kgid_t r_gid;
  207. struct timespec64 r_stamp;
  208. /* for choosing which mds to send this request to */
  209. int r_direct_mode;
  210. u32 r_direct_hash; /* choose dir frag based on this dentry hash */
  211. /* data payload is used for xattr ops */
  212. struct ceph_pagelist *r_pagelist;
  213. /* what caps shall we drop? */
  214. int r_inode_drop, r_inode_unless;
  215. int r_dentry_drop, r_dentry_unless;
  216. int r_old_dentry_drop, r_old_dentry_unless;
  217. struct inode *r_old_inode;
  218. int r_old_inode_drop, r_old_inode_unless;
  219. struct ceph_msg *r_request; /* original request */
  220. int r_request_release_offset;
  221. struct ceph_msg *r_reply;
  222. struct ceph_mds_reply_info_parsed r_reply_info;
  223. struct page *r_locked_page;
  224. int r_err;
  225. unsigned long r_timeout; /* optional. jiffies, 0 is "wait forever" */
  226. unsigned long r_started; /* start time to measure timeout against */
  227. unsigned long r_request_started; /* start time for mds request only,
  228. used to measure lease durations */
  229. /* link unsafe requests to parent directory, for fsync */
  230. struct inode *r_unsafe_dir;
  231. struct list_head r_unsafe_dir_item;
  232. /* unsafe requests that modify the target inode */
  233. struct list_head r_unsafe_target_item;
  234. struct ceph_mds_session *r_session;
  235. int r_attempts; /* resend attempts */
  236. int r_num_fwd; /* number of forward attempts */
  237. int r_resend_mds; /* mds to resend to next, if any*/
  238. u32 r_sent_on_mseq; /* cap mseq request was sent at*/
  239. struct kref r_kref;
  240. struct list_head r_wait;
  241. struct completion r_completion;
  242. struct completion r_safe_completion;
  243. ceph_mds_request_callback_t r_callback;
  244. ceph_mds_request_wait_callback_t r_wait_for_completion;
  245. struct list_head r_unsafe_item; /* per-session unsafe list item */
  246. long long r_dir_release_cnt;
  247. long long r_dir_ordered_cnt;
  248. int r_readdir_cache_idx;
  249. u32 r_readdir_offset;
  250. struct ceph_cap_reservation r_caps_reservation;
  251. int r_num_caps;
  252. };
  253. struct ceph_pool_perm {
  254. struct rb_node node;
  255. int perm;
  256. s64 pool;
  257. size_t pool_ns_len;
  258. char pool_ns[];
  259. };
  260. /*
  261. * mds client state
  262. */
  263. struct ceph_mds_client {
  264. struct ceph_fs_client *fsc;
  265. struct mutex mutex; /* all nested structures */
  266. struct ceph_mdsmap *mdsmap;
  267. struct completion safe_umount_waiters;
  268. wait_queue_head_t session_close_wq;
  269. struct list_head waiting_for_map;
  270. int mdsmap_err;
  271. struct ceph_mds_session **sessions; /* NULL for mds if no session */
  272. atomic_t num_sessions;
  273. int max_sessions; /* len of s_mds_sessions */
  274. int stopping; /* true if shutting down */
  275. atomic64_t quotarealms_count; /* # realms with quota */
  276. /*
  277. * snap_rwsem will cover cap linkage into snaprealms, and
  278. * realm snap contexts. (later, we can do per-realm snap
  279. * contexts locks..) the empty list contains realms with no
  280. * references (implying they contain no inodes with caps) that
  281. * should be destroyed.
  282. */
  283. u64 last_snap_seq;
  284. struct rw_semaphore snap_rwsem;
  285. struct rb_root snap_realms;
  286. struct list_head snap_empty;
  287. spinlock_t snap_empty_lock; /* protect snap_empty */
  288. u64 last_tid; /* most recent mds request */
  289. u64 oldest_tid; /* oldest incomplete mds request,
  290. excluding setfilelock requests */
  291. struct rb_root request_tree; /* pending mds requests */
  292. struct delayed_work delayed_work; /* delayed work */
  293. unsigned long last_renew_caps; /* last time we renewed our caps */
  294. struct list_head cap_delay_list; /* caps with delayed release */
  295. spinlock_t cap_delay_lock; /* protects cap_delay_list */
  296. struct list_head snap_flush_list; /* cap_snaps ready to flush */
  297. spinlock_t snap_flush_lock;
  298. u64 last_cap_flush_tid;
  299. struct list_head cap_flush_list;
  300. struct list_head cap_dirty; /* inodes with dirty caps */
  301. struct list_head cap_dirty_migrating; /* ...that are migration... */
  302. int num_cap_flushing; /* # caps we are flushing */
  303. spinlock_t cap_dirty_lock; /* protects above items */
  304. wait_queue_head_t cap_flushing_wq;
  305. /*
  306. * Cap reservations
  307. *
  308. * Maintain a global pool of preallocated struct ceph_caps, referenced
  309. * by struct ceph_caps_reservations. This ensures that we preallocate
  310. * memory needed to successfully process an MDS response. (If an MDS
  311. * sends us cap information and we fail to process it, we will have
  312. * problems due to the client and MDS being out of sync.)
  313. *
  314. * Reservations are 'owned' by a ceph_cap_reservation context.
  315. */
  316. spinlock_t caps_list_lock;
  317. struct list_head caps_list; /* unused (reserved or
  318. unreserved) */
  319. int caps_total_count; /* total caps allocated */
  320. int caps_use_count; /* in use */
  321. int caps_reserve_count; /* unused, reserved */
  322. int caps_avail_count; /* unused, unreserved */
  323. int caps_min_count; /* keep at least this many
  324. (unreserved) */
  325. spinlock_t dentry_lru_lock;
  326. struct list_head dentry_lru;
  327. int num_dentry;
  328. struct rw_semaphore pool_perm_rwsem;
  329. struct rb_root pool_perm_tree;
  330. char nodename[__NEW_UTS_LEN + 1];
  331. };
  332. extern const char *ceph_mds_op_name(int op);
  333. extern struct ceph_mds_session *
  334. __ceph_lookup_mds_session(struct ceph_mds_client *, int mds);
  335. static inline struct ceph_mds_session *
  336. ceph_get_mds_session(struct ceph_mds_session *s)
  337. {
  338. refcount_inc(&s->s_ref);
  339. return s;
  340. }
  341. extern const char *ceph_session_state_name(int s);
  342. extern void ceph_put_mds_session(struct ceph_mds_session *s);
  343. extern int ceph_send_msg_mds(struct ceph_mds_client *mdsc,
  344. struct ceph_msg *msg, int mds);
  345. extern int ceph_mdsc_init(struct ceph_fs_client *fsc);
  346. extern void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc);
  347. extern void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc);
  348. extern void ceph_mdsc_destroy(struct ceph_fs_client *fsc);
  349. extern void ceph_mdsc_sync(struct ceph_mds_client *mdsc);
  350. extern void ceph_invalidate_dir_request(struct ceph_mds_request *req);
  351. extern int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
  352. struct inode *dir);
  353. extern struct ceph_mds_request *
  354. ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode);
  355. extern void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
  356. struct ceph_mds_request *req);
  357. extern int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
  358. struct inode *dir,
  359. struct ceph_mds_request *req);
  360. static inline void ceph_mdsc_get_request(struct ceph_mds_request *req)
  361. {
  362. kref_get(&req->r_kref);
  363. }
  364. extern void ceph_mdsc_release_request(struct kref *kref);
  365. static inline void ceph_mdsc_put_request(struct ceph_mds_request *req)
  366. {
  367. kref_put(&req->r_kref, ceph_mdsc_release_request);
  368. }
  369. extern void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
  370. struct ceph_mds_session *session);
  371. extern void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc);
  372. extern char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
  373. int stop_on_nosnap);
  374. extern void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry);
  375. extern void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
  376. struct inode *inode,
  377. struct dentry *dentry, char action,
  378. u32 seq);
  379. extern void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc,
  380. struct ceph_msg *msg);
  381. extern void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc,
  382. struct ceph_msg *msg);
  383. extern struct ceph_mds_session *
  384. ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target);
  385. extern void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
  386. struct ceph_mds_session *session);
  387. extern int ceph_trim_caps(struct ceph_mds_client *mdsc,
  388. struct ceph_mds_session *session,
  389. int max_caps);
  390. #endif