mds_client.h 12 KB

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