mds_client.h 14 KB

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