mds_client.h 14 KB

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