mds_client.h 13 KB

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