1c958510f00f59eca3497e69e75ad01c269ef907
[releases.git] / mds_client.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _FS_CEPH_MDS_CLIENT_H
3 #define _FS_CEPH_MDS_CLIENT_H
4
5 #include <linux/completion.h>
6 #include <linux/kref.h>
7 #include <linux/list.h>
8 #include <linux/mutex.h>
9 #include <linux/rbtree.h>
10 #include <linux/spinlock.h>
11 #include <linux/refcount.h>
12 #include <linux/utsname.h>
13 #include <linux/ktime.h>
14
15 #include <linux/ceph/types.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/mdsmap.h>
18 #include <linux/ceph/auth.h>
19
20 #include "metric.h"
21 #include "super.h"
22
23 /* The first 8 bits are reserved for old ceph releases */
24 enum ceph_feature_type {
25         CEPHFS_FEATURE_MIMIC = 8,
26         CEPHFS_FEATURE_REPLY_ENCODING,
27         CEPHFS_FEATURE_RECLAIM_CLIENT,
28         CEPHFS_FEATURE_LAZY_CAP_WANTED,
29         CEPHFS_FEATURE_MULTI_RECONNECT,
30         CEPHFS_FEATURE_DELEG_INO,
31         CEPHFS_FEATURE_METRIC_COLLECT,
32
33         CEPHFS_FEATURE_MAX = CEPHFS_FEATURE_METRIC_COLLECT,
34 };
35
36 #define CEPHFS_FEATURES_CLIENT_SUPPORTED {      \
37         0, 1, 2, 3, 4, 5, 6, 7,                 \
38         CEPHFS_FEATURE_MIMIC,                   \
39         CEPHFS_FEATURE_REPLY_ENCODING,          \
40         CEPHFS_FEATURE_LAZY_CAP_WANTED,         \
41         CEPHFS_FEATURE_MULTI_RECONNECT,         \
42         CEPHFS_FEATURE_DELEG_INO,               \
43         CEPHFS_FEATURE_METRIC_COLLECT,          \
44 }
45 #define CEPHFS_FEATURES_CLIENT_REQUIRED {}
46
47 /*
48  * Some lock dependencies:
49  *
50  * session->s_mutex
51  *         mdsc->mutex
52  *
53  *         mdsc->snap_rwsem
54  *
55  *         ci->i_ceph_lock
56  *                 mdsc->snap_flush_lock
57  *                 mdsc->cap_delay_lock
58  *
59  */
60
61 struct ceph_fs_client;
62 struct ceph_cap;
63
64 /*
65  * parsed info about a single inode.  pointers are into the encoded
66  * on-wire structures within the mds reply message payload.
67  */
68 struct ceph_mds_reply_info_in {
69         struct ceph_mds_reply_inode *in;
70         struct ceph_dir_layout dir_layout;
71         u32 symlink_len;
72         char *symlink;
73         u32 xattr_len;
74         char *xattr_data;
75         u64 inline_version;
76         u32 inline_len;
77         char *inline_data;
78         u32 pool_ns_len;
79         char *pool_ns_data;
80         u64 max_bytes;
81         u64 max_files;
82         s32 dir_pin;
83         struct ceph_timespec btime;
84         struct ceph_timespec snap_btime;
85         u64 change_attr;
86 };
87
88 struct ceph_mds_reply_dir_entry {
89         char                          *name;
90         u32                           name_len;
91         struct ceph_mds_reply_lease   *lease;
92         struct ceph_mds_reply_info_in inode;
93         loff_t                        offset;
94 };
95
96 /*
97  * parsed info about an mds reply, including information about
98  * either: 1) the target inode and/or its parent directory and dentry,
99  * and directory contents (for readdir results), or
100  * 2) the file range lock info (for fcntl F_GETLK results).
101  */
102 struct ceph_mds_reply_info_parsed {
103         struct ceph_mds_reply_head    *head;
104
105         /* trace */
106         struct ceph_mds_reply_info_in diri, targeti;
107         struct ceph_mds_reply_dirfrag *dirfrag;
108         char                          *dname;
109         u32                           dname_len;
110         struct ceph_mds_reply_lease   *dlease;
111
112         /* extra */
113         union {
114                 /* for fcntl F_GETLK results */
115                 struct ceph_filelock *filelock_reply;
116
117                 /* for readdir results */
118                 struct {
119                         struct ceph_mds_reply_dirfrag *dir_dir;
120                         size_t                        dir_buf_size;
121                         int                           dir_nr;
122                         bool                          dir_end;
123                         bool                          dir_complete;
124                         bool                          hash_order;
125                         bool                          offset_hash;
126                         struct ceph_mds_reply_dir_entry  *dir_entries;
127                 };
128
129                 /* for create results */
130                 struct {
131                         bool has_create_ino;
132                         u64 ino;
133                 };
134         };
135
136         /* encoded blob describing snapshot contexts for certain
137            operations (e.g., open) */
138         void *snapblob;
139         int snapblob_len;
140 };
141
142
143 /*
144  * cap releases are batched and sent to the MDS en masse.
145  *
146  * Account for per-message overhead of mds_cap_release header
147  * and __le32 for osd epoch barrier trailing field.
148  */
149 #define CEPH_CAPS_PER_RELEASE ((PAGE_SIZE - sizeof(u32) -               \
150                                 sizeof(struct ceph_mds_cap_release)) /  \
151                                 sizeof(struct ceph_mds_cap_item))
152
153
154 /*
155  * state associated with each MDS<->client session
156  */
157 enum {
158         CEPH_MDS_SESSION_NEW = 1,
159         CEPH_MDS_SESSION_OPENING = 2,
160         CEPH_MDS_SESSION_OPEN = 3,
161         CEPH_MDS_SESSION_HUNG = 4,
162         CEPH_MDS_SESSION_RESTARTING = 5,
163         CEPH_MDS_SESSION_RECONNECTING = 6,
164         CEPH_MDS_SESSION_CLOSING = 7,
165         CEPH_MDS_SESSION_CLOSED = 8,
166         CEPH_MDS_SESSION_REJECTED = 9,
167 };
168
169 struct ceph_mds_session {
170         struct ceph_mds_client *s_mdsc;
171         int               s_mds;
172         int               s_state;
173         unsigned long     s_ttl;      /* time until mds kills us */
174         unsigned long     s_features;
175         u64               s_seq;      /* incoming msg seq # */
176         struct mutex      s_mutex;    /* serialize session messages */
177
178         struct ceph_connection s_con;
179
180         struct ceph_auth_handshake s_auth;
181
182         /* protected by s_gen_ttl_lock */
183         spinlock_t        s_gen_ttl_lock;
184         u32               s_cap_gen;  /* inc each time we get mds stale msg */
185         unsigned long     s_cap_ttl;  /* when session caps expire */
186
187         /* protected by s_cap_lock */
188         spinlock_t        s_cap_lock;
189         refcount_t        s_ref;
190         struct list_head  s_caps;     /* all caps issued by this session */
191         struct ceph_cap  *s_cap_iterator;
192         int               s_nr_caps;
193         int               s_num_cap_releases;
194         int               s_cap_reconnect;
195         int               s_readonly;
196         struct list_head  s_cap_releases; /* waiting cap_release messages */
197         struct work_struct s_cap_release_work;
198
199         /* See ceph_inode_info->i_dirty_item. */
200         struct list_head  s_cap_dirty;        /* inodes w/ dirty caps */
201
202         /* See ceph_inode_info->i_flushing_item. */
203         struct list_head  s_cap_flushing;     /* inodes w/ flushing caps */
204
205         unsigned long     s_renew_requested; /* last time we sent a renew req */
206         u64               s_renew_seq;
207
208         struct list_head  s_waiting;  /* waiting requests */
209         struct list_head  s_unsafe;   /* unsafe requests */
210         struct xarray     s_delegated_inos;
211 };
212
213 /*
214  * modes of choosing which MDS to send a request to
215  */
216 enum {
217         USE_ANY_MDS,
218         USE_RANDOM_MDS,
219         USE_AUTH_MDS,   /* prefer authoritative mds for this metadata item */
220 };
221
222 struct ceph_mds_request;
223 struct ceph_mds_client;
224
225 /*
226  * request completion callback
227  */
228 typedef void (*ceph_mds_request_callback_t) (struct ceph_mds_client *mdsc,
229                                              struct ceph_mds_request *req);
230 /*
231  * wait for request completion callback
232  */
233 typedef int (*ceph_mds_request_wait_callback_t) (struct ceph_mds_client *mdsc,
234                                                  struct ceph_mds_request *req);
235
236 /*
237  * an in-flight mds request
238  */
239 struct ceph_mds_request {
240         u64 r_tid;                   /* transaction id */
241         struct rb_node r_node;
242         struct ceph_mds_client *r_mdsc;
243
244         struct kref       r_kref;
245         int r_op;                    /* mds op code */
246
247         /* operation on what? */
248         struct inode *r_inode;              /* arg1 */
249         struct dentry *r_dentry;            /* arg1 */
250         struct dentry *r_old_dentry;        /* arg2: rename from or link from */
251         struct inode *r_old_dentry_dir;     /* arg2: old dentry's parent dir */
252         char *r_path1, *r_path2;
253         struct ceph_vino r_ino1, r_ino2;
254
255         struct inode *r_parent;             /* parent dir inode */
256         struct inode *r_target_inode;       /* resulting inode */
257
258 #define CEPH_MDS_R_DIRECT_IS_HASH       (1) /* r_direct_hash is valid */
259 #define CEPH_MDS_R_ABORTED              (2) /* call was aborted */
260 #define CEPH_MDS_R_GOT_UNSAFE           (3) /* got an unsafe reply */
261 #define CEPH_MDS_R_GOT_SAFE             (4) /* got a safe reply */
262 #define CEPH_MDS_R_GOT_RESULT           (5) /* got a result */
263 #define CEPH_MDS_R_DID_PREPOPULATE      (6) /* prepopulated readdir */
264 #define CEPH_MDS_R_PARENT_LOCKED        (7) /* is r_parent->i_rwsem wlocked? */
265 #define CEPH_MDS_R_ASYNC                (8) /* async request */
266         unsigned long   r_req_flags;
267
268         struct mutex r_fill_mutex;
269
270         union ceph_mds_request_args r_args;
271         int r_fmode;        /* file mode, if expecting cap */
272         kuid_t r_uid;
273         kgid_t r_gid;
274         int r_request_release_offset;
275         struct timespec64 r_stamp;
276
277         /* for choosing which mds to send this request to */
278         int r_direct_mode;
279         u32 r_direct_hash;      /* choose dir frag based on this dentry hash */
280
281         /* data payload is used for xattr ops */
282         struct ceph_pagelist *r_pagelist;
283
284         /* what caps shall we drop? */
285         int r_inode_drop, r_inode_unless;
286         int r_dentry_drop, r_dentry_unless;
287         int r_old_dentry_drop, r_old_dentry_unless;
288         struct inode *r_old_inode;
289         int r_old_inode_drop, r_old_inode_unless;
290
291         struct ceph_msg  *r_request;  /* original request */
292         struct ceph_msg  *r_reply;
293         struct ceph_mds_reply_info_parsed r_reply_info;
294         int r_err;
295
296
297         struct page *r_locked_page;
298         int r_dir_caps;
299         int r_num_caps;
300         u32               r_readdir_offset;
301
302         unsigned long r_timeout;  /* optional.  jiffies, 0 is "wait forever" */
303         unsigned long r_started;  /* start time to measure timeout against */
304         unsigned long r_start_latency;  /* start time to measure latency */
305         unsigned long r_end_latency;    /* finish time to measure latency */
306         unsigned long r_request_started; /* start time for mds request only,
307                                             used to measure lease durations */
308
309         /* link unsafe requests to parent directory, for fsync */
310         struct inode    *r_unsafe_dir;
311         struct list_head r_unsafe_dir_item;
312
313         /* unsafe requests that modify the target inode */
314         struct list_head r_unsafe_target_item;
315
316         struct ceph_mds_session *r_session;
317
318         int               r_attempts;   /* resend attempts */
319         int               r_num_fwd;    /* number of forward attempts */
320         int               r_resend_mds; /* mds to resend to next, if any*/
321         u32               r_sent_on_mseq; /* cap mseq request was sent at*/
322         u64               r_deleg_ino;
323
324         struct list_head  r_wait;
325         struct completion r_completion;
326         struct completion r_safe_completion;
327         ceph_mds_request_callback_t r_callback;
328         ceph_mds_request_wait_callback_t r_wait_for_completion;
329         struct list_head  r_unsafe_item;  /* per-session unsafe list item */
330
331         long long         r_dir_release_cnt;
332         long long         r_dir_ordered_cnt;
333         int               r_readdir_cache_idx;
334
335         struct ceph_cap_reservation r_caps_reservation;
336 };
337
338 struct ceph_pool_perm {
339         struct rb_node node;
340         int perm;
341         s64 pool;
342         size_t pool_ns_len;
343         char pool_ns[];
344 };
345
346 struct ceph_snapid_map {
347         struct rb_node node;
348         struct list_head lru;
349         atomic_t ref;
350         u64 snap;
351         dev_t dev;
352         unsigned long last_used;
353 };
354
355 /*
356  * node for list of quotarealm inodes that are not visible from the filesystem
357  * mountpoint, but required to handle, e.g. quotas.
358  */
359 struct ceph_quotarealm_inode {
360         struct rb_node node;
361         u64 ino;
362         unsigned long timeout; /* last time a lookup failed for this inode */
363         struct mutex mutex;
364         struct inode *inode;
365 };
366
367 struct cap_wait {
368         struct list_head        list;
369         u64                     ino;
370         pid_t                   tgid;
371         int                     need;
372         int                     want;
373 };
374
375 enum {
376        CEPH_MDSC_STOPPING_BEGIN = 1,
377        CEPH_MDSC_STOPPING_FLUSHED = 2,
378 };
379
380 /*
381  * mds client state
382  */
383 struct ceph_mds_client {
384         struct ceph_fs_client  *fsc;
385         struct mutex            mutex;         /* all nested structures */
386
387         struct ceph_mdsmap      *mdsmap;
388         struct completion       safe_umount_waiters;
389         wait_queue_head_t       session_close_wq;
390         struct list_head        waiting_for_map;
391         int                     mdsmap_err;
392
393         struct ceph_mds_session **sessions;    /* NULL for mds if no session */
394         atomic_t                num_sessions;
395         int                     max_sessions;  /* len of sessions array */
396         int                     stopping;      /* true if shutting down */
397
398         atomic64_t              quotarealms_count; /* # realms with quota */
399         /*
400          * We keep a list of inodes we don't see in the mountpoint but that we
401          * need to track quota realms.
402          */
403         struct rb_root          quotarealms_inodes;
404         struct mutex            quotarealms_inodes_mutex;
405
406         /*
407          * snap_rwsem will cover cap linkage into snaprealms, and
408          * realm snap contexts.  (later, we can do per-realm snap
409          * contexts locks..)  the empty list contains realms with no
410          * references (implying they contain no inodes with caps) that
411          * should be destroyed.
412          */
413         u64                     last_snap_seq;
414         struct rw_semaphore     snap_rwsem;
415         struct rb_root          snap_realms;
416         struct list_head        snap_empty;
417         int                     num_snap_realms;
418         spinlock_t              snap_empty_lock;  /* protect snap_empty */
419
420         u64                    last_tid;      /* most recent mds request */
421         u64                    oldest_tid;    /* oldest incomplete mds request,
422                                                  excluding setfilelock requests */
423         struct rb_root         request_tree;  /* pending mds requests */
424         struct delayed_work    delayed_work;  /* delayed work */
425         unsigned long    last_renew_caps;  /* last time we renewed our caps */
426         struct list_head cap_delay_list;   /* caps with delayed release */
427         spinlock_t       cap_delay_lock;   /* protects cap_delay_list */
428         struct list_head snap_flush_list;  /* cap_snaps ready to flush */
429         spinlock_t       snap_flush_lock;
430
431         u64               last_cap_flush_tid;
432         struct list_head  cap_flush_list;
433         struct list_head  cap_dirty_migrating; /* ...that are migration... */
434         int               num_cap_flushing; /* # caps we are flushing */
435         spinlock_t        cap_dirty_lock;   /* protects above items */
436         wait_queue_head_t cap_flushing_wq;
437
438         struct work_struct cap_reclaim_work;
439         atomic_t           cap_reclaim_pending;
440
441         /*
442          * Cap reservations
443          *
444          * Maintain a global pool of preallocated struct ceph_caps, referenced
445          * by struct ceph_caps_reservations.  This ensures that we preallocate
446          * memory needed to successfully process an MDS response.  (If an MDS
447          * sends us cap information and we fail to process it, we will have
448          * problems due to the client and MDS being out of sync.)
449          *
450          * Reservations are 'owned' by a ceph_cap_reservation context.
451          */
452         spinlock_t      caps_list_lock;
453         struct          list_head caps_list; /* unused (reserved or
454                                                 unreserved) */
455         struct          list_head cap_wait_list;
456         int             caps_total_count;    /* total caps allocated */
457         int             caps_use_count;      /* in use */
458         int             caps_use_max;        /* max used caps */
459         int             caps_reserve_count;  /* unused, reserved */
460         int             caps_avail_count;    /* unused, unreserved */
461         int             caps_min_count;      /* keep at least this many
462                                                 (unreserved) */
463         spinlock_t        dentry_list_lock;
464         struct list_head  dentry_leases;     /* fifo list */
465         struct list_head  dentry_dir_leases; /* lru list */
466
467         struct ceph_client_metric metric;
468
469         spinlock_t              snapid_map_lock;
470         struct rb_root          snapid_map_tree;
471         struct list_head        snapid_map_lru;
472
473         struct rw_semaphore     pool_perm_rwsem;
474         struct rb_root          pool_perm_tree;
475
476         char nodename[__NEW_UTS_LEN + 1];
477 };
478
479 extern const char *ceph_mds_op_name(int op);
480
481 extern bool check_session_state(struct ceph_mds_session *s);
482 void inc_session_sequence(struct ceph_mds_session *s);
483
484 extern struct ceph_mds_session *
485 __ceph_lookup_mds_session(struct ceph_mds_client *, int mds);
486
487 extern const char *ceph_session_state_name(int s);
488
489 extern struct ceph_mds_session *
490 ceph_get_mds_session(struct ceph_mds_session *s);
491 extern void ceph_put_mds_session(struct ceph_mds_session *s);
492
493 extern int ceph_send_msg_mds(struct ceph_mds_client *mdsc,
494                              struct ceph_msg *msg, int mds);
495
496 extern int ceph_mdsc_init(struct ceph_fs_client *fsc);
497 extern void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc);
498 extern void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc);
499 extern void ceph_mdsc_destroy(struct ceph_fs_client *fsc);
500
501 extern void ceph_mdsc_sync(struct ceph_mds_client *mdsc);
502
503 extern void ceph_invalidate_dir_request(struct ceph_mds_request *req);
504 extern int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
505                                            struct inode *dir);
506 extern struct ceph_mds_request *
507 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode);
508 extern int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
509                                     struct inode *dir,
510                                     struct ceph_mds_request *req);
511 extern int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
512                                 struct inode *dir,
513                                 struct ceph_mds_request *req);
514 extern void ceph_mdsc_release_dir_caps(struct ceph_mds_request *req);
515 extern void ceph_mdsc_release_dir_caps_no_check(struct ceph_mds_request *req);
516 static inline void ceph_mdsc_get_request(struct ceph_mds_request *req)
517 {
518         kref_get(&req->r_kref);
519 }
520 extern void ceph_mdsc_release_request(struct kref *kref);
521 static inline void ceph_mdsc_put_request(struct ceph_mds_request *req)
522 {
523         kref_put(&req->r_kref, ceph_mdsc_release_request);
524 }
525
526 extern void send_flush_mdlog(struct ceph_mds_session *s);
527 extern void ceph_mdsc_iterate_sessions(struct ceph_mds_client *mdsc,
528                                        void (*cb)(struct ceph_mds_session *),
529                                        bool check_state);
530 extern struct ceph_msg *ceph_create_session_msg(u32 op, u64 seq);
531 extern void __ceph_queue_cap_release(struct ceph_mds_session *session,
532                                     struct ceph_cap *cap);
533 extern void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
534                                     struct ceph_mds_session *session);
535 extern void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc);
536 extern void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr);
537 extern int ceph_iterate_session_caps(struct ceph_mds_session *session,
538                                      int (*cb)(struct inode *,
539                                                struct ceph_cap *, void *),
540                                      void *arg);
541 extern void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc);
542
543 static inline void ceph_mdsc_free_path(char *path, int len)
544 {
545         if (!IS_ERR_OR_NULL(path))
546                 __putname(path - (PATH_MAX - 1 - len));
547 }
548
549 extern char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
550                                   int stop_on_nosnap);
551
552 extern void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry);
553 extern void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
554                                      struct dentry *dentry, char action,
555                                      u32 seq);
556
557 extern void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc,
558                                     struct ceph_msg *msg);
559 extern void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc,
560                                    struct ceph_msg *msg);
561
562 extern struct ceph_mds_session *
563 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target);
564 extern void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
565                                           struct ceph_mds_session *session);
566
567 extern int ceph_trim_caps(struct ceph_mds_client *mdsc,
568                           struct ceph_mds_session *session,
569                           int max_caps);
570
571 static inline int ceph_wait_on_async_create(struct inode *inode)
572 {
573         struct ceph_inode_info *ci = ceph_inode(inode);
574
575         return wait_on_bit(&ci->i_ceph_flags, CEPH_ASYNC_CREATE_BIT,
576                            TASK_INTERRUPTIBLE);
577 }
578
579 extern u64 ceph_get_deleg_ino(struct ceph_mds_session *session);
580 extern int ceph_restore_deleg_ino(struct ceph_mds_session *session, u64 ino);
581 #endif