Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3 | |
Sage Weil | 496e595 | 2010-09-22 19:57:10 -0700 | [diff] [blame] | 4 | #include <linux/fs.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 5 | #include <linux/wait.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 7 | #include <linux/gfp.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 8 | #include <linux/sched.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/seq_file.h> |
Yan, Zheng | 3e0708b | 2015-05-22 16:38:02 +0800 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 12 | #include <linux/bits.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 13 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 14 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 15 | #include "mds_client.h" |
| 16 | |
Sage Weil | 1fe60e5 | 2012-07-30 16:23:22 -0700 | [diff] [blame] | 17 | #include <linux/ceph/ceph_features.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 18 | #include <linux/ceph/messenger.h> |
| 19 | #include <linux/ceph/decode.h> |
| 20 | #include <linux/ceph/pagelist.h> |
| 21 | #include <linux/ceph/auth.h> |
| 22 | #include <linux/ceph/debugfs.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 23 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 24 | #define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE) |
| 25 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 26 | /* |
| 27 | * A cluster of MDS (metadata server) daemons is responsible for |
| 28 | * managing the file system namespace (the directory hierarchy and |
| 29 | * inodes) and for coordinating shared access to storage. Metadata is |
| 30 | * partitioning hierarchically across a number of servers, and that |
| 31 | * partition varies over time as the cluster adjusts the distribution |
| 32 | * in order to balance load. |
| 33 | * |
| 34 | * The MDS client is primarily responsible to managing synchronous |
| 35 | * metadata requests for operations like open, unlink, and so forth. |
| 36 | * If there is a MDS failure, we find out about it when we (possibly |
| 37 | * request and) receive a new MDS map, and can resubmit affected |
| 38 | * requests. |
| 39 | * |
| 40 | * For the most part, though, we take advantage of a lossless |
| 41 | * communications channel to the MDS, and do not need to worry about |
| 42 | * timing out or resubmitting requests. |
| 43 | * |
| 44 | * We maintain a stateful "session" with each MDS we interact with. |
| 45 | * Within each session, we sent periodic heartbeat messages to ensure |
| 46 | * any capabilities or leases we have been issues remain valid. If |
| 47 | * the session times out and goes stale, our leases and capabilities |
| 48 | * are no longer valid. |
| 49 | */ |
| 50 | |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 51 | struct ceph_reconnect_state { |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 52 | struct ceph_mds_session *session; |
| 53 | int nr_caps, nr_realms; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 54 | struct ceph_pagelist *pagelist; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 55 | unsigned msg_version; |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 56 | bool allow_multi; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 59 | static void __wake_requests(struct ceph_mds_client *mdsc, |
| 60 | struct list_head *head); |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 61 | static void ceph_cap_release_work(struct work_struct *work); |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 62 | static void ceph_cap_reclaim_work(struct work_struct *work); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 63 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 64 | static const struct ceph_connection_operations mds_con_ops; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | /* |
| 68 | * mds reply parsing |
| 69 | */ |
| 70 | |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 71 | static int parse_reply_info_quota(void **p, void *end, |
| 72 | struct ceph_mds_reply_info_in *info) |
| 73 | { |
| 74 | u8 struct_v, struct_compat; |
| 75 | u32 struct_len; |
| 76 | |
| 77 | ceph_decode_8_safe(p, end, struct_v, bad); |
| 78 | ceph_decode_8_safe(p, end, struct_compat, bad); |
| 79 | /* struct_v is expected to be >= 1. we only |
| 80 | * understand encoding with struct_compat == 1. */ |
| 81 | if (!struct_v || struct_compat != 1) |
| 82 | goto bad; |
| 83 | ceph_decode_32_safe(p, end, struct_len, bad); |
| 84 | ceph_decode_need(p, end, struct_len, bad); |
| 85 | end = *p + struct_len; |
| 86 | ceph_decode_64_safe(p, end, info->max_bytes, bad); |
| 87 | ceph_decode_64_safe(p, end, info->max_files, bad); |
| 88 | *p = end; |
| 89 | return 0; |
| 90 | bad: |
| 91 | return -EIO; |
| 92 | } |
| 93 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 94 | /* |
| 95 | * parse individual inode info |
| 96 | */ |
| 97 | static int parse_reply_info_in(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 98 | struct ceph_mds_reply_info_in *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 99 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 100 | { |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 101 | int err = 0; |
| 102 | u8 struct_v = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 103 | |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 104 | if (features == (u64)-1) { |
| 105 | u32 struct_len; |
| 106 | u8 struct_compat; |
| 107 | ceph_decode_8_safe(p, end, struct_v, bad); |
| 108 | ceph_decode_8_safe(p, end, struct_compat, bad); |
| 109 | /* struct_v is expected to be >= 1. we only understand |
| 110 | * encoding with struct_compat == 1. */ |
| 111 | if (!struct_v || struct_compat != 1) |
| 112 | goto bad; |
| 113 | ceph_decode_32_safe(p, end, struct_len, bad); |
| 114 | ceph_decode_need(p, end, struct_len, bad); |
| 115 | end = *p + struct_len; |
| 116 | } |
| 117 | |
| 118 | ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 119 | info->in = *p; |
| 120 | *p += sizeof(struct ceph_mds_reply_inode) + |
| 121 | sizeof(*info->in->fragtree.splits) * |
| 122 | le32_to_cpu(info->in->fragtree.nsplits); |
| 123 | |
| 124 | ceph_decode_32_safe(p, end, info->symlink_len, bad); |
| 125 | ceph_decode_need(p, end, info->symlink_len, bad); |
| 126 | info->symlink = *p; |
| 127 | *p += info->symlink_len; |
| 128 | |
Ilya Dryomov | 23c625c | 2018-11-08 14:55:21 +0100 | [diff] [blame] | 129 | ceph_decode_copy_safe(p, end, &info->dir_layout, |
| 130 | sizeof(info->dir_layout), bad); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 131 | ceph_decode_32_safe(p, end, info->xattr_len, bad); |
| 132 | ceph_decode_need(p, end, info->xattr_len, bad); |
| 133 | info->xattr_data = *p; |
| 134 | *p += info->xattr_len; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 135 | |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 136 | if (features == (u64)-1) { |
| 137 | /* inline data */ |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 138 | ceph_decode_64_safe(p, end, info->inline_version, bad); |
| 139 | ceph_decode_32_safe(p, end, info->inline_len, bad); |
| 140 | ceph_decode_need(p, end, info->inline_len, bad); |
| 141 | info->inline_data = *p; |
| 142 | *p += info->inline_len; |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 143 | /* quota */ |
| 144 | err = parse_reply_info_quota(p, end, info); |
| 145 | if (err < 0) |
| 146 | goto out_bad; |
| 147 | /* pool namespace */ |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 148 | ceph_decode_32_safe(p, end, info->pool_ns_len, bad); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 149 | if (info->pool_ns_len > 0) { |
| 150 | ceph_decode_need(p, end, info->pool_ns_len, bad); |
| 151 | info->pool_ns_data = *p; |
| 152 | *p += info->pool_ns_len; |
| 153 | } |
Jeff Layton | 245ce99 | 2019-05-29 11:19:42 -0400 | [diff] [blame] | 154 | |
| 155 | /* btime */ |
| 156 | ceph_decode_need(p, end, sizeof(info->btime), bad); |
| 157 | ceph_decode_copy(p, &info->btime, sizeof(info->btime)); |
| 158 | |
| 159 | /* change attribute */ |
Jeff Layton | a35ead3 | 2019-06-06 07:29:23 -0400 | [diff] [blame] | 160 | ceph_decode_64_safe(p, end, info->change_attr, bad); |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 161 | |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 162 | /* dir pin */ |
| 163 | if (struct_v >= 2) { |
| 164 | ceph_decode_32_safe(p, end, info->dir_pin, bad); |
| 165 | } else { |
| 166 | info->dir_pin = -ENODATA; |
| 167 | } |
| 168 | |
David Disseldorp | 193e7b3 | 2019-04-18 14:15:46 +0200 | [diff] [blame] | 169 | /* snapshot birth time, remains zero for v<=2 */ |
| 170 | if (struct_v >= 3) { |
| 171 | ceph_decode_need(p, end, sizeof(info->snap_btime), bad); |
| 172 | ceph_decode_copy(p, &info->snap_btime, |
| 173 | sizeof(info->snap_btime)); |
| 174 | } else { |
| 175 | memset(&info->snap_btime, 0, sizeof(info->snap_btime)); |
| 176 | } |
| 177 | |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 178 | *p = end; |
| 179 | } else { |
| 180 | if (features & CEPH_FEATURE_MDS_INLINE_DATA) { |
| 181 | ceph_decode_64_safe(p, end, info->inline_version, bad); |
| 182 | ceph_decode_32_safe(p, end, info->inline_len, bad); |
| 183 | ceph_decode_need(p, end, info->inline_len, bad); |
| 184 | info->inline_data = *p; |
| 185 | *p += info->inline_len; |
| 186 | } else |
| 187 | info->inline_version = CEPH_INLINE_NONE; |
| 188 | |
| 189 | if (features & CEPH_FEATURE_MDS_QUOTA) { |
| 190 | err = parse_reply_info_quota(p, end, info); |
| 191 | if (err < 0) |
| 192 | goto out_bad; |
| 193 | } else { |
| 194 | info->max_bytes = 0; |
| 195 | info->max_files = 0; |
| 196 | } |
| 197 | |
| 198 | info->pool_ns_len = 0; |
| 199 | info->pool_ns_data = NULL; |
| 200 | if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) { |
| 201 | ceph_decode_32_safe(p, end, info->pool_ns_len, bad); |
| 202 | if (info->pool_ns_len > 0) { |
| 203 | ceph_decode_need(p, end, info->pool_ns_len, bad); |
| 204 | info->pool_ns_data = *p; |
| 205 | *p += info->pool_ns_len; |
| 206 | } |
| 207 | } |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 208 | |
Jeff Layton | 245ce99 | 2019-05-29 11:19:42 -0400 | [diff] [blame] | 209 | if (features & CEPH_FEATURE_FS_BTIME) { |
| 210 | ceph_decode_need(p, end, sizeof(info->btime), bad); |
| 211 | ceph_decode_copy(p, &info->btime, sizeof(info->btime)); |
Jeff Layton | a35ead3 | 2019-06-06 07:29:23 -0400 | [diff] [blame] | 212 | ceph_decode_64_safe(p, end, info->change_attr, bad); |
Jeff Layton | 245ce99 | 2019-05-29 11:19:42 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 215 | info->dir_pin = -ENODATA; |
David Disseldorp | 193e7b3 | 2019-04-18 14:15:46 +0200 | [diff] [blame] | 216 | /* info->snap_btime remains zero */ |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 217 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | bad: |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 220 | err = -EIO; |
| 221 | out_bad: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 222 | return err; |
| 223 | } |
| 224 | |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 225 | static int parse_reply_info_dir(void **p, void *end, |
| 226 | struct ceph_mds_reply_dirfrag **dirfrag, |
| 227 | u64 features) |
| 228 | { |
| 229 | if (features == (u64)-1) { |
| 230 | u8 struct_v, struct_compat; |
| 231 | u32 struct_len; |
| 232 | ceph_decode_8_safe(p, end, struct_v, bad); |
| 233 | ceph_decode_8_safe(p, end, struct_compat, bad); |
| 234 | /* struct_v is expected to be >= 1. we only understand |
| 235 | * encoding whose struct_compat == 1. */ |
| 236 | if (!struct_v || struct_compat != 1) |
| 237 | goto bad; |
| 238 | ceph_decode_32_safe(p, end, struct_len, bad); |
| 239 | ceph_decode_need(p, end, struct_len, bad); |
| 240 | end = *p + struct_len; |
| 241 | } |
| 242 | |
| 243 | ceph_decode_need(p, end, sizeof(**dirfrag), bad); |
| 244 | *dirfrag = *p; |
| 245 | *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist); |
| 246 | if (unlikely(*p > end)) |
| 247 | goto bad; |
| 248 | if (features == (u64)-1) |
| 249 | *p = end; |
| 250 | return 0; |
| 251 | bad: |
| 252 | return -EIO; |
| 253 | } |
| 254 | |
| 255 | static int parse_reply_info_lease(void **p, void *end, |
| 256 | struct ceph_mds_reply_lease **lease, |
| 257 | u64 features) |
| 258 | { |
| 259 | if (features == (u64)-1) { |
| 260 | u8 struct_v, struct_compat; |
| 261 | u32 struct_len; |
| 262 | ceph_decode_8_safe(p, end, struct_v, bad); |
| 263 | ceph_decode_8_safe(p, end, struct_compat, bad); |
| 264 | /* struct_v is expected to be >= 1. we only understand |
| 265 | * encoding whose struct_compat == 1. */ |
| 266 | if (!struct_v || struct_compat != 1) |
| 267 | goto bad; |
| 268 | ceph_decode_32_safe(p, end, struct_len, bad); |
| 269 | ceph_decode_need(p, end, struct_len, bad); |
| 270 | end = *p + struct_len; |
| 271 | } |
| 272 | |
| 273 | ceph_decode_need(p, end, sizeof(**lease), bad); |
| 274 | *lease = *p; |
| 275 | *p += sizeof(**lease); |
| 276 | if (features == (u64)-1) |
| 277 | *p = end; |
| 278 | return 0; |
| 279 | bad: |
| 280 | return -EIO; |
| 281 | } |
| 282 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 283 | /* |
| 284 | * parse a normal reply, which may contain a (dir+)dentry and/or a |
| 285 | * target inode. |
| 286 | */ |
| 287 | static int parse_reply_info_trace(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 288 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 289 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 290 | { |
| 291 | int err; |
| 292 | |
| 293 | if (info->head->is_dentry) { |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 294 | err = parse_reply_info_in(p, end, &info->diri, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 295 | if (err < 0) |
| 296 | goto out_bad; |
| 297 | |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 298 | err = parse_reply_info_dir(p, end, &info->dirfrag, features); |
| 299 | if (err < 0) |
| 300 | goto out_bad; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 301 | |
| 302 | ceph_decode_32_safe(p, end, info->dname_len, bad); |
| 303 | ceph_decode_need(p, end, info->dname_len, bad); |
| 304 | info->dname = *p; |
| 305 | *p += info->dname_len; |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 306 | |
| 307 | err = parse_reply_info_lease(p, end, &info->dlease, features); |
| 308 | if (err < 0) |
| 309 | goto out_bad; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (info->head->is_target) { |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 313 | err = parse_reply_info_in(p, end, &info->targeti, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 314 | if (err < 0) |
| 315 | goto out_bad; |
| 316 | } |
| 317 | |
| 318 | if (unlikely(*p != end)) |
| 319 | goto bad; |
| 320 | return 0; |
| 321 | |
| 322 | bad: |
| 323 | err = -EIO; |
| 324 | out_bad: |
| 325 | pr_err("problem parsing mds trace %d\n", err); |
| 326 | return err; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * parse readdir results |
| 331 | */ |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 332 | static int parse_reply_info_readdir(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 333 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 334 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 335 | { |
| 336 | u32 num, i = 0; |
| 337 | int err; |
| 338 | |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 339 | err = parse_reply_info_dir(p, end, &info->dir_dir, features); |
| 340 | if (err < 0) |
| 341 | goto out_bad; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 342 | |
| 343 | ceph_decode_need(p, end, sizeof(num) + 2, bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 344 | num = ceph_decode_32(p); |
Yan, Zheng | 956d39d | 2016-04-27 17:48:30 +0800 | [diff] [blame] | 345 | { |
| 346 | u16 flags = ceph_decode_16(p); |
| 347 | info->dir_end = !!(flags & CEPH_READDIR_FRAG_END); |
| 348 | info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 349 | info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER); |
Yan, Zheng | 7916254 | 2017-04-05 12:54:05 -0400 | [diff] [blame] | 350 | info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH); |
Yan, Zheng | 956d39d | 2016-04-27 17:48:30 +0800 | [diff] [blame] | 351 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 352 | if (num == 0) |
| 353 | goto done; |
| 354 | |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 355 | BUG_ON(!info->dir_entries); |
| 356 | if ((unsigned long)(info->dir_entries + num) > |
| 357 | (unsigned long)info->dir_entries + info->dir_buf_size) { |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 358 | pr_err("dir contents are larger than expected\n"); |
| 359 | WARN_ON(1); |
| 360 | goto bad; |
| 361 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 362 | |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 363 | info->dir_nr = num; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 364 | while (num) { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 365 | struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 366 | /* dentry */ |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 367 | ceph_decode_32_safe(p, end, rde->name_len, bad); |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 368 | ceph_decode_need(p, end, rde->name_len, bad); |
| 369 | rde->name = *p; |
| 370 | *p += rde->name_len; |
| 371 | dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 372 | |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 373 | /* dentry lease */ |
| 374 | err = parse_reply_info_lease(p, end, &rde->lease, features); |
| 375 | if (err) |
| 376 | goto out_bad; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 377 | /* inode */ |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 378 | err = parse_reply_info_in(p, end, &rde->inode, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 379 | if (err < 0) |
| 380 | goto out_bad; |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 381 | /* ceph_readdir_prepopulate() will update it */ |
| 382 | rde->offset = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 383 | i++; |
| 384 | num--; |
| 385 | } |
| 386 | |
| 387 | done: |
Jeff Layton | 1d3f872 | 2019-09-26 16:05:11 -0400 | [diff] [blame] | 388 | /* Skip over any unrecognized fields */ |
| 389 | *p = end; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 390 | return 0; |
| 391 | |
| 392 | bad: |
| 393 | err = -EIO; |
| 394 | out_bad: |
| 395 | pr_err("problem parsing dir contents %d\n", err); |
| 396 | return err; |
| 397 | } |
| 398 | |
| 399 | /* |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 400 | * parse fcntl F_GETLK results |
| 401 | */ |
| 402 | static int parse_reply_info_filelock(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 403 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 404 | u64 features) |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 405 | { |
| 406 | if (*p + sizeof(*info->filelock_reply) > end) |
| 407 | goto bad; |
| 408 | |
| 409 | info->filelock_reply = *p; |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 410 | |
Jeff Layton | 1d3f872 | 2019-09-26 16:05:11 -0400 | [diff] [blame] | 411 | /* Skip over any unrecognized fields */ |
| 412 | *p = end; |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 413 | return 0; |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 414 | bad: |
| 415 | return -EIO; |
| 416 | } |
| 417 | |
| 418 | /* |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 419 | * parse create results |
| 420 | */ |
| 421 | static int parse_reply_info_create(void **p, void *end, |
| 422 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 423 | u64 features) |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 424 | { |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 425 | if (features == (u64)-1 || |
| 426 | (features & CEPH_FEATURE_REPLY_CREATE_INODE)) { |
Jeff Layton | 1d3f872 | 2019-09-26 16:05:11 -0400 | [diff] [blame] | 427 | /* Malformed reply? */ |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 428 | if (*p == end) { |
| 429 | info->has_create_ino = false; |
| 430 | } else { |
| 431 | info->has_create_ino = true; |
Jeff Layton | 1d3f872 | 2019-09-26 16:05:11 -0400 | [diff] [blame] | 432 | ceph_decode_64_safe(p, end, info->ino, bad); |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 433 | } |
Jeff Layton | 1d3f872 | 2019-09-26 16:05:11 -0400 | [diff] [blame] | 434 | } else { |
| 435 | if (*p != end) |
| 436 | goto bad; |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Jeff Layton | 1d3f872 | 2019-09-26 16:05:11 -0400 | [diff] [blame] | 439 | /* Skip over any unrecognized fields */ |
| 440 | *p = end; |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 441 | return 0; |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 442 | bad: |
| 443 | return -EIO; |
| 444 | } |
| 445 | |
| 446 | /* |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 447 | * parse extra results |
| 448 | */ |
| 449 | static int parse_reply_info_extra(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 450 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 451 | u64 features) |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 452 | { |
Jeff Layton | 6df8c9d | 2017-01-12 14:42:41 -0500 | [diff] [blame] | 453 | u32 op = le32_to_cpu(info->head->op); |
| 454 | |
| 455 | if (op == CEPH_MDS_OP_GETFILELOCK) |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 456 | return parse_reply_info_filelock(p, end, info, features); |
Jeff Layton | 6df8c9d | 2017-01-12 14:42:41 -0500 | [diff] [blame] | 457 | else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP) |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 458 | return parse_reply_info_readdir(p, end, info, features); |
Jeff Layton | 6df8c9d | 2017-01-12 14:42:41 -0500 | [diff] [blame] | 459 | else if (op == CEPH_MDS_OP_CREATE) |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 460 | return parse_reply_info_create(p, end, info, features); |
| 461 | else |
| 462 | return -EIO; |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 466 | * parse entire mds reply |
| 467 | */ |
| 468 | static int parse_reply_info(struct ceph_msg *msg, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 469 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 470 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 471 | { |
| 472 | void *p, *end; |
| 473 | u32 len; |
| 474 | int err; |
| 475 | |
| 476 | info->head = msg->front.iov_base; |
| 477 | p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head); |
| 478 | end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head); |
| 479 | |
| 480 | /* trace */ |
| 481 | ceph_decode_32_safe(&p, end, len, bad); |
| 482 | if (len > 0) { |
Xi Wang | 32852a8 | 2012-01-14 22:20:59 -0500 | [diff] [blame] | 483 | ceph_decode_need(&p, end, len, bad); |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 484 | err = parse_reply_info_trace(&p, p+len, info, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 485 | if (err < 0) |
| 486 | goto out_bad; |
| 487 | } |
| 488 | |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 489 | /* extra */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 490 | ceph_decode_32_safe(&p, end, len, bad); |
| 491 | if (len > 0) { |
Xi Wang | 32852a8 | 2012-01-14 22:20:59 -0500 | [diff] [blame] | 492 | ceph_decode_need(&p, end, len, bad); |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 493 | err = parse_reply_info_extra(&p, p+len, info, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 494 | if (err < 0) |
| 495 | goto out_bad; |
| 496 | } |
| 497 | |
| 498 | /* snap blob */ |
| 499 | ceph_decode_32_safe(&p, end, len, bad); |
| 500 | info->snapblob_len = len; |
| 501 | info->snapblob = p; |
| 502 | p += len; |
| 503 | |
| 504 | if (p != end) |
| 505 | goto bad; |
| 506 | return 0; |
| 507 | |
| 508 | bad: |
| 509 | err = -EIO; |
| 510 | out_bad: |
| 511 | pr_err("mds parse_reply err %d\n", err); |
| 512 | return err; |
| 513 | } |
| 514 | |
| 515 | static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info) |
| 516 | { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 517 | if (!info->dir_entries) |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 518 | return; |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 519 | free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
| 523 | /* |
| 524 | * sessions |
| 525 | */ |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 526 | const char *ceph_session_state_name(int s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 527 | { |
| 528 | switch (s) { |
| 529 | case CEPH_MDS_SESSION_NEW: return "new"; |
| 530 | case CEPH_MDS_SESSION_OPENING: return "opening"; |
| 531 | case CEPH_MDS_SESSION_OPEN: return "open"; |
| 532 | case CEPH_MDS_SESSION_HUNG: return "hung"; |
| 533 | case CEPH_MDS_SESSION_CLOSING: return "closing"; |
Xiubo Li | 4d681c2 | 2019-12-05 22:35:51 -0500 | [diff] [blame] | 534 | case CEPH_MDS_SESSION_CLOSED: return "closed"; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 535 | case CEPH_MDS_SESSION_RESTARTING: return "restarting"; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 536 | case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting"; |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 537 | case CEPH_MDS_SESSION_REJECTED: return "rejected"; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 538 | default: return "???"; |
| 539 | } |
| 540 | } |
| 541 | |
Xiubo Li | 5b3248c | 2019-12-19 19:44:09 -0500 | [diff] [blame] | 542 | struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 543 | { |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 544 | if (refcount_inc_not_zero(&s->s_ref)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 545 | dout("mdsc get_session %p %d -> %d\n", s, |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 546 | refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 547 | return s; |
| 548 | } else { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 549 | dout("mdsc get_session %p 0 -- FAIL\n", s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 550 | return NULL; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void ceph_put_mds_session(struct ceph_mds_session *s) |
| 555 | { |
| 556 | dout("mdsc put_session %p %d -> %d\n", s, |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 557 | refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1); |
| 558 | if (refcount_dec_and_test(&s->s_ref)) { |
Alex Elder | 6c4a191 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 559 | if (s->s_auth.authorizer) |
Ilya Dryomov | 6c1ea26 | 2016-04-11 19:34:49 +0200 | [diff] [blame] | 560 | ceph_auth_destroy_authorizer(s->s_auth.authorizer); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 561 | kfree(s); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 562 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* |
| 566 | * called under mdsc->mutex |
| 567 | */ |
| 568 | struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc, |
| 569 | int mds) |
| 570 | { |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 571 | if (mds >= mdsc->max_sessions || !mdsc->sessions[mds]) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 572 | return NULL; |
Xiubo Li | 5b3248c | 2019-12-19 19:44:09 -0500 | [diff] [blame] | 573 | return ceph_get_mds_session(mdsc->sessions[mds]); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static bool __have_session(struct ceph_mds_client *mdsc, int mds) |
| 577 | { |
Chengguang Xu | 98cfda8 | 2018-03-13 10:43:45 +0800 | [diff] [blame] | 578 | if (mds >= mdsc->max_sessions || !mdsc->sessions[mds]) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 579 | return false; |
Chengguang Xu | 98cfda8 | 2018-03-13 10:43:45 +0800 | [diff] [blame] | 580 | else |
| 581 | return true; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 584 | static int __verify_registered_session(struct ceph_mds_client *mdsc, |
| 585 | struct ceph_mds_session *s) |
| 586 | { |
| 587 | if (s->s_mds >= mdsc->max_sessions || |
| 588 | mdsc->sessions[s->s_mds] != s) |
| 589 | return -ENOENT; |
| 590 | return 0; |
| 591 | } |
| 592 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 593 | /* |
| 594 | * create+register a new session for given mds. |
| 595 | * called under mdsc->mutex. |
| 596 | */ |
| 597 | static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc, |
| 598 | int mds) |
| 599 | { |
| 600 | struct ceph_mds_session *s; |
| 601 | |
Xiubo Li | b38c9eb | 2019-12-04 06:57:39 -0500 | [diff] [blame] | 602 | if (mds >= mdsc->mdsmap->possible_max_rank) |
Nathaniel Yazdani | c338c07 | 2013-08-04 21:04:30 -0700 | [diff] [blame] | 603 | return ERR_PTR(-EINVAL); |
| 604 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 605 | s = kzalloc(sizeof(*s), GFP_NOFS); |
Dan Carpenter | 4736b00 | 2010-03-20 15:30:16 +0300 | [diff] [blame] | 606 | if (!s) |
| 607 | return ERR_PTR(-ENOMEM); |
Chengguang Xu | 47474d0 | 2018-03-13 23:01:07 +0800 | [diff] [blame] | 608 | |
| 609 | if (mds >= mdsc->max_sessions) { |
| 610 | int newmax = 1 << get_count_order(mds + 1); |
| 611 | struct ceph_mds_session **sa; |
| 612 | |
| 613 | dout("%s: realloc to %d\n", __func__, newmax); |
| 614 | sa = kcalloc(newmax, sizeof(void *), GFP_NOFS); |
| 615 | if (!sa) |
| 616 | goto fail_realloc; |
| 617 | if (mdsc->sessions) { |
| 618 | memcpy(sa, mdsc->sessions, |
| 619 | mdsc->max_sessions * sizeof(void *)); |
| 620 | kfree(mdsc->sessions); |
| 621 | } |
| 622 | mdsc->sessions = sa; |
| 623 | mdsc->max_sessions = newmax; |
| 624 | } |
| 625 | |
| 626 | dout("%s: mds%d\n", __func__, mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 627 | s->s_mdsc = mdsc; |
| 628 | s->s_mds = mds; |
| 629 | s->s_state = CEPH_MDS_SESSION_NEW; |
| 630 | s->s_ttl = 0; |
| 631 | s->s_seq = 0; |
| 632 | mutex_init(&s->s_mutex); |
| 633 | |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 634 | ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 635 | |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 636 | spin_lock_init(&s->s_gen_ttl_lock); |
Yan, Zheng | 1e9c2eb | 2019-01-28 20:43:55 +0800 | [diff] [blame] | 637 | s->s_cap_gen = 1; |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 638 | s->s_cap_ttl = jiffies - 1; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 639 | |
| 640 | spin_lock_init(&s->s_cap_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 641 | s->s_renew_requested = 0; |
| 642 | s->s_renew_seq = 0; |
| 643 | INIT_LIST_HEAD(&s->s_caps); |
| 644 | s->s_nr_caps = 0; |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 645 | refcount_set(&s->s_ref, 1); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 646 | INIT_LIST_HEAD(&s->s_waiting); |
| 647 | INIT_LIST_HEAD(&s->s_unsafe); |
| 648 | s->s_num_cap_releases = 0; |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 649 | s->s_cap_reconnect = 0; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 650 | s->s_cap_iterator = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 651 | INIT_LIST_HEAD(&s->s_cap_releases); |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 652 | INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work); |
| 653 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 654 | INIT_LIST_HEAD(&s->s_cap_flushing); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 655 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 656 | mdsc->sessions[mds] = s; |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 657 | atomic_inc(&mdsc->num_sessions); |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 658 | refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */ |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 659 | |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 660 | ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds, |
| 661 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 662 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 663 | return s; |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 664 | |
| 665 | fail_realloc: |
| 666 | kfree(s); |
| 667 | return ERR_PTR(-ENOMEM); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | /* |
| 671 | * called under mdsc->mutex |
| 672 | */ |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 673 | static void __unregister_session(struct ceph_mds_client *mdsc, |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 674 | struct ceph_mds_session *s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 675 | { |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 676 | dout("__unregister_session mds%d %p\n", s->s_mds, s); |
| 677 | BUG_ON(mdsc->sessions[s->s_mds] != s); |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 678 | mdsc->sessions[s->s_mds] = NULL; |
| 679 | ceph_con_close(&s->s_con); |
| 680 | ceph_put_mds_session(s); |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 681 | atomic_dec(&mdsc->num_sessions); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * drop session refs in request. |
| 686 | * |
| 687 | * should be last request ref, or hold mdsc->mutex |
| 688 | */ |
| 689 | static void put_request_session(struct ceph_mds_request *req) |
| 690 | { |
| 691 | if (req->r_session) { |
| 692 | ceph_put_mds_session(req->r_session); |
| 693 | req->r_session = NULL; |
| 694 | } |
| 695 | } |
| 696 | |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 697 | void ceph_mdsc_release_request(struct kref *kref) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 698 | { |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 699 | struct ceph_mds_request *req = container_of(kref, |
| 700 | struct ceph_mds_request, |
| 701 | r_kref); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 702 | destroy_reply_info(&req->r_reply_info); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 703 | if (req->r_request) |
| 704 | ceph_msg_put(req->r_request); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 705 | if (req->r_reply) |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 706 | ceph_msg_put(req->r_reply); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 707 | if (req->r_inode) { |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 708 | ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 709 | /* avoid calling iput_final() in mds dispatch threads */ |
| 710 | ceph_async_iput(req->r_inode); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 711 | } |
Jeff Layton | 9c1c2b3 | 2019-04-03 13:16:01 -0400 | [diff] [blame] | 712 | if (req->r_parent) { |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 713 | ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN); |
Jeff Layton | 9c1c2b3 | 2019-04-03 13:16:01 -0400 | [diff] [blame] | 714 | ceph_async_iput(req->r_parent); |
| 715 | } |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 716 | ceph_async_iput(req->r_target_inode); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 717 | if (req->r_dentry) |
| 718 | dput(req->r_dentry); |
Sage Weil | 844d87c | 2013-02-05 13:40:09 -0800 | [diff] [blame] | 719 | if (req->r_old_dentry) |
| 720 | dput(req->r_old_dentry); |
| 721 | if (req->r_old_dentry_dir) { |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 722 | /* |
| 723 | * track (and drop pins for) r_old_dentry_dir |
| 724 | * separately, since r_old_dentry's d_parent may have |
| 725 | * changed between the dir mutex being dropped and |
| 726 | * this request being freed. |
| 727 | */ |
| 728 | ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir), |
| 729 | CEPH_CAP_PIN); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 730 | ceph_async_iput(req->r_old_dentry_dir); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 731 | } |
| 732 | kfree(req->r_path1); |
| 733 | kfree(req->r_path2); |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 734 | if (req->r_pagelist) |
| 735 | ceph_pagelist_release(req->r_pagelist); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 736 | put_request_session(req); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 737 | ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation); |
Yan, Zheng | 428138c | 2019-06-14 10:55:05 +0800 | [diff] [blame] | 738 | WARN_ON_ONCE(!list_empty(&req->r_wait)); |
Jeff Layton | 058daab | 2020-02-17 18:38:37 -0500 | [diff] [blame] | 739 | kmem_cache_free(ceph_mds_request_cachep, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 742 | DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node) |
| 743 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 744 | /* |
| 745 | * lookup session, bump ref if found. |
| 746 | * |
| 747 | * called under mdsc->mutex. |
| 748 | */ |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 749 | static struct ceph_mds_request * |
| 750 | lookup_get_request(struct ceph_mds_client *mdsc, u64 tid) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 751 | { |
| 752 | struct ceph_mds_request *req; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 753 | |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 754 | req = lookup_request(&mdsc->request_tree, tid); |
| 755 | if (req) |
| 756 | ceph_mdsc_get_request(req); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 757 | |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 758 | return req; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Register an in-flight request, and assign a tid. Link to directory |
| 763 | * are modifying (if any). |
| 764 | * |
| 765 | * Called under mdsc->mutex. |
| 766 | */ |
| 767 | static void __register_request(struct ceph_mds_client *mdsc, |
| 768 | struct ceph_mds_request *req, |
| 769 | struct inode *dir) |
| 770 | { |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 771 | int ret = 0; |
| 772 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 773 | req->r_tid = ++mdsc->last_tid; |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 774 | if (req->r_num_caps) { |
| 775 | ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation, |
| 776 | req->r_num_caps); |
| 777 | if (ret < 0) { |
| 778 | pr_err("__register_request %p " |
| 779 | "failed to reserve caps: %d\n", req, ret); |
| 780 | /* set req->r_err to fail early from __do_request */ |
| 781 | req->r_err = ret; |
| 782 | return; |
| 783 | } |
| 784 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 785 | dout("__register_request %p tid %lld\n", req, req->r_tid); |
| 786 | ceph_mdsc_get_request(req); |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 787 | insert_request(&mdsc->request_tree, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 788 | |
Sage Weil | cb4276c | 2010-11-08 07:28:52 -0800 | [diff] [blame] | 789 | req->r_uid = current_fsuid(); |
| 790 | req->r_gid = current_fsgid(); |
| 791 | |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 792 | if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK) |
| 793 | mdsc->oldest_tid = req->r_tid; |
| 794 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 795 | if (dir) { |
Jeff Layton | 3db0a2f | 2019-04-04 08:05:38 -0400 | [diff] [blame] | 796 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 797 | |
Sage Weil | 3b66378 | 2011-05-18 16:12:12 -0700 | [diff] [blame] | 798 | ihold(dir); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 799 | req->r_unsafe_dir = dir; |
Jeff Layton | 3db0a2f | 2019-04-04 08:05:38 -0400 | [diff] [blame] | 800 | spin_lock(&ci->i_unsafe_lock); |
| 801 | list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops); |
| 802 | spin_unlock(&ci->i_unsafe_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
| 806 | static void __unregister_request(struct ceph_mds_client *mdsc, |
| 807 | struct ceph_mds_request *req) |
| 808 | { |
| 809 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 810 | |
Jeff Layton | df963ea | 2017-02-14 10:09:40 -0500 | [diff] [blame] | 811 | /* Never leave an unregistered request on an unsafe list! */ |
| 812 | list_del_init(&req->r_unsafe_item); |
| 813 | |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 814 | if (req->r_tid == mdsc->oldest_tid) { |
| 815 | struct rb_node *p = rb_next(&req->r_node); |
| 816 | mdsc->oldest_tid = 0; |
| 817 | while (p) { |
| 818 | struct ceph_mds_request *next_req = |
| 819 | rb_entry(p, struct ceph_mds_request, r_node); |
| 820 | if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) { |
| 821 | mdsc->oldest_tid = next_req->r_tid; |
| 822 | break; |
| 823 | } |
| 824 | p = rb_next(p); |
| 825 | } |
| 826 | } |
| 827 | |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 828 | erase_request(&mdsc->request_tree, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 829 | |
Jeff Layton | 3db0a2f | 2019-04-04 08:05:38 -0400 | [diff] [blame] | 830 | if (req->r_unsafe_dir) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 831 | struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 832 | spin_lock(&ci->i_unsafe_lock); |
| 833 | list_del_init(&req->r_unsafe_dir_item); |
| 834 | spin_unlock(&ci->i_unsafe_lock); |
Yan, Zheng | 4c06ace | 2015-10-27 17:18:00 +0800 | [diff] [blame] | 835 | } |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 836 | if (req->r_target_inode && |
| 837 | test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 838 | struct ceph_inode_info *ci = ceph_inode(req->r_target_inode); |
| 839 | spin_lock(&ci->i_unsafe_lock); |
| 840 | list_del_init(&req->r_unsafe_target_item); |
| 841 | spin_unlock(&ci->i_unsafe_lock); |
| 842 | } |
Sage Weil | 3b66378 | 2011-05-18 16:12:12 -0700 | [diff] [blame] | 843 | |
Yan, Zheng | 4c06ace | 2015-10-27 17:18:00 +0800 | [diff] [blame] | 844 | if (req->r_unsafe_dir) { |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 845 | /* avoid calling iput_final() in mds dispatch threads */ |
| 846 | ceph_async_iput(req->r_unsafe_dir); |
Sage Weil | 3b66378 | 2011-05-18 16:12:12 -0700 | [diff] [blame] | 847 | req->r_unsafe_dir = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 848 | } |
Sage Weil | 94aa8ae | 2010-03-28 21:22:50 -0700 | [diff] [blame] | 849 | |
Yan, Zheng | fc55d2c | 2013-10-31 09:10:47 +0800 | [diff] [blame] | 850 | complete_all(&req->r_safe_completion); |
| 851 | |
Sage Weil | 94aa8ae | 2010-03-28 21:22:50 -0700 | [diff] [blame] | 852 | ceph_mdsc_put_request(req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 856 | * Walk back up the dentry tree until we hit a dentry representing a |
| 857 | * non-snapshot inode. We do this using the rcu_read_lock (which must be held |
| 858 | * when calling this) to ensure that the objects won't disappear while we're |
| 859 | * working with them. Once we hit a candidate dentry, we attempt to take a |
| 860 | * reference to it, and return that as the result. |
| 861 | */ |
Dan Carpenter | f107548 | 2017-02-23 13:39:59 +0300 | [diff] [blame] | 862 | static struct inode *get_nonsnap_parent(struct dentry *dentry) |
| 863 | { |
| 864 | struct inode *inode = NULL; |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 865 | |
| 866 | while (dentry && !IS_ROOT(dentry)) { |
| 867 | inode = d_inode_rcu(dentry); |
| 868 | if (!inode || ceph_snap(inode) == CEPH_NOSNAP) |
| 869 | break; |
| 870 | dentry = dentry->d_parent; |
| 871 | } |
| 872 | if (inode) |
| 873 | inode = igrab(inode); |
| 874 | return inode; |
| 875 | } |
| 876 | |
| 877 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 878 | * Choose mds to send request to next. If there is a hint set in the |
| 879 | * request (e.g., due to a prior forward hint from the mds), use that. |
| 880 | * Otherwise, consult frag tree and/or caps to identify the |
| 881 | * appropriate mds. If all else fails, choose randomly. |
| 882 | * |
| 883 | * Called under mdsc->mutex. |
| 884 | */ |
| 885 | static int __choose_mds(struct ceph_mds_client *mdsc, |
Xiubo Li | c4853e9 | 2019-12-09 07:47:15 -0500 | [diff] [blame] | 886 | struct ceph_mds_request *req, |
| 887 | bool *random) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 888 | { |
| 889 | struct inode *inode; |
| 890 | struct ceph_inode_info *ci; |
| 891 | struct ceph_cap *cap; |
| 892 | int mode = req->r_direct_mode; |
| 893 | int mds = -1; |
| 894 | u32 hash = req->r_direct_hash; |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 895 | bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 896 | |
Xiubo Li | c4853e9 | 2019-12-09 07:47:15 -0500 | [diff] [blame] | 897 | if (random) |
| 898 | *random = false; |
| 899 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 900 | /* |
| 901 | * is there a specific mds we should try? ignore hint if we have |
| 902 | * no session and the mds is not up (active or recovering). |
| 903 | */ |
| 904 | if (req->r_resend_mds >= 0 && |
| 905 | (__have_session(mdsc, req->r_resend_mds) || |
| 906 | ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) { |
Xiubo Li | 3c80209 | 2020-01-01 22:09:37 -0500 | [diff] [blame] | 907 | dout("%s using resend_mds mds%d\n", __func__, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 908 | req->r_resend_mds); |
| 909 | return req->r_resend_mds; |
| 910 | } |
| 911 | |
| 912 | if (mode == USE_RANDOM_MDS) |
| 913 | goto random; |
| 914 | |
| 915 | inode = NULL; |
| 916 | if (req->r_inode) { |
Yan, Zheng | 5d37ca1 | 2017-07-26 12:48:08 +0800 | [diff] [blame] | 917 | if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) { |
| 918 | inode = req->r_inode; |
| 919 | ihold(inode); |
| 920 | } else { |
Yan, Zheng | 38f340c | 2017-09-22 11:41:06 +0800 | [diff] [blame] | 921 | /* req->r_dentry is non-null for LSSNAP request */ |
| 922 | rcu_read_lock(); |
| 923 | inode = get_nonsnap_parent(req->r_dentry); |
| 924 | rcu_read_unlock(); |
Xiubo Li | 3c80209 | 2020-01-01 22:09:37 -0500 | [diff] [blame] | 925 | dout("%s using snapdir's parent %p\n", __func__, inode); |
Yan, Zheng | 5d37ca1 | 2017-07-26 12:48:08 +0800 | [diff] [blame] | 926 | } |
Yan, Zheng | 38f340c | 2017-09-22 11:41:06 +0800 | [diff] [blame] | 927 | } else if (req->r_dentry) { |
Sage Weil | d79698d | 2011-07-26 11:31:26 -0700 | [diff] [blame] | 928 | /* ignore race with rename; old or new d_parent is okay */ |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 929 | struct dentry *parent; |
| 930 | struct inode *dir; |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 931 | |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 932 | rcu_read_lock(); |
Yan, Zheng | 41883ba | 2019-05-23 10:22:55 +0800 | [diff] [blame] | 933 | parent = READ_ONCE(req->r_dentry->d_parent); |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 934 | dir = req->r_parent ? : d_inode_rcu(parent); |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 935 | |
| 936 | if (!dir || dir->i_sb != mdsc->fsc->sb) { |
| 937 | /* not this fs or parent went negative */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 938 | inode = d_inode(req->r_dentry); |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 939 | if (inode) |
| 940 | ihold(inode); |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 941 | } else if (ceph_snap(dir) != CEPH_NOSNAP) { |
| 942 | /* direct snapped/virtual snapdir requests |
| 943 | * based on parent dir inode */ |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 944 | inode = get_nonsnap_parent(parent); |
Xiubo Li | 3c80209 | 2020-01-01 22:09:37 -0500 | [diff] [blame] | 945 | dout("%s using nonsnap parent %p\n", __func__, inode); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 946 | } else { |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 947 | /* dentry target */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 948 | inode = d_inode(req->r_dentry); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 949 | if (!inode || mode == USE_AUTH_MDS) { |
| 950 | /* dir + name */ |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 951 | inode = igrab(dir); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 952 | hash = ceph_dentry_hash(dir, req->r_dentry); |
| 953 | is_hash = true; |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 954 | } else { |
| 955 | ihold(inode); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 956 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 957 | } |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 958 | rcu_read_unlock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 959 | } |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 960 | |
Xiubo Li | 3c80209 | 2020-01-01 22:09:37 -0500 | [diff] [blame] | 961 | dout("%s %p is_hash=%d (0x%x) mode %d\n", __func__, inode, (int)is_hash, |
| 962 | hash, mode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 963 | if (!inode) |
| 964 | goto random; |
| 965 | ci = ceph_inode(inode); |
| 966 | |
| 967 | if (is_hash && S_ISDIR(inode->i_mode)) { |
| 968 | struct ceph_inode_frag frag; |
| 969 | int found; |
| 970 | |
| 971 | ceph_choose_frag(ci, hash, &frag, &found); |
| 972 | if (found) { |
| 973 | if (mode == USE_ANY_MDS && frag.ndist > 0) { |
| 974 | u8 r; |
| 975 | |
| 976 | /* choose a random replica */ |
| 977 | get_random_bytes(&r, 1); |
| 978 | r %= frag.ndist; |
| 979 | mds = frag.dist[r]; |
Xiubo Li | 3c80209 | 2020-01-01 22:09:37 -0500 | [diff] [blame] | 980 | dout("%s %p %llx.%llx frag %u mds%d (%d/%d)\n", |
| 981 | __func__, inode, ceph_vinop(inode), |
| 982 | frag.frag, mds, (int)r, frag.ndist); |
Sage Weil | d66bbd4 | 2011-01-21 21:16:46 -0800 | [diff] [blame] | 983 | if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >= |
Xiubo Li | 5d47648 | 2019-11-26 07:24:22 -0500 | [diff] [blame] | 984 | CEPH_MDS_STATE_ACTIVE && |
| 985 | !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds)) |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 986 | goto out; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* since this file/dir wasn't known to be |
| 990 | * replicated, then we want to look for the |
| 991 | * authoritative mds. */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 992 | if (frag.mds >= 0) { |
| 993 | /* choose auth mds */ |
| 994 | mds = frag.mds; |
Xiubo Li | 3c80209 | 2020-01-01 22:09:37 -0500 | [diff] [blame] | 995 | dout("%s %p %llx.%llx frag %u mds%d (auth)\n", |
| 996 | __func__, inode, ceph_vinop(inode), |
| 997 | frag.frag, mds); |
Sage Weil | d66bbd4 | 2011-01-21 21:16:46 -0800 | [diff] [blame] | 998 | if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >= |
Xiubo Li | 5d47648 | 2019-11-26 07:24:22 -0500 | [diff] [blame] | 999 | CEPH_MDS_STATE_ACTIVE) { |
| 1000 | if (mode == USE_ANY_MDS && |
| 1001 | !ceph_mdsmap_is_laggy(mdsc->mdsmap, |
| 1002 | mds)) |
| 1003 | goto out; |
| 1004 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1005 | } |
Xiubo Li | 5d47648 | 2019-11-26 07:24:22 -0500 | [diff] [blame] | 1006 | mode = USE_AUTH_MDS; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1010 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1011 | cap = NULL; |
| 1012 | if (mode == USE_AUTH_MDS) |
| 1013 | cap = ci->i_auth_cap; |
| 1014 | if (!cap && !RB_EMPTY_ROOT(&ci->i_caps)) |
| 1015 | cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node); |
| 1016 | if (!cap) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1017 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 1018 | ceph_async_iput(inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1019 | goto random; |
| 1020 | } |
| 1021 | mds = cap->session->s_mds; |
Xiubo Li | 3c80209 | 2020-01-01 22:09:37 -0500 | [diff] [blame] | 1022 | dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1023 | inode, ceph_vinop(inode), mds, |
| 1024 | cap == ci->i_auth_cap ? "auth " : "", cap); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1025 | spin_unlock(&ci->i_ceph_lock); |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 1026 | out: |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 1027 | /* avoid calling iput_final() while holding mdsc->mutex or |
| 1028 | * in mds dispatch threads */ |
| 1029 | ceph_async_iput(inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1030 | return mds; |
| 1031 | |
| 1032 | random: |
Xiubo Li | c4853e9 | 2019-12-09 07:47:15 -0500 | [diff] [blame] | 1033 | if (random) |
| 1034 | *random = true; |
| 1035 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1036 | mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap); |
Xiubo Li | 3c80209 | 2020-01-01 22:09:37 -0500 | [diff] [blame] | 1037 | dout("%s chose random mds%d\n", __func__, mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1038 | return mds; |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | /* |
| 1043 | * session messages |
| 1044 | */ |
| 1045 | static struct ceph_msg *create_session_msg(u32 op, u64 seq) |
| 1046 | { |
| 1047 | struct ceph_msg *msg; |
| 1048 | struct ceph_mds_session_head *h; |
| 1049 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 1050 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS, |
| 1051 | false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1052 | if (!msg) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1053 | pr_err("create_session_msg ENOMEM creating msg\n"); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1054 | return NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1055 | } |
| 1056 | h = msg->front.iov_base; |
| 1057 | h->op = cpu_to_le32(op); |
| 1058 | h->seq = cpu_to_le64(seq); |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1059 | |
| 1060 | return msg; |
| 1061 | } |
| 1062 | |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 1063 | static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED; |
| 1064 | #define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8) |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1065 | static void encode_supported_features(void **p, void *end) |
| 1066 | { |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 1067 | static const size_t count = ARRAY_SIZE(feature_bits); |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1068 | |
| 1069 | if (count > 0) { |
| 1070 | size_t i; |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 1071 | size_t size = FEATURE_BYTES(count); |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1072 | |
| 1073 | BUG_ON(*p + 4 + size > end); |
| 1074 | ceph_encode_32(p, size); |
| 1075 | memset(*p, 0, size); |
| 1076 | for (i = 0; i < count; i++) |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 1077 | ((unsigned char*)(*p))[i / 8] |= BIT(feature_bits[i] % 8); |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1078 | *p += size; |
| 1079 | } else { |
| 1080 | BUG_ON(*p + 4 > end); |
| 1081 | ceph_encode_32(p, 0); |
| 1082 | } |
| 1083 | } |
| 1084 | |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1085 | /* |
| 1086 | * session message, specialization for CEPH_SESSION_REQUEST_OPEN |
| 1087 | * to include additional client metadata fields. |
| 1088 | */ |
| 1089 | static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq) |
| 1090 | { |
| 1091 | struct ceph_msg *msg; |
| 1092 | struct ceph_mds_session_head *h; |
| 1093 | int i = -1; |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1094 | int extra_bytes = 0; |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1095 | int metadata_key_count = 0; |
| 1096 | struct ceph_options *opt = mdsc->fsc->client->options; |
Yan, Zheng | 3f38495 | 2016-04-21 11:09:55 +0800 | [diff] [blame] | 1097 | struct ceph_mount_options *fsopt = mdsc->fsc->mount_options; |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 1098 | size_t size, count; |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1099 | void *p, *end; |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1100 | |
Yan, Zheng | a6a5ce4 | 2015-01-16 10:54:43 +0800 | [diff] [blame] | 1101 | const char* metadata[][2] = { |
Yan, Zheng | 717e6f2 | 2017-09-11 12:10:08 +0800 | [diff] [blame] | 1102 | {"hostname", mdsc->nodename}, |
| 1103 | {"kernel_version", init_utsname()->release}, |
Yan, Zheng | 3f38495 | 2016-04-21 11:09:55 +0800 | [diff] [blame] | 1104 | {"entity_id", opt->name ? : ""}, |
| 1105 | {"root", fsopt->server_path ? : "/"}, |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1106 | {NULL, NULL} |
| 1107 | }; |
| 1108 | |
| 1109 | /* Calculate serialized length of metadata */ |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1110 | extra_bytes = 4; /* map length */ |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1111 | for (i = 0; metadata[i][0]; ++i) { |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1112 | extra_bytes += 8 + strlen(metadata[i][0]) + |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1113 | strlen(metadata[i][1]); |
| 1114 | metadata_key_count++; |
| 1115 | } |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 1116 | |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1117 | /* supported feature */ |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 1118 | size = 0; |
| 1119 | count = ARRAY_SIZE(feature_bits); |
| 1120 | if (count > 0) |
| 1121 | size = FEATURE_BYTES(count); |
| 1122 | extra_bytes += 4 + size; |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1123 | |
| 1124 | /* Allocate the message */ |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1125 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes, |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1126 | GFP_NOFS, false); |
| 1127 | if (!msg) { |
| 1128 | pr_err("create_session_msg ENOMEM creating msg\n"); |
| 1129 | return NULL; |
| 1130 | } |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1131 | p = msg->front.iov_base; |
| 1132 | end = p + msg->front.iov_len; |
| 1133 | |
| 1134 | h = p; |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1135 | h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN); |
| 1136 | h->seq = cpu_to_le64(seq); |
| 1137 | |
| 1138 | /* |
| 1139 | * Serialize client metadata into waiting buffer space, using |
| 1140 | * the format that userspace expects for map<string, string> |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 1141 | * |
Xiubo Li | 9ba1e22 | 2020-01-08 05:17:31 -0500 | [diff] [blame] | 1142 | * ClientSession messages with metadata are v3 |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1143 | */ |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1144 | msg->hdr.version = cpu_to_le16(3); |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 1145 | msg->hdr.compat_version = cpu_to_le16(1); |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1146 | |
| 1147 | /* The write pointer, following the session_head structure */ |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1148 | p += sizeof(*h); |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1149 | |
| 1150 | /* Number of entries in the map */ |
| 1151 | ceph_encode_32(&p, metadata_key_count); |
| 1152 | |
| 1153 | /* Two length-prefixed strings for each entry in the map */ |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1154 | for (i = 0; metadata[i][0]; ++i) { |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1155 | size_t const key_len = strlen(metadata[i][0]); |
| 1156 | size_t const val_len = strlen(metadata[i][1]); |
| 1157 | |
| 1158 | ceph_encode_32(&p, key_len); |
| 1159 | memcpy(p, metadata[i][0], key_len); |
| 1160 | p += key_len; |
| 1161 | ceph_encode_32(&p, val_len); |
| 1162 | memcpy(p, metadata[i][1], val_len); |
| 1163 | p += val_len; |
| 1164 | } |
| 1165 | |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 1166 | encode_supported_features(&p, end); |
| 1167 | msg->front.iov_len = p - msg->front.iov_base; |
| 1168 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 1169 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1170 | return msg; |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * send session open request. |
| 1175 | * |
| 1176 | * called under mdsc->mutex |
| 1177 | */ |
| 1178 | static int __open_session(struct ceph_mds_client *mdsc, |
| 1179 | struct ceph_mds_session *session) |
| 1180 | { |
| 1181 | struct ceph_msg *msg; |
| 1182 | int mstate; |
| 1183 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1184 | |
| 1185 | /* wait for mds to go active? */ |
| 1186 | mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds); |
| 1187 | dout("open_session to mds%d (%s)\n", mds, |
| 1188 | ceph_mds_state_name(mstate)); |
| 1189 | session->s_state = CEPH_MDS_SESSION_OPENING; |
| 1190 | session->s_renew_requested = jiffies; |
| 1191 | |
| 1192 | /* send connect message */ |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1193 | msg = create_session_open_msg(mdsc, session->s_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1194 | if (!msg) |
| 1195 | return -ENOMEM; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1196 | ceph_con_send(&session->s_con, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | /* |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1201 | * open sessions for any export targets for the given mds |
| 1202 | * |
| 1203 | * called under mdsc->mutex |
| 1204 | */ |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 1205 | static struct ceph_mds_session * |
| 1206 | __open_export_target_session(struct ceph_mds_client *mdsc, int target) |
| 1207 | { |
| 1208 | struct ceph_mds_session *session; |
| 1209 | |
| 1210 | session = __ceph_lookup_mds_session(mdsc, target); |
| 1211 | if (!session) { |
| 1212 | session = register_session(mdsc, target); |
| 1213 | if (IS_ERR(session)) |
| 1214 | return session; |
| 1215 | } |
| 1216 | if (session->s_state == CEPH_MDS_SESSION_NEW || |
| 1217 | session->s_state == CEPH_MDS_SESSION_CLOSING) |
| 1218 | __open_session(mdsc, session); |
| 1219 | |
| 1220 | return session; |
| 1221 | } |
| 1222 | |
| 1223 | struct ceph_mds_session * |
| 1224 | ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target) |
| 1225 | { |
| 1226 | struct ceph_mds_session *session; |
| 1227 | |
| 1228 | dout("open_export_target_session to mds%d\n", target); |
| 1229 | |
| 1230 | mutex_lock(&mdsc->mutex); |
| 1231 | session = __open_export_target_session(mdsc, target); |
| 1232 | mutex_unlock(&mdsc->mutex); |
| 1233 | |
| 1234 | return session; |
| 1235 | } |
| 1236 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1237 | static void __open_export_target_sessions(struct ceph_mds_client *mdsc, |
| 1238 | struct ceph_mds_session *session) |
| 1239 | { |
| 1240 | struct ceph_mds_info *mi; |
| 1241 | struct ceph_mds_session *ts; |
| 1242 | int i, mds = session->s_mds; |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1243 | |
Xiubo Li | b38c9eb | 2019-12-04 06:57:39 -0500 | [diff] [blame] | 1244 | if (mds >= mdsc->mdsmap->possible_max_rank) |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1245 | return; |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 1246 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1247 | mi = &mdsc->mdsmap->m_info[mds]; |
| 1248 | dout("open_export_target_sessions for mds%d (%d targets)\n", |
| 1249 | session->s_mds, mi->num_export_targets); |
| 1250 | |
| 1251 | for (i = 0; i < mi->num_export_targets; i++) { |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 1252 | ts = __open_export_target_session(mdsc, mi->export_targets[i]); |
| 1253 | if (!IS_ERR(ts)) |
| 1254 | ceph_put_mds_session(ts); |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 1258 | void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc, |
| 1259 | struct ceph_mds_session *session) |
| 1260 | { |
| 1261 | mutex_lock(&mdsc->mutex); |
| 1262 | __open_export_target_sessions(mdsc, session); |
| 1263 | mutex_unlock(&mdsc->mutex); |
| 1264 | } |
| 1265 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1266 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1267 | * session caps |
| 1268 | */ |
| 1269 | |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1270 | static void detach_cap_releases(struct ceph_mds_session *session, |
| 1271 | struct list_head *target) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1272 | { |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1273 | lockdep_assert_held(&session->s_cap_lock); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1274 | |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1275 | list_splice_init(&session->s_cap_releases, target); |
| 1276 | session->s_num_cap_releases = 0; |
| 1277 | dout("dispose_cap_releases mds%d\n", session->s_mds); |
| 1278 | } |
| 1279 | |
| 1280 | static void dispose_cap_releases(struct ceph_mds_client *mdsc, |
| 1281 | struct list_head *dispose) |
| 1282 | { |
| 1283 | while (!list_empty(dispose)) { |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1284 | struct ceph_cap *cap; |
| 1285 | /* zero out the in-progress message */ |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1286 | cap = list_first_entry(dispose, struct ceph_cap, session_caps); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1287 | list_del(&cap->session_caps); |
| 1288 | ceph_put_cap(mdsc, cap); |
| 1289 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Yan, Zheng | 1c841a9 | 2015-03-24 20:15:36 +0800 | [diff] [blame] | 1292 | static void cleanup_session_requests(struct ceph_mds_client *mdsc, |
| 1293 | struct ceph_mds_session *session) |
| 1294 | { |
| 1295 | struct ceph_mds_request *req; |
| 1296 | struct rb_node *p; |
Yan, Zheng | f4b9786 | 2019-07-25 20:16:42 +0800 | [diff] [blame] | 1297 | struct ceph_inode_info *ci; |
Yan, Zheng | 1c841a9 | 2015-03-24 20:15:36 +0800 | [diff] [blame] | 1298 | |
| 1299 | dout("cleanup_session_requests mds%d\n", session->s_mds); |
| 1300 | mutex_lock(&mdsc->mutex); |
| 1301 | while (!list_empty(&session->s_unsafe)) { |
| 1302 | req = list_first_entry(&session->s_unsafe, |
| 1303 | struct ceph_mds_request, r_unsafe_item); |
Yan, Zheng | 3e0708b | 2015-05-22 16:38:02 +0800 | [diff] [blame] | 1304 | pr_warn_ratelimited(" dropping unsafe request %llu\n", |
| 1305 | req->r_tid); |
Yan, Zheng | f4b9786 | 2019-07-25 20:16:42 +0800 | [diff] [blame] | 1306 | if (req->r_target_inode) { |
| 1307 | /* dropping unsafe change of inode's attributes */ |
| 1308 | ci = ceph_inode(req->r_target_inode); |
| 1309 | errseq_set(&ci->i_meta_err, -EIO); |
| 1310 | } |
| 1311 | if (req->r_unsafe_dir) { |
| 1312 | /* dropping unsafe directory operation */ |
| 1313 | ci = ceph_inode(req->r_unsafe_dir); |
| 1314 | errseq_set(&ci->i_meta_err, -EIO); |
| 1315 | } |
Yan, Zheng | 1c841a9 | 2015-03-24 20:15:36 +0800 | [diff] [blame] | 1316 | __unregister_request(mdsc, req); |
| 1317 | } |
| 1318 | /* zero r_attempts, so kick_requests() will re-send requests */ |
| 1319 | p = rb_first(&mdsc->request_tree); |
| 1320 | while (p) { |
| 1321 | req = rb_entry(p, struct ceph_mds_request, r_node); |
| 1322 | p = rb_next(p); |
| 1323 | if (req->r_session && |
| 1324 | req->r_session->s_mds == session->s_mds) |
| 1325 | req->r_attempts = 0; |
| 1326 | } |
| 1327 | mutex_unlock(&mdsc->mutex); |
| 1328 | } |
| 1329 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1330 | /* |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1331 | * Helper to safely iterate over all caps associated with a session, with |
| 1332 | * special care taken to handle a racing __ceph_remove_cap(). |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1333 | * |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1334 | * Caller must hold session s_mutex. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1335 | */ |
Jeff Layton | f5d77269 | 2019-04-24 12:09:04 -0400 | [diff] [blame] | 1336 | int ceph_iterate_session_caps(struct ceph_mds_session *session, |
| 1337 | int (*cb)(struct inode *, struct ceph_cap *, |
| 1338 | void *), void *arg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1339 | { |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1340 | struct list_head *p; |
| 1341 | struct ceph_cap *cap; |
| 1342 | struct inode *inode, *last_inode = NULL; |
| 1343 | struct ceph_cap *old_cap = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1344 | int ret; |
| 1345 | |
| 1346 | dout("iterate_session_caps %p mds%d\n", session, session->s_mds); |
| 1347 | spin_lock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1348 | p = session->s_caps.next; |
| 1349 | while (p != &session->s_caps) { |
| 1350 | cap = list_entry(p, struct ceph_cap, session_caps); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1351 | inode = igrab(&cap->ci->vfs_inode); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1352 | if (!inode) { |
| 1353 | p = p->next; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1354 | continue; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1355 | } |
| 1356 | session->s_cap_iterator = cap; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1357 | spin_unlock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1358 | |
| 1359 | if (last_inode) { |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 1360 | /* avoid calling iput_final() while holding |
| 1361 | * s_mutex or in mds dispatch threads */ |
| 1362 | ceph_async_iput(last_inode); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1363 | last_inode = NULL; |
| 1364 | } |
| 1365 | if (old_cap) { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1366 | ceph_put_cap(session->s_mdsc, old_cap); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1367 | old_cap = NULL; |
| 1368 | } |
| 1369 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1370 | ret = cb(inode, cap, arg); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1371 | last_inode = inode; |
| 1372 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1373 | spin_lock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1374 | p = p->next; |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1375 | if (!cap->ci) { |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1376 | dout("iterate_session_caps finishing cap %p removal\n", |
| 1377 | cap); |
| 1378 | BUG_ON(cap->session != session); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1379 | cap->session = NULL; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1380 | list_del_init(&cap->session_caps); |
| 1381 | session->s_nr_caps--; |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 1382 | if (cap->queue_release) |
| 1383 | __ceph_queue_cap_release(session, cap); |
| 1384 | else |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1385 | old_cap = cap; /* put_cap it w/o locks held */ |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1386 | } |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1387 | if (ret < 0) |
| 1388 | goto out; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1389 | } |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1390 | ret = 0; |
| 1391 | out: |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1392 | session->s_cap_iterator = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1393 | spin_unlock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1394 | |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 1395 | ceph_async_iput(last_inode); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1396 | if (old_cap) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1397 | ceph_put_cap(session->s_mdsc, old_cap); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1398 | |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1399 | return ret; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap, |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1403 | void *arg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1404 | { |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1405 | struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1406 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1407 | LIST_HEAD(to_remove); |
Yan, Zheng | f4b9786 | 2019-07-25 20:16:42 +0800 | [diff] [blame] | 1408 | bool dirty_dropped = false; |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1409 | bool invalidate = false; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1410 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1411 | dout("removing cap %p, ci is %p, inode is %p\n", |
| 1412 | cap, ci, &ci->vfs_inode); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1413 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1414 | if (cap->mds_wanted | cap->issued) |
| 1415 | ci->i_ceph_flags |= CEPH_I_CAP_DROPPED; |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1416 | __ceph_remove_cap(cap, false); |
Yan, Zheng | 571ade3 | 2015-03-24 11:36:08 +0800 | [diff] [blame] | 1417 | if (!ci->i_auth_cap) { |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1418 | struct ceph_cap_flush *cf; |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1419 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1420 | |
Yan, Zheng | d468e72 | 2019-07-25 20:16:44 +0800 | [diff] [blame] | 1421 | if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
| 1422 | if (inode->i_data.nrpages > 0) |
| 1423 | invalidate = true; |
| 1424 | if (ci->i_wrbuffer_ref > 0) |
| 1425 | mapping_set_error(&inode->i_data, -EIO); |
| 1426 | } |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1427 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1428 | while (!list_empty(&ci->i_cap_flush_list)) { |
| 1429 | cf = list_first_entry(&ci->i_cap_flush_list, |
| 1430 | struct ceph_cap_flush, i_list); |
Wei Yongjun | 8cdcc07 | 2016-08-13 09:10:28 +0000 | [diff] [blame] | 1431 | list_move(&cf->i_list, &to_remove); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1434 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1435 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1436 | list_for_each_entry(cf, &to_remove, i_list) |
| 1437 | list_del(&cf->g_list); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1438 | |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1439 | if (!list_empty(&ci->i_dirty_item)) { |
Yan, Zheng | 3e0708b | 2015-05-22 16:38:02 +0800 | [diff] [blame] | 1440 | pr_warn_ratelimited( |
| 1441 | " dropping dirty %s state for %p %lld\n", |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1442 | ceph_cap_string(ci->i_dirty_caps), |
| 1443 | inode, ceph_ino(inode)); |
| 1444 | ci->i_dirty_caps = 0; |
| 1445 | list_del_init(&ci->i_dirty_item); |
Yan, Zheng | f4b9786 | 2019-07-25 20:16:42 +0800 | [diff] [blame] | 1446 | dirty_dropped = true; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1447 | } |
| 1448 | if (!list_empty(&ci->i_flushing_item)) { |
Yan, Zheng | 3e0708b | 2015-05-22 16:38:02 +0800 | [diff] [blame] | 1449 | pr_warn_ratelimited( |
| 1450 | " dropping dirty+flushing %s state for %p %lld\n", |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1451 | ceph_cap_string(ci->i_flushing_caps), |
| 1452 | inode, ceph_ino(inode)); |
| 1453 | ci->i_flushing_caps = 0; |
| 1454 | list_del_init(&ci->i_flushing_item); |
| 1455 | mdsc->num_cap_flushing--; |
Yan, Zheng | f4b9786 | 2019-07-25 20:16:42 +0800 | [diff] [blame] | 1456 | dirty_dropped = true; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1457 | } |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1458 | spin_unlock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1459 | |
Yan, Zheng | f4b9786 | 2019-07-25 20:16:42 +0800 | [diff] [blame] | 1460 | if (dirty_dropped) { |
| 1461 | errseq_set(&ci->i_meta_err, -EIO); |
| 1462 | |
| 1463 | if (ci->i_wrbuffer_ref_head == 0 && |
| 1464 | ci->i_wr_ref == 0 && |
| 1465 | ci->i_dirty_caps == 0 && |
| 1466 | ci->i_flushing_caps == 0) { |
| 1467 | ceph_put_snap_context(ci->i_head_snapc); |
| 1468 | ci->i_head_snapc = NULL; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Yan, Zheng | b3f8d68 | 2017-09-11 10:58:55 +0800 | [diff] [blame] | 1472 | if (atomic_read(&ci->i_filelock_ref) > 0) { |
| 1473 | /* make further file lock syscall return -EIO */ |
| 1474 | ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK; |
| 1475 | pr_warn_ratelimited(" dropping file locks for %p %lld\n", |
| 1476 | inode, ceph_ino(inode)); |
| 1477 | } |
| 1478 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1479 | if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) { |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1480 | list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1481 | ci->i_prealloc_cap_flush = NULL; |
| 1482 | } |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1483 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1484 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1485 | while (!list_empty(&to_remove)) { |
| 1486 | struct ceph_cap_flush *cf; |
| 1487 | cf = list_first_entry(&to_remove, |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1488 | struct ceph_cap_flush, i_list); |
| 1489 | list_del(&cf->i_list); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1490 | ceph_free_cap_flush(cf); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1491 | } |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 1492 | |
| 1493 | wake_up_all(&ci->i_cap_wq); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1494 | if (invalidate) |
| 1495 | ceph_queue_invalidate(inode); |
Yan, Zheng | f4b9786 | 2019-07-25 20:16:42 +0800 | [diff] [blame] | 1496 | if (dirty_dropped) |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1497 | iput(inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * caller must hold session s_mutex |
| 1503 | */ |
| 1504 | static void remove_session_caps(struct ceph_mds_session *session) |
| 1505 | { |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1506 | struct ceph_fs_client *fsc = session->s_mdsc->fsc; |
| 1507 | struct super_block *sb = fsc->sb; |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1508 | LIST_HEAD(dispose); |
| 1509 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1510 | dout("remove_session_caps on %p\n", session); |
Jeff Layton | f5d77269 | 2019-04-24 12:09:04 -0400 | [diff] [blame] | 1511 | ceph_iterate_session_caps(session, remove_session_caps_cb, fsc); |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1512 | |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 1513 | wake_up_all(&fsc->mdsc->cap_flushing_wq); |
| 1514 | |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1515 | spin_lock(&session->s_cap_lock); |
| 1516 | if (session->s_nr_caps > 0) { |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1517 | struct inode *inode; |
| 1518 | struct ceph_cap *cap, *prev = NULL; |
| 1519 | struct ceph_vino vino; |
| 1520 | /* |
| 1521 | * iterate_session_caps() skips inodes that are being |
| 1522 | * deleted, we need to wait until deletions are complete. |
| 1523 | * __wait_on_freeing_inode() is designed for the job, |
| 1524 | * but it is not exported, so use lookup inode function |
| 1525 | * to access it. |
| 1526 | */ |
| 1527 | while (!list_empty(&session->s_caps)) { |
| 1528 | cap = list_entry(session->s_caps.next, |
| 1529 | struct ceph_cap, session_caps); |
| 1530 | if (cap == prev) |
| 1531 | break; |
| 1532 | prev = cap; |
| 1533 | vino = cap->ci->i_vino; |
| 1534 | spin_unlock(&session->s_cap_lock); |
| 1535 | |
Yan, Zheng | ed284c4 | 2013-09-02 15:19:53 +0800 | [diff] [blame] | 1536 | inode = ceph_find_inode(sb, vino); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 1537 | /* avoid calling iput_final() while holding s_mutex */ |
| 1538 | ceph_async_iput(inode); |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1539 | |
| 1540 | spin_lock(&session->s_cap_lock); |
| 1541 | } |
| 1542 | } |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1543 | |
| 1544 | // drop cap expires and unlock s_cap_lock |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1545 | detach_cap_releases(session, &dispose); |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1546 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1547 | BUG_ON(session->s_nr_caps > 0); |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1548 | BUG_ON(!list_empty(&session->s_cap_flushing)); |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1549 | spin_unlock(&session->s_cap_lock); |
| 1550 | dispose_cap_releases(session->s_mdsc, &dispose); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1553 | enum { |
| 1554 | RECONNECT, |
| 1555 | RENEWCAPS, |
| 1556 | FORCE_RO, |
| 1557 | }; |
| 1558 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1559 | /* |
| 1560 | * wake up any threads waiting on this session's caps. if the cap is |
| 1561 | * old (didn't get renewed on the client reconnect), remove it now. |
| 1562 | * |
| 1563 | * caller must hold s_mutex. |
| 1564 | */ |
| 1565 | static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap, |
| 1566 | void *arg) |
| 1567 | { |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1568 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1569 | unsigned long ev = (unsigned long)arg; |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1570 | |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1571 | if (ev == RECONNECT) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1572 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1573 | ci->i_wanted_max_size = 0; |
| 1574 | ci->i_requested_max_size = 0; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1575 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1576 | } else if (ev == RENEWCAPS) { |
| 1577 | if (cap->cap_gen < cap->session->s_cap_gen) { |
| 1578 | /* mds did not re-issue stale cap */ |
| 1579 | spin_lock(&ci->i_ceph_lock); |
| 1580 | cap->issued = cap->implemented = CEPH_CAP_PIN; |
| 1581 | /* make sure mds knows what we want */ |
| 1582 | if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted) |
| 1583 | ci->i_ceph_flags |= CEPH_I_CAP_DROPPED; |
| 1584 | spin_unlock(&ci->i_ceph_lock); |
| 1585 | } |
| 1586 | } else if (ev == FORCE_RO) { |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1587 | } |
Yan, Zheng | e536030 | 2016-05-19 19:15:19 +0800 | [diff] [blame] | 1588 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1589 | return 0; |
| 1590 | } |
| 1591 | |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1592 | static void wake_up_session_caps(struct ceph_mds_session *session, int ev) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1593 | { |
| 1594 | dout("wake_up_session_caps %p mds%d\n", session, session->s_mds); |
Jeff Layton | f5d77269 | 2019-04-24 12:09:04 -0400 | [diff] [blame] | 1595 | ceph_iterate_session_caps(session, wake_up_session_cb, |
| 1596 | (void *)(unsigned long)ev); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * Send periodic message to MDS renewing all currently held caps. The |
| 1601 | * ack will reset the expiration for all caps from this session. |
| 1602 | * |
| 1603 | * caller holds s_mutex |
| 1604 | */ |
| 1605 | static int send_renew_caps(struct ceph_mds_client *mdsc, |
| 1606 | struct ceph_mds_session *session) |
| 1607 | { |
| 1608 | struct ceph_msg *msg; |
| 1609 | int state; |
| 1610 | |
| 1611 | if (time_after_eq(jiffies, session->s_cap_ttl) && |
| 1612 | time_after_eq(session->s_cap_ttl, session->s_renew_requested)) |
| 1613 | pr_info("mds%d caps stale\n", session->s_mds); |
Sage Weil | e4cb4cb | 2010-03-18 13:43:09 -0700 | [diff] [blame] | 1614 | session->s_renew_requested = jiffies; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1615 | |
| 1616 | /* do not try to renew caps until a recovering mds has reconnected |
| 1617 | * with its clients. */ |
| 1618 | state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds); |
| 1619 | if (state < CEPH_MDS_STATE_RECONNECT) { |
| 1620 | dout("send_renew_caps ignoring mds%d (%s)\n", |
| 1621 | session->s_mds, ceph_mds_state_name(state)); |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | dout("send_renew_caps to mds%d (%s)\n", session->s_mds, |
| 1626 | ceph_mds_state_name(state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1627 | msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS, |
| 1628 | ++session->s_renew_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1629 | if (!msg) |
| 1630 | return -ENOMEM; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1631 | ceph_con_send(&session->s_con, msg); |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 1635 | static int send_flushmsg_ack(struct ceph_mds_client *mdsc, |
| 1636 | struct ceph_mds_session *session, u64 seq) |
| 1637 | { |
| 1638 | struct ceph_msg *msg; |
| 1639 | |
| 1640 | dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 1641 | session->s_mds, ceph_session_state_name(session->s_state), seq); |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 1642 | msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq); |
| 1643 | if (!msg) |
| 1644 | return -ENOMEM; |
| 1645 | ceph_con_send(&session->s_con, msg); |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1650 | /* |
| 1651 | * Note new cap ttl, and any transition from stale -> not stale (fresh?). |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1652 | * |
| 1653 | * Called under session->s_mutex |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1654 | */ |
| 1655 | static void renewed_caps(struct ceph_mds_client *mdsc, |
| 1656 | struct ceph_mds_session *session, int is_renew) |
| 1657 | { |
| 1658 | int was_stale; |
| 1659 | int wake = 0; |
| 1660 | |
| 1661 | spin_lock(&session->s_cap_lock); |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 1662 | was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1663 | |
| 1664 | session->s_cap_ttl = session->s_renew_requested + |
| 1665 | mdsc->mdsmap->m_session_timeout*HZ; |
| 1666 | |
| 1667 | if (was_stale) { |
| 1668 | if (time_before(jiffies, session->s_cap_ttl)) { |
| 1669 | pr_info("mds%d caps renewed\n", session->s_mds); |
| 1670 | wake = 1; |
| 1671 | } else { |
| 1672 | pr_info("mds%d caps still stale\n", session->s_mds); |
| 1673 | } |
| 1674 | } |
| 1675 | dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n", |
| 1676 | session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh", |
| 1677 | time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh"); |
| 1678 | spin_unlock(&session->s_cap_lock); |
| 1679 | |
| 1680 | if (wake) |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1681 | wake_up_session_caps(session, RENEWCAPS); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * send a session close request |
| 1686 | */ |
| 1687 | static int request_close_session(struct ceph_mds_client *mdsc, |
| 1688 | struct ceph_mds_session *session) |
| 1689 | { |
| 1690 | struct ceph_msg *msg; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1691 | |
| 1692 | dout("request_close_session mds%d state %s seq %lld\n", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 1693 | session->s_mds, ceph_session_state_name(session->s_state), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1694 | session->s_seq); |
| 1695 | msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1696 | if (!msg) |
| 1697 | return -ENOMEM; |
| 1698 | ceph_con_send(&session->s_con, msg); |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 1699 | return 1; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | /* |
| 1703 | * Called with s_mutex held. |
| 1704 | */ |
| 1705 | static int __close_session(struct ceph_mds_client *mdsc, |
| 1706 | struct ceph_mds_session *session) |
| 1707 | { |
| 1708 | if (session->s_state >= CEPH_MDS_SESSION_CLOSING) |
| 1709 | return 0; |
| 1710 | session->s_state = CEPH_MDS_SESSION_CLOSING; |
| 1711 | return request_close_session(mdsc, session); |
| 1712 | } |
| 1713 | |
Yan, Zheng | 040d786 | 2017-11-30 11:59:22 +0800 | [diff] [blame] | 1714 | static bool drop_negative_children(struct dentry *dentry) |
| 1715 | { |
| 1716 | struct dentry *child; |
| 1717 | bool all_negative = true; |
| 1718 | |
| 1719 | if (!d_is_dir(dentry)) |
| 1720 | goto out; |
| 1721 | |
| 1722 | spin_lock(&dentry->d_lock); |
| 1723 | list_for_each_entry(child, &dentry->d_subdirs, d_child) { |
| 1724 | if (d_really_is_positive(child)) { |
| 1725 | all_negative = false; |
| 1726 | break; |
| 1727 | } |
| 1728 | } |
| 1729 | spin_unlock(&dentry->d_lock); |
| 1730 | |
| 1731 | if (all_negative) |
| 1732 | shrink_dcache_parent(dentry); |
| 1733 | out: |
| 1734 | return all_negative; |
| 1735 | } |
| 1736 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1737 | /* |
| 1738 | * Trim old(er) caps. |
| 1739 | * |
| 1740 | * Because we can't cache an inode without one or more caps, we do |
| 1741 | * this indirectly: if a cap is unused, we prune its aliases, at which |
| 1742 | * point the inode will hopefully get dropped to. |
| 1743 | * |
| 1744 | * Yes, this is a bit sloppy. Our only real goal here is to respond to |
| 1745 | * memory pressure from the MDS, though, so it needn't be perfect. |
| 1746 | */ |
| 1747 | static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg) |
| 1748 | { |
Jeff Layton | 533a281 | 2019-07-19 15:22:28 -0400 | [diff] [blame] | 1749 | int *remaining = arg; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1750 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1751 | int used, wanted, oissued, mine; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1752 | |
Jeff Layton | 533a281 | 2019-07-19 15:22:28 -0400 | [diff] [blame] | 1753 | if (*remaining <= 0) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1754 | return -1; |
| 1755 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1756 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1757 | mine = cap->issued | cap->implemented; |
| 1758 | used = __ceph_caps_used(ci); |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1759 | wanted = __ceph_caps_file_wanted(ci); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1760 | oissued = __ceph_caps_issued_other(ci, cap); |
| 1761 | |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1762 | dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1763 | inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued), |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1764 | ceph_cap_string(used), ceph_cap_string(wanted)); |
| 1765 | if (cap == ci->i_auth_cap) { |
Yan, Zheng | 622f3e2 | 2015-05-07 10:59:47 +0800 | [diff] [blame] | 1766 | if (ci->i_dirty_caps || ci->i_flushing_caps || |
| 1767 | !list_empty(&ci->i_cap_snaps)) |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1768 | goto out; |
| 1769 | if ((used | wanted) & CEPH_CAP_ANY_WR) |
| 1770 | goto out; |
Yan, Zheng | 89aa593 | 2017-09-08 15:23:18 +0800 | [diff] [blame] | 1771 | /* Note: it's possible that i_filelock_ref becomes non-zero |
| 1772 | * after dropping auth caps. It doesn't hurt because reply |
| 1773 | * of lock mds request will re-add auth caps. */ |
| 1774 | if (atomic_read(&ci->i_filelock_ref) > 0) |
| 1775 | goto out; |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1776 | } |
Yan, Zheng | 5e804ac | 2015-10-26 16:08:43 +0800 | [diff] [blame] | 1777 | /* The inode has cached pages, but it's no longer used. |
| 1778 | * we can safely drop it */ |
Yan, Zheng | 525d15e | 2019-05-11 17:27:59 +0800 | [diff] [blame] | 1779 | if (S_ISREG(inode->i_mode) && |
| 1780 | wanted == 0 && used == CEPH_CAP_FILE_CACHE && |
Yan, Zheng | 5e804ac | 2015-10-26 16:08:43 +0800 | [diff] [blame] | 1781 | !(oissued & CEPH_CAP_FILE_CACHE)) { |
| 1782 | used = 0; |
| 1783 | oissued = 0; |
| 1784 | } |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1785 | if ((used | wanted) & ~oissued & mine) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1786 | goto out; /* we need these caps */ |
| 1787 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1788 | if (oissued) { |
| 1789 | /* we aren't the only cap.. just remove us */ |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1790 | __ceph_remove_cap(cap, true); |
Jeff Layton | 533a281 | 2019-07-19 15:22:28 -0400 | [diff] [blame] | 1791 | (*remaining)--; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1792 | } else { |
Yan, Zheng | 040d786 | 2017-11-30 11:59:22 +0800 | [diff] [blame] | 1793 | struct dentry *dentry; |
Yan, Zheng | 5e804ac | 2015-10-26 16:08:43 +0800 | [diff] [blame] | 1794 | /* try dropping referring dentries */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1795 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 040d786 | 2017-11-30 11:59:22 +0800 | [diff] [blame] | 1796 | dentry = d_find_any_alias(inode); |
| 1797 | if (dentry && drop_negative_children(dentry)) { |
| 1798 | int count; |
| 1799 | dput(dentry); |
| 1800 | d_prune_aliases(inode); |
| 1801 | count = atomic_read(&inode->i_count); |
| 1802 | if (count == 1) |
Jeff Layton | 533a281 | 2019-07-19 15:22:28 -0400 | [diff] [blame] | 1803 | (*remaining)--; |
Yan, Zheng | 040d786 | 2017-11-30 11:59:22 +0800 | [diff] [blame] | 1804 | dout("trim_caps_cb %p cap %p pruned, count now %d\n", |
| 1805 | inode, cap, count); |
| 1806 | } else { |
| 1807 | dput(dentry); |
| 1808 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1809 | return 0; |
| 1810 | } |
| 1811 | |
| 1812 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1813 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | /* |
| 1818 | * Trim session cap count down to some max number. |
| 1819 | */ |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 1820 | int ceph_trim_caps(struct ceph_mds_client *mdsc, |
| 1821 | struct ceph_mds_session *session, |
| 1822 | int max_caps) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1823 | { |
| 1824 | int trim_caps = session->s_nr_caps - max_caps; |
| 1825 | |
| 1826 | dout("trim_caps mds%d start: %d / %d, trim %d\n", |
| 1827 | session->s_mds, session->s_nr_caps, max_caps, trim_caps); |
| 1828 | if (trim_caps > 0) { |
Jeff Layton | 533a281 | 2019-07-19 15:22:28 -0400 | [diff] [blame] | 1829 | int remaining = trim_caps; |
| 1830 | |
| 1831 | ceph_iterate_session_caps(session, trim_caps_cb, &remaining); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1832 | dout("trim_caps mds%d done: %d / %d, trimmed %d\n", |
| 1833 | session->s_mds, session->s_nr_caps, max_caps, |
Jeff Layton | 533a281 | 2019-07-19 15:22:28 -0400 | [diff] [blame] | 1834 | trim_caps - remaining); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1835 | } |
Yan, Zheng | a56371d | 2014-04-01 20:34:56 +0800 | [diff] [blame] | 1836 | |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 1837 | ceph_flush_cap_releases(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1838 | return 0; |
| 1839 | } |
| 1840 | |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1841 | static int check_caps_flush(struct ceph_mds_client *mdsc, |
| 1842 | u64 want_flush_tid) |
| 1843 | { |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1844 | int ret = 1; |
| 1845 | |
| 1846 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1847 | if (!list_empty(&mdsc->cap_flush_list)) { |
| 1848 | struct ceph_cap_flush *cf = |
| 1849 | list_first_entry(&mdsc->cap_flush_list, |
| 1850 | struct ceph_cap_flush, g_list); |
| 1851 | if (cf->tid <= want_flush_tid) { |
| 1852 | dout("check_caps_flush still flushing tid " |
| 1853 | "%llu <= %llu\n", cf->tid, want_flush_tid); |
| 1854 | ret = 0; |
| 1855 | } |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1856 | } |
| 1857 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1858 | return ret; |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1859 | } |
| 1860 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1861 | /* |
| 1862 | * flush all dirty inode data to disk. |
| 1863 | * |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1864 | * returns true if we've flushed through want_flush_tid |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1865 | */ |
Yan, Zheng | affbc19 | 2015-05-05 21:22:13 +0800 | [diff] [blame] | 1866 | static void wait_caps_flush(struct ceph_mds_client *mdsc, |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1867 | u64 want_flush_tid) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1868 | { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1869 | dout("check_caps_flush want %llu\n", want_flush_tid); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1870 | |
| 1871 | wait_event(mdsc->cap_flushing_wq, |
| 1872 | check_caps_flush(mdsc, want_flush_tid)); |
| 1873 | |
| 1874 | dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | /* |
| 1878 | * called under s_mutex |
| 1879 | */ |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 1880 | static void ceph_send_cap_releases(struct ceph_mds_client *mdsc, |
| 1881 | struct ceph_mds_session *session) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1882 | { |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1883 | struct ceph_msg *msg = NULL; |
| 1884 | struct ceph_mds_cap_release *head; |
| 1885 | struct ceph_mds_cap_item *item; |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1886 | struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc; |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1887 | struct ceph_cap *cap; |
| 1888 | LIST_HEAD(tmp_list); |
| 1889 | int num_cap_releases; |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1890 | __le32 barrier, *cap_barrier; |
| 1891 | |
| 1892 | down_read(&osdc->lock); |
| 1893 | barrier = cpu_to_le32(osdc->epoch_barrier); |
| 1894 | up_read(&osdc->lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1895 | |
Sage Weil | 0f8605f | 2010-05-05 15:51:35 -0700 | [diff] [blame] | 1896 | spin_lock(&session->s_cap_lock); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1897 | again: |
| 1898 | list_splice_init(&session->s_cap_releases, &tmp_list); |
| 1899 | num_cap_releases = session->s_num_cap_releases; |
| 1900 | session->s_num_cap_releases = 0; |
| 1901 | spin_unlock(&session->s_cap_lock); |
| 1902 | |
| 1903 | while (!list_empty(&tmp_list)) { |
| 1904 | if (!msg) { |
| 1905 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1906 | PAGE_SIZE, GFP_NOFS, false); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1907 | if (!msg) |
| 1908 | goto out_err; |
| 1909 | head = msg->front.iov_base; |
| 1910 | head->num = cpu_to_le32(0); |
| 1911 | msg->front.iov_len = sizeof(*head); |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1912 | |
| 1913 | msg->hdr.version = cpu_to_le16(2); |
| 1914 | msg->hdr.compat_version = cpu_to_le16(1); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1915 | } |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1916 | |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1917 | cap = list_first_entry(&tmp_list, struct ceph_cap, |
| 1918 | session_caps); |
| 1919 | list_del(&cap->session_caps); |
| 1920 | num_cap_releases--; |
| 1921 | |
| 1922 | head = msg->front.iov_base; |
Jeff Layton | 4198aba | 2019-05-02 08:06:50 -0400 | [diff] [blame] | 1923 | put_unaligned_le32(get_unaligned_le32(&head->num) + 1, |
| 1924 | &head->num); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1925 | item = msg->front.iov_base + msg->front.iov_len; |
| 1926 | item->ino = cpu_to_le64(cap->cap_ino); |
| 1927 | item->cap_id = cpu_to_le64(cap->cap_id); |
| 1928 | item->migrate_seq = cpu_to_le32(cap->mseq); |
| 1929 | item->seq = cpu_to_le32(cap->issue_seq); |
| 1930 | msg->front.iov_len += sizeof(*item); |
| 1931 | |
| 1932 | ceph_put_cap(mdsc, cap); |
| 1933 | |
| 1934 | if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1935 | // Append cap_barrier field |
| 1936 | cap_barrier = msg->front.iov_base + msg->front.iov_len; |
| 1937 | *cap_barrier = barrier; |
| 1938 | msg->front.iov_len += sizeof(*cap_barrier); |
| 1939 | |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1940 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 1941 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); |
| 1942 | ceph_con_send(&session->s_con, msg); |
| 1943 | msg = NULL; |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | BUG_ON(num_cap_releases != 0); |
| 1948 | |
| 1949 | spin_lock(&session->s_cap_lock); |
| 1950 | if (!list_empty(&session->s_cap_releases)) |
| 1951 | goto again; |
| 1952 | spin_unlock(&session->s_cap_lock); |
| 1953 | |
| 1954 | if (msg) { |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1955 | // Append cap_barrier field |
| 1956 | cap_barrier = msg->front.iov_base + msg->front.iov_len; |
| 1957 | *cap_barrier = barrier; |
| 1958 | msg->front.iov_len += sizeof(*cap_barrier); |
| 1959 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1960 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 1961 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); |
| 1962 | ceph_con_send(&session->s_con, msg); |
| 1963 | } |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1964 | return; |
| 1965 | out_err: |
| 1966 | pr_err("send_cap_releases mds%d, failed to allocate message\n", |
| 1967 | session->s_mds); |
| 1968 | spin_lock(&session->s_cap_lock); |
| 1969 | list_splice(&tmp_list, &session->s_cap_releases); |
| 1970 | session->s_num_cap_releases += num_cap_releases; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1971 | spin_unlock(&session->s_cap_lock); |
| 1972 | } |
| 1973 | |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 1974 | static void ceph_cap_release_work(struct work_struct *work) |
| 1975 | { |
| 1976 | struct ceph_mds_session *session = |
| 1977 | container_of(work, struct ceph_mds_session, s_cap_release_work); |
| 1978 | |
| 1979 | mutex_lock(&session->s_mutex); |
| 1980 | if (session->s_state == CEPH_MDS_SESSION_OPEN || |
| 1981 | session->s_state == CEPH_MDS_SESSION_HUNG) |
| 1982 | ceph_send_cap_releases(session->s_mdsc, session); |
| 1983 | mutex_unlock(&session->s_mutex); |
| 1984 | ceph_put_mds_session(session); |
| 1985 | } |
| 1986 | |
| 1987 | void ceph_flush_cap_releases(struct ceph_mds_client *mdsc, |
| 1988 | struct ceph_mds_session *session) |
| 1989 | { |
| 1990 | if (mdsc->stopping) |
| 1991 | return; |
| 1992 | |
Xiubo Li | 5b3248c | 2019-12-19 19:44:09 -0500 | [diff] [blame] | 1993 | ceph_get_mds_session(session); |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 1994 | if (queue_work(mdsc->fsc->cap_wq, |
| 1995 | &session->s_cap_release_work)) { |
| 1996 | dout("cap release work queued\n"); |
| 1997 | } else { |
| 1998 | ceph_put_mds_session(session); |
| 1999 | dout("failed to queue cap release work\n"); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /* |
| 2004 | * caller holds session->s_cap_lock |
| 2005 | */ |
| 2006 | void __ceph_queue_cap_release(struct ceph_mds_session *session, |
| 2007 | struct ceph_cap *cap) |
| 2008 | { |
| 2009 | list_add_tail(&cap->session_caps, &session->s_cap_releases); |
| 2010 | session->s_num_cap_releases++; |
| 2011 | |
| 2012 | if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE)) |
| 2013 | ceph_flush_cap_releases(session->s_mdsc, session); |
| 2014 | } |
| 2015 | |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 2016 | static void ceph_cap_reclaim_work(struct work_struct *work) |
| 2017 | { |
| 2018 | struct ceph_mds_client *mdsc = |
| 2019 | container_of(work, struct ceph_mds_client, cap_reclaim_work); |
| 2020 | int ret = ceph_trim_dentries(mdsc); |
| 2021 | if (ret == -EAGAIN) |
| 2022 | ceph_queue_cap_reclaim_work(mdsc); |
| 2023 | } |
| 2024 | |
| 2025 | void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc) |
| 2026 | { |
| 2027 | if (mdsc->stopping) |
| 2028 | return; |
| 2029 | |
| 2030 | if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) { |
| 2031 | dout("caps reclaim work queued\n"); |
| 2032 | } else { |
| 2033 | dout("failed to queue caps release work\n"); |
| 2034 | } |
| 2035 | } |
| 2036 | |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 2037 | void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr) |
| 2038 | { |
| 2039 | int val; |
| 2040 | if (!nr) |
| 2041 | return; |
| 2042 | val = atomic_add_return(nr, &mdsc->cap_reclaim_pending); |
Xiubo Li | bba1560 | 2019-11-26 07:32:22 -0500 | [diff] [blame] | 2043 | if ((val % CEPH_CAPS_PER_RELEASE) < nr) { |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 2044 | atomic_set(&mdsc->cap_reclaim_pending, 0); |
| 2045 | ceph_queue_cap_reclaim_work(mdsc); |
| 2046 | } |
| 2047 | } |
| 2048 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2049 | /* |
| 2050 | * requests |
| 2051 | */ |
| 2052 | |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 2053 | int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req, |
| 2054 | struct inode *dir) |
| 2055 | { |
| 2056 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 2057 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
| 2058 | struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options; |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 2059 | size_t size = sizeof(struct ceph_mds_reply_dir_entry); |
Jeff Layton | ad8c28a | 2019-09-09 15:58:55 -0400 | [diff] [blame] | 2060 | unsigned int num_entries; |
| 2061 | int order; |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 2062 | |
| 2063 | spin_lock(&ci->i_ceph_lock); |
| 2064 | num_entries = ci->i_files + ci->i_subdirs; |
| 2065 | spin_unlock(&ci->i_ceph_lock); |
Jeff Layton | ad8c28a | 2019-09-09 15:58:55 -0400 | [diff] [blame] | 2066 | num_entries = max(num_entries, 1U); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 2067 | num_entries = min(num_entries, opt->max_readdir); |
| 2068 | |
| 2069 | order = get_order(size * num_entries); |
| 2070 | while (order >= 0) { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 2071 | rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL | |
| 2072 | __GFP_NOWARN, |
| 2073 | order); |
| 2074 | if (rinfo->dir_entries) |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 2075 | break; |
| 2076 | order--; |
| 2077 | } |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 2078 | if (!rinfo->dir_entries) |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 2079 | return -ENOMEM; |
| 2080 | |
| 2081 | num_entries = (PAGE_SIZE << order) / size; |
| 2082 | num_entries = min(num_entries, opt->max_readdir); |
| 2083 | |
| 2084 | rinfo->dir_buf_size = PAGE_SIZE << order; |
| 2085 | req->r_num_caps = num_entries + 1; |
| 2086 | req->r_args.readdir.max_entries = cpu_to_le32(num_entries); |
| 2087 | req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes); |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2091 | /* |
| 2092 | * Create an mds request. |
| 2093 | */ |
| 2094 | struct ceph_mds_request * |
| 2095 | ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode) |
| 2096 | { |
Jeff Layton | 058daab | 2020-02-17 18:38:37 -0500 | [diff] [blame] | 2097 | struct ceph_mds_request *req; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2098 | |
Jeff Layton | 058daab | 2020-02-17 18:38:37 -0500 | [diff] [blame] | 2099 | req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2100 | if (!req) |
| 2101 | return ERR_PTR(-ENOMEM); |
| 2102 | |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2103 | mutex_init(&req->r_fill_mutex); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 2104 | req->r_mdsc = mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2105 | req->r_started = jiffies; |
| 2106 | req->r_resend_mds = -1; |
| 2107 | INIT_LIST_HEAD(&req->r_unsafe_dir_item); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2108 | INIT_LIST_HEAD(&req->r_unsafe_target_item); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2109 | req->r_fmode = -1; |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 2110 | kref_init(&req->r_kref); |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2111 | RB_CLEAR_NODE(&req->r_node); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2112 | INIT_LIST_HEAD(&req->r_wait); |
| 2113 | init_completion(&req->r_completion); |
| 2114 | init_completion(&req->r_safe_completion); |
| 2115 | INIT_LIST_HEAD(&req->r_unsafe_item); |
| 2116 | |
Deepa Dinamani | 668c9a6 | 2019-12-02 21:19:42 -0800 | [diff] [blame] | 2117 | ktime_get_coarse_real_ts64(&req->r_stamp); |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 2118 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2119 | req->r_op = op; |
| 2120 | req->r_direct_mode = mode; |
| 2121 | return req; |
| 2122 | } |
| 2123 | |
| 2124 | /* |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2125 | * return oldest (lowest) request, tid in request tree, 0 if none. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2126 | * |
| 2127 | * called under mdsc->mutex. |
| 2128 | */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2129 | static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc) |
| 2130 | { |
| 2131 | if (RB_EMPTY_ROOT(&mdsc->request_tree)) |
| 2132 | return NULL; |
| 2133 | return rb_entry(rb_first(&mdsc->request_tree), |
| 2134 | struct ceph_mds_request, r_node); |
| 2135 | } |
| 2136 | |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 2137 | static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2138 | { |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 2139 | return mdsc->oldest_tid; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | /* |
| 2143 | * Build a dentry's path. Allocate on heap; caller must kfree. Based |
| 2144 | * on build_path_from_dentry in fs/cifs/dir.c. |
| 2145 | * |
| 2146 | * If @stop_on_nosnap, generate path relative to the first non-snapped |
| 2147 | * inode. |
| 2148 | * |
| 2149 | * Encode hidden .snap dirs as a double /, i.e. |
| 2150 | * foo/.snap/bar -> foo//bar |
| 2151 | */ |
Jeff Layton | 69a10fb | 2019-04-26 13:33:39 -0400 | [diff] [blame] | 2152 | char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2153 | int stop_on_nosnap) |
| 2154 | { |
| 2155 | struct dentry *temp; |
| 2156 | char *path; |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2157 | int pos; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 2158 | unsigned seq; |
Jeff Layton | 69a10fb | 2019-04-26 13:33:39 -0400 | [diff] [blame] | 2159 | u64 base; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2160 | |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 2161 | if (!dentry) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2162 | return ERR_PTR(-EINVAL); |
| 2163 | |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2164 | path = __getname(); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 2165 | if (!path) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2166 | return ERR_PTR(-ENOMEM); |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2167 | retry: |
| 2168 | pos = PATH_MAX - 1; |
| 2169 | path[pos] = '\0'; |
| 2170 | |
| 2171 | seq = read_seqbegin(&rename_lock); |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 2172 | rcu_read_lock(); |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2173 | temp = dentry; |
| 2174 | for (;;) { |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 2175 | struct inode *inode; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2176 | |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 2177 | spin_lock(&temp->d_lock); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2178 | inode = d_inode(temp); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2179 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) { |
Sage Weil | 104648a | 2010-03-18 10:14:30 -0700 | [diff] [blame] | 2180 | dout("build_path path+%d: %p SNAPDIR\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2181 | pos, temp); |
Jeff Layton | d6b8bd6 | 2019-05-09 07:58:38 -0400 | [diff] [blame] | 2182 | } else if (stop_on_nosnap && inode && dentry != temp && |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2183 | ceph_snap(inode) == CEPH_NOSNAP) { |
Yehuda Sadeh | 9d5a09e | 2011-12-13 09:57:44 -0800 | [diff] [blame] | 2184 | spin_unlock(&temp->d_lock); |
Jeff Layton | d6b8bd6 | 2019-05-09 07:58:38 -0400 | [diff] [blame] | 2185 | pos++; /* get rid of any prepended '/' */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2186 | break; |
| 2187 | } else { |
| 2188 | pos -= temp->d_name.len; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 2189 | if (pos < 0) { |
| 2190 | spin_unlock(&temp->d_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2191 | break; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 2192 | } |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2193 | memcpy(path + pos, temp->d_name.name, temp->d_name.len); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2194 | } |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 2195 | spin_unlock(&temp->d_lock); |
Yan, Zheng | 41883ba | 2019-05-23 10:22:55 +0800 | [diff] [blame] | 2196 | temp = READ_ONCE(temp->d_parent); |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2197 | |
| 2198 | /* Are we at the root? */ |
| 2199 | if (IS_ROOT(temp)) |
| 2200 | break; |
| 2201 | |
| 2202 | /* Are we out of buffer? */ |
| 2203 | if (--pos < 0) |
| 2204 | break; |
| 2205 | |
| 2206 | path[pos] = '/'; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2207 | } |
Jeff Layton | 69a10fb | 2019-04-26 13:33:39 -0400 | [diff] [blame] | 2208 | base = ceph_ino(d_inode(temp)); |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 2209 | rcu_read_unlock(); |
Jeff Layton | f5946bc | 2019-10-16 08:20:17 -0400 | [diff] [blame] | 2210 | |
| 2211 | if (read_seqretry(&rename_lock, seq)) |
| 2212 | goto retry; |
| 2213 | |
| 2214 | if (pos < 0) { |
| 2215 | /* |
| 2216 | * A rename didn't occur, but somehow we didn't end up where |
| 2217 | * we thought we would. Throw a warning and try again. |
| 2218 | */ |
| 2219 | pr_warn("build_path did not end path lookup where " |
| 2220 | "expected, pos is %d\n", pos); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2221 | goto retry; |
| 2222 | } |
| 2223 | |
Jeff Layton | 69a10fb | 2019-04-26 13:33:39 -0400 | [diff] [blame] | 2224 | *pbase = base; |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2225 | *plen = PATH_MAX - 1 - pos; |
Sage Weil | 104648a | 2010-03-18 10:14:30 -0700 | [diff] [blame] | 2226 | dout("build_path on %p %d built %llx '%.*s'\n", |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2227 | dentry, d_count(dentry), base, *plen, path + pos); |
| 2228 | return path + pos; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 2231 | static int build_dentry_path(struct dentry *dentry, struct inode *dir, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2232 | const char **ppath, int *ppathlen, u64 *pino, |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2233 | bool *pfreepath, bool parent_locked) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2234 | { |
| 2235 | char *path; |
| 2236 | |
Jeff Layton | c6b0b65 | 2016-12-15 08:37:57 -0500 | [diff] [blame] | 2237 | rcu_read_lock(); |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 2238 | if (!dir) |
| 2239 | dir = d_inode_rcu(dentry->d_parent); |
Jeff Layton | 964fff7 | 2019-04-29 11:51:02 -0400 | [diff] [blame] | 2240 | if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) { |
Jeff Layton | c6b0b65 | 2016-12-15 08:37:57 -0500 | [diff] [blame] | 2241 | *pino = ceph_ino(dir); |
| 2242 | rcu_read_unlock(); |
Jeff Layton | 964fff7 | 2019-04-29 11:51:02 -0400 | [diff] [blame] | 2243 | *ppath = dentry->d_name.name; |
| 2244 | *ppathlen = dentry->d_name.len; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2245 | return 0; |
| 2246 | } |
Jeff Layton | c6b0b65 | 2016-12-15 08:37:57 -0500 | [diff] [blame] | 2247 | rcu_read_unlock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2248 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); |
| 2249 | if (IS_ERR(path)) |
| 2250 | return PTR_ERR(path); |
| 2251 | *ppath = path; |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2252 | *pfreepath = true; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2253 | return 0; |
| 2254 | } |
| 2255 | |
| 2256 | static int build_inode_path(struct inode *inode, |
| 2257 | const char **ppath, int *ppathlen, u64 *pino, |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2258 | bool *pfreepath) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2259 | { |
| 2260 | struct dentry *dentry; |
| 2261 | char *path; |
| 2262 | |
| 2263 | if (ceph_snap(inode) == CEPH_NOSNAP) { |
| 2264 | *pino = ceph_ino(inode); |
| 2265 | *ppathlen = 0; |
| 2266 | return 0; |
| 2267 | } |
| 2268 | dentry = d_find_alias(inode); |
| 2269 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); |
| 2270 | dput(dentry); |
| 2271 | if (IS_ERR(path)) |
| 2272 | return PTR_ERR(path); |
| 2273 | *ppath = path; |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2274 | *pfreepath = true; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2275 | return 0; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * request arguments may be specified via an inode *, a dentry *, or |
| 2280 | * an explicit ino+path. |
| 2281 | */ |
| 2282 | static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry, |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 2283 | struct inode *rdiri, const char *rpath, |
| 2284 | u64 rino, const char **ppath, int *pathlen, |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2285 | u64 *ino, bool *freepath, bool parent_locked) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2286 | { |
| 2287 | int r = 0; |
| 2288 | |
| 2289 | if (rinode) { |
| 2290 | r = build_inode_path(rinode, ppath, pathlen, ino, freepath); |
| 2291 | dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode), |
| 2292 | ceph_snap(rinode)); |
| 2293 | } else if (rdentry) { |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 2294 | r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino, |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2295 | freepath, parent_locked); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2296 | dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen, |
| 2297 | *ppath); |
Sage Weil | 795858d | 2011-08-15 13:02:37 -0700 | [diff] [blame] | 2298 | } else if (rpath || rino) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2299 | *ino = rino; |
| 2300 | *ppath = rpath; |
David Zafman | b000056 | 2012-10-25 10:23:46 -0700 | [diff] [blame] | 2301 | *pathlen = rpath ? strlen(rpath) : 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2302 | dout(" path %.*s\n", *pathlen, rpath); |
| 2303 | } |
| 2304 | |
| 2305 | return r; |
| 2306 | } |
| 2307 | |
| 2308 | /* |
| 2309 | * called under mdsc->mutex |
| 2310 | */ |
| 2311 | static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc, |
| 2312 | struct ceph_mds_request *req, |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2313 | int mds, bool drop_cap_releases) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2314 | { |
| 2315 | struct ceph_msg *msg; |
| 2316 | struct ceph_mds_request_head *head; |
| 2317 | const char *path1 = NULL; |
| 2318 | const char *path2 = NULL; |
| 2319 | u64 ino1 = 0, ino2 = 0; |
| 2320 | int pathlen1 = 0, pathlen2 = 0; |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2321 | bool freepath1 = false, freepath2 = false; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2322 | int len; |
| 2323 | u16 releases; |
| 2324 | void *p, *end; |
| 2325 | int ret; |
| 2326 | |
| 2327 | ret = set_request_path_attr(req->r_inode, req->r_dentry, |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2328 | req->r_parent, req->r_path1, req->r_ino1.ino, |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2329 | &path1, &pathlen1, &ino1, &freepath1, |
| 2330 | test_bit(CEPH_MDS_R_PARENT_LOCKED, |
| 2331 | &req->r_req_flags)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2332 | if (ret < 0) { |
| 2333 | msg = ERR_PTR(ret); |
| 2334 | goto out; |
| 2335 | } |
| 2336 | |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2337 | /* If r_old_dentry is set, then assume that its parent is locked */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2338 | ret = set_request_path_attr(NULL, req->r_old_dentry, |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 2339 | req->r_old_dentry_dir, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2340 | req->r_path2, req->r_ino2.ino, |
Jeff Layton | 1bcb344 | 2019-04-15 12:00:42 -0400 | [diff] [blame] | 2341 | &path2, &pathlen2, &ino2, &freepath2, true); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2342 | if (ret < 0) { |
| 2343 | msg = ERR_PTR(ret); |
| 2344 | goto out_free1; |
| 2345 | } |
| 2346 | |
| 2347 | len = sizeof(*head) + |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 2348 | pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) + |
Arnd Bergmann | 777d738 | 2015-09-30 15:04:42 +0200 | [diff] [blame] | 2349 | sizeof(struct ceph_timespec); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2350 | |
| 2351 | /* calculate (max) length for cap releases */ |
| 2352 | len += sizeof(struct ceph_mds_request_release) * |
| 2353 | (!!req->r_inode_drop + !!req->r_dentry_drop + |
| 2354 | !!req->r_old_inode_drop + !!req->r_old_dentry_drop); |
| 2355 | if (req->r_dentry_drop) |
Jeff Layton | c1dfc27 | 2019-04-17 14:23:17 -0400 | [diff] [blame] | 2356 | len += pathlen1; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2357 | if (req->r_old_dentry_drop) |
Jeff Layton | c1dfc27 | 2019-04-17 14:23:17 -0400 | [diff] [blame] | 2358 | len += pathlen2; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2359 | |
Ilya Dryomov | 0d9c1ab | 2018-10-15 17:38:23 +0200 | [diff] [blame] | 2360 | msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2361 | if (!msg) { |
| 2362 | msg = ERR_PTR(-ENOMEM); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2363 | goto out_free2; |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2364 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2365 | |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 2366 | msg->hdr.version = cpu_to_le16(2); |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2367 | msg->hdr.tid = cpu_to_le64(req->r_tid); |
| 2368 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2369 | head = msg->front.iov_base; |
| 2370 | p = msg->front.iov_base + sizeof(*head); |
| 2371 | end = msg->front.iov_base + msg->front.iov_len; |
| 2372 | |
| 2373 | head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch); |
| 2374 | head->op = cpu_to_le32(req->r_op); |
Eric W. Biederman | ff3d004 | 2013-01-31 04:01:53 -0800 | [diff] [blame] | 2375 | head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid)); |
| 2376 | head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid)); |
Jeff Layton | 2def865 | 2019-10-14 15:41:54 -0400 | [diff] [blame] | 2377 | head->ino = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2378 | head->args = req->r_args; |
| 2379 | |
| 2380 | ceph_encode_filepath(&p, end, ino1, path1); |
| 2381 | ceph_encode_filepath(&p, end, ino2, path2); |
| 2382 | |
Sage Weil | e979cf5 | 2010-07-15 14:58:39 -0700 | [diff] [blame] | 2383 | /* make note of release offset, in case we need to replay */ |
| 2384 | req->r_request_release_offset = p - msg->front.iov_base; |
| 2385 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2386 | /* cap releases */ |
| 2387 | releases = 0; |
| 2388 | if (req->r_inode_drop) |
| 2389 | releases += ceph_encode_inode_release(&p, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2390 | req->r_inode ? req->r_inode : d_inode(req->r_dentry), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2391 | mds, req->r_inode_drop, req->r_inode_unless, 0); |
| 2392 | if (req->r_dentry_drop) |
| 2393 | releases += ceph_encode_dentry_release(&p, req->r_dentry, |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2394 | req->r_parent, mds, req->r_dentry_drop, |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 2395 | req->r_dentry_unless); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2396 | if (req->r_old_dentry_drop) |
| 2397 | releases += ceph_encode_dentry_release(&p, req->r_old_dentry, |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 2398 | req->r_old_dentry_dir, mds, |
| 2399 | req->r_old_dentry_drop, |
| 2400 | req->r_old_dentry_unless); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2401 | if (req->r_old_inode_drop) |
| 2402 | releases += ceph_encode_inode_release(&p, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2403 | d_inode(req->r_old_dentry), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2404 | mds, req->r_old_inode_drop, req->r_old_inode_unless, 0); |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2405 | |
| 2406 | if (drop_cap_releases) { |
| 2407 | releases = 0; |
| 2408 | p = msg->front.iov_base + req->r_request_release_offset; |
| 2409 | } |
| 2410 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2411 | head->num_releases = cpu_to_le16(releases); |
| 2412 | |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 2413 | /* time stamp */ |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2414 | { |
| 2415 | struct ceph_timespec ts; |
Arnd Bergmann | 0ed1e90 | 2018-07-13 22:18:38 +0200 | [diff] [blame] | 2416 | ceph_encode_timespec64(&ts, &req->r_stamp); |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2417 | ceph_encode_copy(&p, &ts, sizeof(ts)); |
| 2418 | } |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 2419 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2420 | BUG_ON(p > end); |
| 2421 | msg->front.iov_len = p - msg->front.iov_base; |
| 2422 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 2423 | |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 2424 | if (req->r_pagelist) { |
| 2425 | struct ceph_pagelist *pagelist = req->r_pagelist; |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 2426 | ceph_msg_data_add_pagelist(msg, pagelist); |
| 2427 | msg->hdr.data_len = cpu_to_le32(pagelist->length); |
| 2428 | } else { |
| 2429 | msg->hdr.data_len = 0; |
Alex Elder | ebf18f4 | 2013-03-04 22:29:57 -0600 | [diff] [blame] | 2430 | } |
Alex Elder | 02afca6 | 2013-02-14 12:16:43 -0600 | [diff] [blame] | 2431 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2432 | msg->hdr.data_off = cpu_to_le16(0); |
| 2433 | |
| 2434 | out_free2: |
| 2435 | if (freepath2) |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2436 | ceph_mdsc_free_path((char *)path2, pathlen2); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2437 | out_free1: |
| 2438 | if (freepath1) |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 2439 | ceph_mdsc_free_path((char *)path1, pathlen1); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2440 | out: |
| 2441 | return msg; |
| 2442 | } |
| 2443 | |
| 2444 | /* |
| 2445 | * called under mdsc->mutex if error, under no mutex if |
| 2446 | * success. |
| 2447 | */ |
| 2448 | static void complete_request(struct ceph_mds_client *mdsc, |
| 2449 | struct ceph_mds_request *req) |
| 2450 | { |
| 2451 | if (req->r_callback) |
| 2452 | req->r_callback(mdsc, req); |
Jeff Layton | 111c708 | 2019-04-02 09:43:18 -0400 | [diff] [blame] | 2453 | complete_all(&req->r_completion); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * called under mdsc->mutex |
| 2458 | */ |
| 2459 | static int __prepare_send_request(struct ceph_mds_client *mdsc, |
| 2460 | struct ceph_mds_request *req, |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2461 | int mds, bool drop_cap_releases) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2462 | { |
| 2463 | struct ceph_mds_request_head *rhead; |
| 2464 | struct ceph_msg *msg; |
| 2465 | int flags = 0; |
| 2466 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2467 | req->r_attempts++; |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2468 | if (req->r_inode) { |
| 2469 | struct ceph_cap *cap = |
| 2470 | ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds); |
| 2471 | |
| 2472 | if (cap) |
| 2473 | req->r_sent_on_mseq = cap->mseq; |
| 2474 | else |
| 2475 | req->r_sent_on_mseq = -1; |
| 2476 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2477 | dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req, |
| 2478 | req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts); |
| 2479 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2480 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2481 | void *p; |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2482 | /* |
| 2483 | * Replay. Do not regenerate message (and rebuild |
| 2484 | * paths, etc.); just use the original message. |
| 2485 | * Rebuilding paths will break for renames because |
| 2486 | * d_move mangles the src name. |
| 2487 | */ |
| 2488 | msg = req->r_request; |
| 2489 | rhead = msg->front.iov_base; |
| 2490 | |
| 2491 | flags = le32_to_cpu(rhead->flags); |
| 2492 | flags |= CEPH_MDS_FLAG_REPLAY; |
| 2493 | rhead->flags = cpu_to_le32(flags); |
| 2494 | |
| 2495 | if (req->r_target_inode) |
| 2496 | rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode)); |
| 2497 | |
| 2498 | rhead->num_retry = req->r_attempts - 1; |
Sage Weil | e979cf5 | 2010-07-15 14:58:39 -0700 | [diff] [blame] | 2499 | |
| 2500 | /* remove cap/dentry releases from message */ |
| 2501 | rhead->num_releases = 0; |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2502 | |
| 2503 | /* time stamp */ |
| 2504 | p = msg->front.iov_base + req->r_request_release_offset; |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2505 | { |
| 2506 | struct ceph_timespec ts; |
Arnd Bergmann | 0ed1e90 | 2018-07-13 22:18:38 +0200 | [diff] [blame] | 2507 | ceph_encode_timespec64(&ts, &req->r_stamp); |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2508 | ceph_encode_copy(&p, &ts, sizeof(ts)); |
| 2509 | } |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2510 | |
| 2511 | msg->front.iov_len = p - msg->front.iov_base; |
| 2512 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2513 | return 0; |
| 2514 | } |
| 2515 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2516 | if (req->r_request) { |
| 2517 | ceph_msg_put(req->r_request); |
| 2518 | req->r_request = NULL; |
| 2519 | } |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2520 | msg = create_request_message(mdsc, req, mds, drop_cap_releases); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2521 | if (IS_ERR(msg)) { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2522 | req->r_err = PTR_ERR(msg); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2523 | return PTR_ERR(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2524 | } |
| 2525 | req->r_request = msg; |
| 2526 | |
| 2527 | rhead = msg->front.iov_base; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2528 | rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc)); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2529 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2530 | flags |= CEPH_MDS_FLAG_REPLAY; |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2531 | if (req->r_parent) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2532 | flags |= CEPH_MDS_FLAG_WANT_DENTRY; |
| 2533 | rhead->flags = cpu_to_le32(flags); |
| 2534 | rhead->num_fwd = req->r_num_fwd; |
| 2535 | rhead->num_retry = req->r_attempts - 1; |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2536 | rhead->ino = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2537 | |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2538 | dout(" r_parent = %p\n", req->r_parent); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2539 | return 0; |
| 2540 | } |
| 2541 | |
| 2542 | /* |
Xiubo Li | 9cf5456 | 2019-12-05 20:50:21 -0500 | [diff] [blame] | 2543 | * called under mdsc->mutex |
| 2544 | */ |
| 2545 | static int __send_request(struct ceph_mds_client *mdsc, |
| 2546 | struct ceph_mds_session *session, |
| 2547 | struct ceph_mds_request *req, |
| 2548 | bool drop_cap_releases) |
| 2549 | { |
| 2550 | int err; |
| 2551 | |
| 2552 | err = __prepare_send_request(mdsc, req, session->s_mds, |
| 2553 | drop_cap_releases); |
| 2554 | if (!err) { |
| 2555 | ceph_msg_get(req->r_request); |
| 2556 | ceph_con_send(&session->s_con, req->r_request); |
| 2557 | } |
| 2558 | |
| 2559 | return err; |
| 2560 | } |
| 2561 | |
| 2562 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2563 | * send request, or put it on the appropriate wait list. |
| 2564 | */ |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2565 | static void __do_request(struct ceph_mds_client *mdsc, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2566 | struct ceph_mds_request *req) |
| 2567 | { |
| 2568 | struct ceph_mds_session *session = NULL; |
| 2569 | int mds = -1; |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 2570 | int err = 0; |
Xiubo Li | c4853e9 | 2019-12-09 07:47:15 -0500 | [diff] [blame] | 2571 | bool random; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2572 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2573 | if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) { |
| 2574 | if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) |
Yan, Zheng | eb1b8af | 2013-09-26 14:25:36 +0800 | [diff] [blame] | 2575 | __unregister_request(mdsc, req); |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2576 | return; |
Yan, Zheng | eb1b8af | 2013-09-26 14:25:36 +0800 | [diff] [blame] | 2577 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2578 | |
| 2579 | if (req->r_timeout && |
| 2580 | time_after_eq(jiffies, req->r_started + req->r_timeout)) { |
| 2581 | dout("do_request timed out\n"); |
Xiubo Li | 8ccf7fc | 2020-02-23 22:23:11 -0500 | [diff] [blame^] | 2582 | err = -ETIMEDOUT; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2583 | goto finish; |
| 2584 | } |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 2585 | if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 2586 | dout("do_request forced umount\n"); |
| 2587 | err = -EIO; |
| 2588 | goto finish; |
| 2589 | } |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 2590 | if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) { |
Yan, Zheng | e9e427f | 2016-11-10 16:02:06 +0800 | [diff] [blame] | 2591 | if (mdsc->mdsmap_err) { |
| 2592 | err = mdsc->mdsmap_err; |
| 2593 | dout("do_request mdsmap err %d\n", err); |
| 2594 | goto finish; |
| 2595 | } |
Yan, Zheng | cc8e834 | 2017-01-04 16:21:58 +0800 | [diff] [blame] | 2596 | if (mdsc->mdsmap->m_epoch == 0) { |
| 2597 | dout("do_request no mdsmap, waiting for map\n"); |
| 2598 | list_add(&req->r_wait, &mdsc->waiting_for_map); |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2599 | return; |
Yan, Zheng | cc8e834 | 2017-01-04 16:21:58 +0800 | [diff] [blame] | 2600 | } |
Yan, Zheng | e9e427f | 2016-11-10 16:02:06 +0800 | [diff] [blame] | 2601 | if (!(mdsc->fsc->mount_options->flags & |
| 2602 | CEPH_MOUNT_OPT_MOUNTWAIT) && |
| 2603 | !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) { |
Xiubo Li | 9782005 | 2019-12-10 20:29:40 -0500 | [diff] [blame] | 2604 | err = -EHOSTUNREACH; |
Yan, Zheng | e9e427f | 2016-11-10 16:02:06 +0800 | [diff] [blame] | 2605 | goto finish; |
| 2606 | } |
| 2607 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2608 | |
Sage Weil | dc69e2e | 2010-11-02 13:49:00 -0700 | [diff] [blame] | 2609 | put_request_session(req); |
| 2610 | |
Xiubo Li | c4853e9 | 2019-12-09 07:47:15 -0500 | [diff] [blame] | 2611 | mds = __choose_mds(mdsc, req, &random); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2612 | if (mds < 0 || |
| 2613 | ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) { |
| 2614 | dout("do_request no mds or not active, waiting for map\n"); |
| 2615 | list_add(&req->r_wait, &mdsc->waiting_for_map); |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2616 | return; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | /* get, open session */ |
| 2620 | session = __ceph_lookup_mds_session(mdsc, mds); |
Sage Weil | 9c423956 | 2010-03-20 20:43:28 -0700 | [diff] [blame] | 2621 | if (!session) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2622 | session = register_session(mdsc, mds); |
Sage Weil | 9c423956 | 2010-03-20 20:43:28 -0700 | [diff] [blame] | 2623 | if (IS_ERR(session)) { |
| 2624 | err = PTR_ERR(session); |
| 2625 | goto finish; |
| 2626 | } |
| 2627 | } |
Xiubo Li | 5b3248c | 2019-12-19 19:44:09 -0500 | [diff] [blame] | 2628 | req->r_session = ceph_get_mds_session(session); |
Sage Weil | dc69e2e | 2010-11-02 13:49:00 -0700 | [diff] [blame] | 2629 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2630 | dout("do_request mds%d session %p state %s\n", mds, session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 2631 | ceph_session_state_name(session->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2632 | if (session->s_state != CEPH_MDS_SESSION_OPEN && |
| 2633 | session->s_state != CEPH_MDS_SESSION_HUNG) { |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 2634 | if (session->s_state == CEPH_MDS_SESSION_REJECTED) { |
| 2635 | err = -EACCES; |
| 2636 | goto out_session; |
| 2637 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2638 | if (session->s_state == CEPH_MDS_SESSION_NEW || |
Xiubo Li | c4853e9 | 2019-12-09 07:47:15 -0500 | [diff] [blame] | 2639 | session->s_state == CEPH_MDS_SESSION_CLOSING) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2640 | __open_session(mdsc, session); |
Xiubo Li | c4853e9 | 2019-12-09 07:47:15 -0500 | [diff] [blame] | 2641 | /* retry the same mds later */ |
| 2642 | if (random) |
| 2643 | req->r_resend_mds = mds; |
| 2644 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2645 | list_add(&req->r_wait, &session->s_waiting); |
| 2646 | goto out_session; |
| 2647 | } |
| 2648 | |
| 2649 | /* send request */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2650 | req->r_resend_mds = -1; /* forget any previous mds hint */ |
| 2651 | |
| 2652 | if (req->r_request_started == 0) /* note request start time */ |
| 2653 | req->r_request_started = jiffies; |
| 2654 | |
Xiubo Li | 9cf5456 | 2019-12-05 20:50:21 -0500 | [diff] [blame] | 2655 | err = __send_request(mdsc, session, req, false); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2656 | |
| 2657 | out_session: |
| 2658 | ceph_put_mds_session(session); |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 2659 | finish: |
| 2660 | if (err) { |
| 2661 | dout("__do_request early error %d\n", err); |
| 2662 | req->r_err = err; |
| 2663 | complete_request(mdsc, req); |
| 2664 | __unregister_request(mdsc, req); |
| 2665 | } |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2666 | return; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | /* |
| 2670 | * called under mdsc->mutex |
| 2671 | */ |
| 2672 | static void __wake_requests(struct ceph_mds_client *mdsc, |
| 2673 | struct list_head *head) |
| 2674 | { |
Yan, Zheng | ed75ec2 | 2012-11-19 10:49:06 +0800 | [diff] [blame] | 2675 | struct ceph_mds_request *req; |
| 2676 | LIST_HEAD(tmp_list); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2677 | |
Yan, Zheng | ed75ec2 | 2012-11-19 10:49:06 +0800 | [diff] [blame] | 2678 | list_splice_init(head, &tmp_list); |
| 2679 | |
| 2680 | while (!list_empty(&tmp_list)) { |
| 2681 | req = list_entry(tmp_list.next, |
| 2682 | struct ceph_mds_request, r_wait); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2683 | list_del_init(&req->r_wait); |
Sage Weil | 7971bd9 | 2013-05-01 21:15:58 -0700 | [diff] [blame] | 2684 | dout(" wake request %p tid %llu\n", req, req->r_tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2685 | __do_request(mdsc, req); |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | /* |
| 2690 | * Wake up threads with requests pending for @mds, so that they can |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2691 | * resubmit their requests to a possibly different mds. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2692 | */ |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2693 | static void kick_requests(struct ceph_mds_client *mdsc, int mds) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2694 | { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2695 | struct ceph_mds_request *req; |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2696 | struct rb_node *p = rb_first(&mdsc->request_tree); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2697 | |
| 2698 | dout("kick_requests mds%d\n", mds); |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2699 | while (p) { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2700 | req = rb_entry(p, struct ceph_mds_request, r_node); |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2701 | p = rb_next(p); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2702 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2703 | continue; |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2704 | if (req->r_attempts > 0) |
| 2705 | continue; /* only new requests */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2706 | if (req->r_session && |
| 2707 | req->r_session->s_mds == mds) { |
| 2708 | dout(" kicking tid %llu\n", req->r_tid); |
Yan, Zheng | 03974e8 | 2014-09-11 14:28:56 +0800 | [diff] [blame] | 2709 | list_del_init(&req->r_wait); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2710 | __do_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
Jeff Layton | 86bda53 | 2019-04-02 09:24:36 -0400 | [diff] [blame] | 2715 | int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2716 | struct ceph_mds_request *req) |
| 2717 | { |
Jeff Layton | 86bda53 | 2019-04-02 09:24:36 -0400 | [diff] [blame] | 2718 | int err; |
| 2719 | |
| 2720 | /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */ |
| 2721 | if (req->r_inode) |
| 2722 | ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); |
Jeff Layton | 9c1c2b3 | 2019-04-03 13:16:01 -0400 | [diff] [blame] | 2723 | if (req->r_parent) { |
Jeff Layton | 86bda53 | 2019-04-02 09:24:36 -0400 | [diff] [blame] | 2724 | ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN); |
Jeff Layton | 9c1c2b3 | 2019-04-03 13:16:01 -0400 | [diff] [blame] | 2725 | ihold(req->r_parent); |
| 2726 | } |
Jeff Layton | 86bda53 | 2019-04-02 09:24:36 -0400 | [diff] [blame] | 2727 | if (req->r_old_dentry_dir) |
| 2728 | ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir), |
| 2729 | CEPH_CAP_PIN); |
| 2730 | |
| 2731 | dout("submit_request on %p for inode %p\n", req, dir); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2732 | mutex_lock(&mdsc->mutex); |
Jeff Layton | 86bda53 | 2019-04-02 09:24:36 -0400 | [diff] [blame] | 2733 | __register_request(mdsc, req, dir); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2734 | __do_request(mdsc, req); |
Jeff Layton | 86bda53 | 2019-04-02 09:24:36 -0400 | [diff] [blame] | 2735 | err = req->r_err; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2736 | mutex_unlock(&mdsc->mutex); |
Jeff Layton | 86bda53 | 2019-04-02 09:24:36 -0400 | [diff] [blame] | 2737 | return err; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
Jeff Layton | 8340f22 | 2019-04-02 12:34:38 -0400 | [diff] [blame] | 2740 | static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc, |
| 2741 | struct ceph_mds_request *req) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2742 | { |
| 2743 | int err; |
| 2744 | |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2745 | /* wait */ |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2746 | dout("do_request waiting\n"); |
Ilya Dryomov | 5be7303 | 2015-05-19 12:05:38 +0300 | [diff] [blame] | 2747 | if (!req->r_timeout && req->r_wait_for_completion) { |
Yan, Zheng | 9280be2 | 2014-10-14 10:33:35 +0800 | [diff] [blame] | 2748 | err = req->r_wait_for_completion(mdsc, req); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2749 | } else { |
Ilya Dryomov | 5be7303 | 2015-05-19 12:05:38 +0300 | [diff] [blame] | 2750 | long timeleft = wait_for_completion_killable_timeout( |
| 2751 | &req->r_completion, |
| 2752 | ceph_timeout_jiffies(req->r_timeout)); |
| 2753 | if (timeleft > 0) |
| 2754 | err = 0; |
| 2755 | else if (!timeleft) |
Xiubo Li | 8ccf7fc | 2020-02-23 22:23:11 -0500 | [diff] [blame^] | 2756 | err = -ETIMEDOUT; /* timed out */ |
Ilya Dryomov | 5be7303 | 2015-05-19 12:05:38 +0300 | [diff] [blame] | 2757 | else |
| 2758 | err = timeleft; /* killed */ |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2759 | } |
| 2760 | dout("do_request waited, got %d\n", err); |
| 2761 | mutex_lock(&mdsc->mutex); |
| 2762 | |
| 2763 | /* only abort if we didn't race with a real reply */ |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2764 | if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2765 | err = le32_to_cpu(req->r_reply_info.head->result); |
| 2766 | } else if (err < 0) { |
| 2767 | dout("aborted request %lld with %d\n", req->r_tid, err); |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2768 | |
| 2769 | /* |
| 2770 | * ensure we aren't running concurrently with |
| 2771 | * ceph_fill_trace or ceph_readdir_prepopulate, which |
| 2772 | * rely on locks (dir mutex) held by our caller. |
| 2773 | */ |
| 2774 | mutex_lock(&req->r_fill_mutex); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2775 | req->r_err = err; |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2776 | set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags); |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2777 | mutex_unlock(&req->r_fill_mutex); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2778 | |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2779 | if (req->r_parent && |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2780 | (req->r_op & CEPH_MDS_OP_WRITE)) |
| 2781 | ceph_invalidate_dir_request(req); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2782 | } else { |
| 2783 | err = req->r_err; |
| 2784 | } |
| 2785 | |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2786 | mutex_unlock(&mdsc->mutex); |
Jeff Layton | 8340f22 | 2019-04-02 12:34:38 -0400 | [diff] [blame] | 2787 | return err; |
| 2788 | } |
| 2789 | |
| 2790 | /* |
| 2791 | * Synchrously perform an mds request. Take care of all of the |
| 2792 | * session setup, forwarding, retry details. |
| 2793 | */ |
| 2794 | int ceph_mdsc_do_request(struct ceph_mds_client *mdsc, |
| 2795 | struct inode *dir, |
| 2796 | struct ceph_mds_request *req) |
| 2797 | { |
| 2798 | int err; |
| 2799 | |
| 2800 | dout("do_request on %p\n", req); |
| 2801 | |
| 2802 | /* issue */ |
| 2803 | err = ceph_mdsc_submit_request(mdsc, dir, req); |
| 2804 | if (!err) |
| 2805 | err = ceph_mdsc_wait_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2806 | dout("do_request %p done, result %d\n", req, err); |
| 2807 | return err; |
| 2808 | } |
| 2809 | |
| 2810 | /* |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 2811 | * Invalidate dir's completeness, dentry lease state on an aborted MDS |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2812 | * namespace request. |
| 2813 | */ |
| 2814 | void ceph_invalidate_dir_request(struct ceph_mds_request *req) |
| 2815 | { |
Yan, Zheng | 8d8f371 | 2017-11-24 11:51:32 +0800 | [diff] [blame] | 2816 | struct inode *dir = req->r_parent; |
| 2817 | struct inode *old_dir = req->r_old_dentry_dir; |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2818 | |
Yan, Zheng | 8d8f371 | 2017-11-24 11:51:32 +0800 | [diff] [blame] | 2819 | dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir); |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2820 | |
Yan, Zheng | 8d8f371 | 2017-11-24 11:51:32 +0800 | [diff] [blame] | 2821 | ceph_dir_clear_complete(dir); |
| 2822 | if (old_dir) |
| 2823 | ceph_dir_clear_complete(old_dir); |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2824 | if (req->r_dentry) |
| 2825 | ceph_invalidate_dentry_lease(req->r_dentry); |
| 2826 | if (req->r_old_dentry) |
| 2827 | ceph_invalidate_dentry_lease(req->r_old_dentry); |
| 2828 | } |
| 2829 | |
| 2830 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2831 | * Handle mds reply. |
| 2832 | * |
| 2833 | * We take the session mutex and parse and process the reply immediately. |
| 2834 | * This preserves the logical ordering of replies, capabilities, etc., sent |
| 2835 | * by the MDS as they are applied to our local cache. |
| 2836 | */ |
| 2837 | static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) |
| 2838 | { |
| 2839 | struct ceph_mds_client *mdsc = session->s_mdsc; |
| 2840 | struct ceph_mds_request *req; |
| 2841 | struct ceph_mds_reply_head *head = msg->front.iov_base; |
| 2842 | struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2843 | struct ceph_snap_realm *realm; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2844 | u64 tid; |
| 2845 | int err, result; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2846 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2847 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2848 | if (msg->front.iov_len < sizeof(*head)) { |
| 2849 | pr_err("mdsc_handle_reply got corrupt (short) reply\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2850 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2851 | return; |
| 2852 | } |
| 2853 | |
| 2854 | /* get request, session */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2855 | tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2856 | mutex_lock(&mdsc->mutex); |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2857 | req = lookup_get_request(mdsc, tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2858 | if (!req) { |
| 2859 | dout("handle_reply on unknown tid %llu\n", tid); |
| 2860 | mutex_unlock(&mdsc->mutex); |
| 2861 | return; |
| 2862 | } |
| 2863 | dout("handle_reply %p\n", req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2864 | |
| 2865 | /* correct session? */ |
Sage Weil | d96d604 | 2010-03-20 20:50:58 -0700 | [diff] [blame] | 2866 | if (req->r_session != session) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2867 | pr_err("mdsc_handle_reply got %llu on session mds%d" |
| 2868 | " not mds%d\n", tid, session->s_mds, |
| 2869 | req->r_session ? req->r_session->s_mds : -1); |
| 2870 | mutex_unlock(&mdsc->mutex); |
| 2871 | goto out; |
| 2872 | } |
| 2873 | |
| 2874 | /* dup? */ |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2875 | if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) || |
| 2876 | (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) { |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 2877 | pr_warn("got a dup %s reply on %llu from mds%d\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2878 | head->safe ? "safe" : "unsafe", tid, mds); |
| 2879 | mutex_unlock(&mdsc->mutex); |
| 2880 | goto out; |
| 2881 | } |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2882 | if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) { |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 2883 | pr_warn("got unsafe after safe on %llu from mds%d\n", |
Sage Weil | 85792d0d | 2010-05-13 09:06:02 -0700 | [diff] [blame] | 2884 | tid, mds); |
| 2885 | mutex_unlock(&mdsc->mutex); |
| 2886 | goto out; |
| 2887 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2888 | |
| 2889 | result = le32_to_cpu(head->result); |
| 2890 | |
| 2891 | /* |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2892 | * Handle an ESTALE |
| 2893 | * if we're not talking to the authority, send to them |
| 2894 | * if the authority has changed while we weren't looking, |
| 2895 | * send to new authority |
| 2896 | * Otherwise we just have to return an ESTALE |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2897 | */ |
| 2898 | if (result == -ESTALE) { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 2899 | dout("got ESTALE on request %llu\n", req->r_tid); |
Yan, Zheng | 51da8e8 | 2014-07-14 10:48:29 +0800 | [diff] [blame] | 2900 | req->r_resend_mds = -1; |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 2901 | if (req->r_direct_mode != USE_AUTH_MDS) { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 2902 | dout("not using auth, setting for that now\n"); |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2903 | req->r_direct_mode = USE_AUTH_MDS; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2904 | __do_request(mdsc, req); |
| 2905 | mutex_unlock(&mdsc->mutex); |
| 2906 | goto out; |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2907 | } else { |
Xiubo Li | c4853e9 | 2019-12-09 07:47:15 -0500 | [diff] [blame] | 2908 | int mds = __choose_mds(mdsc, req, NULL); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 2909 | if (mds >= 0 && mds != req->r_session->s_mds) { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 2910 | dout("but auth changed, so resending\n"); |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2911 | __do_request(mdsc, req); |
| 2912 | mutex_unlock(&mdsc->mutex); |
| 2913 | goto out; |
| 2914 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2915 | } |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 2916 | dout("have to return ESTALE on request %llu\n", req->r_tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2917 | } |
| 2918 | |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2919 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2920 | if (head->safe) { |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2921 | set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2922 | __unregister_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2923 | |
Xiubo Li | 07edc05 | 2019-12-04 01:27:18 -0500 | [diff] [blame] | 2924 | /* last request during umount? */ |
| 2925 | if (mdsc->stopping && !__get_oldest_req(mdsc)) |
| 2926 | complete_all(&mdsc->safe_umount_waiters); |
| 2927 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2928 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2929 | /* |
| 2930 | * We already handled the unsafe response, now do the |
| 2931 | * cleanup. No need to examine the response; the MDS |
| 2932 | * doesn't include any result info in the safe |
| 2933 | * response. And even if it did, there is nothing |
| 2934 | * useful we could do with a revised return value. |
| 2935 | */ |
| 2936 | dout("got safe reply %llu, mds%d\n", tid, mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2937 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2938 | mutex_unlock(&mdsc->mutex); |
| 2939 | goto out; |
| 2940 | } |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2941 | } else { |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2942 | set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2943 | list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe); |
| 2944 | } |
| 2945 | |
| 2946 | dout("handle_reply tid %lld result %d\n", tid, result); |
| 2947 | rinfo = &req->r_reply_info; |
Yan, Zheng | b37fe1f | 2019-01-09 10:10:17 +0800 | [diff] [blame] | 2948 | if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features)) |
| 2949 | err = parse_reply_info(msg, rinfo, (u64)-1); |
| 2950 | else |
| 2951 | err = parse_reply_info(msg, rinfo, session->s_con.peer_features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2952 | mutex_unlock(&mdsc->mutex); |
| 2953 | |
| 2954 | mutex_lock(&session->s_mutex); |
| 2955 | if (err < 0) { |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 2956 | pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2957 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2958 | goto out_err; |
| 2959 | } |
| 2960 | |
| 2961 | /* snap trace */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2962 | realm = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2963 | if (rinfo->snapblob_len) { |
| 2964 | down_write(&mdsc->snap_rwsem); |
| 2965 | ceph_update_snap_trace(mdsc, rinfo->snapblob, |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2966 | rinfo->snapblob + rinfo->snapblob_len, |
| 2967 | le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP, |
| 2968 | &realm); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2969 | downgrade_write(&mdsc->snap_rwsem); |
| 2970 | } else { |
| 2971 | down_read(&mdsc->snap_rwsem); |
| 2972 | } |
| 2973 | |
| 2974 | /* insert trace into our cache */ |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2975 | mutex_lock(&req->r_fill_mutex); |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 2976 | current->journal_info = req; |
Jeff Layton | f5a03b0 | 2017-01-31 11:06:13 -0500 | [diff] [blame] | 2977 | err = ceph_fill_trace(mdsc->fsc->sb, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2978 | if (err == 0) { |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 2979 | if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR || |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 2980 | req->r_op == CEPH_MDS_OP_LSSNAP)) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2981 | ceph_readdir_prepopulate(req, req->r_session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2982 | } |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 2983 | current->journal_info = NULL; |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2984 | mutex_unlock(&req->r_fill_mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2985 | |
| 2986 | up_read(&mdsc->snap_rwsem); |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2987 | if (realm) |
| 2988 | ceph_put_snap_realm(mdsc, realm); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2989 | |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 2990 | if (err == 0) { |
| 2991 | if (req->r_target_inode && |
| 2992 | test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
| 2993 | struct ceph_inode_info *ci = |
| 2994 | ceph_inode(req->r_target_inode); |
| 2995 | spin_lock(&ci->i_unsafe_lock); |
| 2996 | list_add_tail(&req->r_unsafe_target_item, |
| 2997 | &ci->i_unsafe_iops); |
| 2998 | spin_unlock(&ci->i_unsafe_lock); |
| 2999 | } |
| 3000 | |
| 3001 | ceph_unreserve_caps(mdsc, &req->r_caps_reservation); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 3002 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3003 | out_err: |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 3004 | mutex_lock(&mdsc->mutex); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 3005 | if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 3006 | if (err) { |
| 3007 | req->r_err = err; |
| 3008 | } else { |
Jianpeng Ma | 5fdb138 | 2015-08-18 10:30:38 +0800 | [diff] [blame] | 3009 | req->r_reply = ceph_msg_get(msg); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 3010 | set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 3011 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3012 | } else { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 3013 | dout("reply arrived after request %lld was aborted\n", tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3014 | } |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 3015 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3016 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3017 | mutex_unlock(&session->s_mutex); |
| 3018 | |
| 3019 | /* kick calling process */ |
| 3020 | complete_request(mdsc, req); |
| 3021 | out: |
| 3022 | ceph_mdsc_put_request(req); |
| 3023 | return; |
| 3024 | } |
| 3025 | |
| 3026 | |
| 3027 | |
| 3028 | /* |
| 3029 | * handle mds notification that our request has been forwarded. |
| 3030 | */ |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3031 | static void handle_forward(struct ceph_mds_client *mdsc, |
| 3032 | struct ceph_mds_session *session, |
| 3033 | struct ceph_msg *msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3034 | { |
| 3035 | struct ceph_mds_request *req; |
Sage Weil | a1ea787 | 2010-02-23 14:02:44 -0800 | [diff] [blame] | 3036 | u64 tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3037 | u32 next_mds; |
| 3038 | u32 fwd_seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3039 | int err = -EINVAL; |
| 3040 | void *p = msg->front.iov_base; |
| 3041 | void *end = p + msg->front.iov_len; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3042 | |
Sage Weil | a1ea787 | 2010-02-23 14:02:44 -0800 | [diff] [blame] | 3043 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 3044 | next_mds = ceph_decode_32(&p); |
| 3045 | fwd_seq = ceph_decode_32(&p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3046 | |
| 3047 | mutex_lock(&mdsc->mutex); |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 3048 | req = lookup_get_request(mdsc, tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3049 | if (!req) { |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 3050 | dout("forward tid %llu to mds%d - req dne\n", tid, next_mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3051 | goto out; /* dup reply? */ |
| 3052 | } |
| 3053 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 3054 | if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 3055 | dout("forward tid %llu aborted, unregistering\n", tid); |
| 3056 | __unregister_request(mdsc, req); |
| 3057 | } else if (fwd_seq <= req->r_num_fwd) { |
| 3058 | dout("forward tid %llu to mds%d - old seq %d <= %d\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3059 | tid, next_mds, req->r_num_fwd, fwd_seq); |
| 3060 | } else { |
| 3061 | /* resend. forward race not possible; mds would drop */ |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 3062 | dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds); |
| 3063 | BUG_ON(req->r_err); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 3064 | BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)); |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 3065 | req->r_attempts = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3066 | req->r_num_fwd = fwd_seq; |
| 3067 | req->r_resend_mds = next_mds; |
| 3068 | put_request_session(req); |
| 3069 | __do_request(mdsc, req); |
| 3070 | } |
| 3071 | ceph_mdsc_put_request(req); |
| 3072 | out: |
| 3073 | mutex_unlock(&mdsc->mutex); |
| 3074 | return; |
| 3075 | |
| 3076 | bad: |
| 3077 | pr_err("mdsc_handle_forward decode error err=%d\n", err); |
| 3078 | } |
| 3079 | |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 3080 | static int __decode_session_metadata(void **p, void *end, |
| 3081 | bool *blacklisted) |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3082 | { |
| 3083 | /* map<string,string> */ |
| 3084 | u32 n; |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 3085 | bool err_str; |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3086 | ceph_decode_32_safe(p, end, n, bad); |
| 3087 | while (n-- > 0) { |
| 3088 | u32 len; |
| 3089 | ceph_decode_32_safe(p, end, len, bad); |
| 3090 | ceph_decode_need(p, end, len, bad); |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 3091 | err_str = !strncmp(*p, "error_string", len); |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3092 | *p += len; |
| 3093 | ceph_decode_32_safe(p, end, len, bad); |
| 3094 | ceph_decode_need(p, end, len, bad); |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 3095 | if (err_str && strnstr(*p, "blacklisted", len)) |
| 3096 | *blacklisted = true; |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3097 | *p += len; |
| 3098 | } |
| 3099 | return 0; |
| 3100 | bad: |
| 3101 | return -1; |
| 3102 | } |
| 3103 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3104 | /* |
| 3105 | * handle a mds session control message |
| 3106 | */ |
| 3107 | static void handle_session(struct ceph_mds_session *session, |
| 3108 | struct ceph_msg *msg) |
| 3109 | { |
| 3110 | struct ceph_mds_client *mdsc = session->s_mdsc; |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3111 | int mds = session->s_mds; |
| 3112 | int msg_version = le16_to_cpu(msg->hdr.version); |
| 3113 | void *p = msg->front.iov_base; |
| 3114 | void *end = p + msg->front.iov_len; |
| 3115 | struct ceph_mds_session_head *h; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3116 | u32 op; |
| 3117 | u64 seq; |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3118 | unsigned long features = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3119 | int wake = 0; |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 3120 | bool blacklisted = false; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3121 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3122 | /* decode */ |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3123 | ceph_decode_need(&p, end, sizeof(*h), bad); |
| 3124 | h = p; |
| 3125 | p += sizeof(*h); |
| 3126 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3127 | op = le32_to_cpu(h->op); |
| 3128 | seq = le64_to_cpu(h->seq); |
| 3129 | |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3130 | if (msg_version >= 3) { |
| 3131 | u32 len; |
| 3132 | /* version >= 2, metadata */ |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 3133 | if (__decode_session_metadata(&p, end, &blacklisted) < 0) |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3134 | goto bad; |
| 3135 | /* version >= 3, feature bits */ |
| 3136 | ceph_decode_32_safe(&p, end, len, bad); |
| 3137 | ceph_decode_need(&p, end, len, bad); |
| 3138 | memcpy(&features, p, min_t(size_t, len, sizeof(features))); |
| 3139 | p += len; |
| 3140 | } |
| 3141 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3142 | mutex_lock(&mdsc->mutex); |
Yan, Zheng | 0a07fc8 | 2017-03-29 15:30:24 +0800 | [diff] [blame] | 3143 | if (op == CEPH_SESSION_CLOSE) { |
Xiubo Li | 5b3248c | 2019-12-19 19:44:09 -0500 | [diff] [blame] | 3144 | ceph_get_mds_session(session); |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3145 | __unregister_session(mdsc, session); |
Yan, Zheng | 0a07fc8 | 2017-03-29 15:30:24 +0800 | [diff] [blame] | 3146 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3147 | /* FIXME: this ttl calculation is generous */ |
| 3148 | session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose; |
| 3149 | mutex_unlock(&mdsc->mutex); |
| 3150 | |
| 3151 | mutex_lock(&session->s_mutex); |
| 3152 | |
| 3153 | dout("handle_session mds%d %s %p state %s seq %llu\n", |
| 3154 | mds, ceph_session_op_name(op), session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 3155 | ceph_session_state_name(session->s_state), seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3156 | |
| 3157 | if (session->s_state == CEPH_MDS_SESSION_HUNG) { |
| 3158 | session->s_state = CEPH_MDS_SESSION_OPEN; |
| 3159 | pr_info("mds%d came back\n", session->s_mds); |
| 3160 | } |
| 3161 | |
| 3162 | switch (op) { |
| 3163 | case CEPH_SESSION_OPEN: |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 3164 | if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) |
| 3165 | pr_info("mds%d reconnect success\n", session->s_mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3166 | session->s_state = CEPH_MDS_SESSION_OPEN; |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame] | 3167 | session->s_features = features; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3168 | renewed_caps(mdsc, session, 0); |
| 3169 | wake = 1; |
| 3170 | if (mdsc->stopping) |
| 3171 | __close_session(mdsc, session); |
| 3172 | break; |
| 3173 | |
| 3174 | case CEPH_SESSION_RENEWCAPS: |
| 3175 | if (session->s_renew_seq == seq) |
| 3176 | renewed_caps(mdsc, session, 1); |
| 3177 | break; |
| 3178 | |
| 3179 | case CEPH_SESSION_CLOSE: |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 3180 | if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) |
| 3181 | pr_info("mds%d reconnect denied\n", session->s_mds); |
Xiubo Li | 4d681c2 | 2019-12-05 22:35:51 -0500 | [diff] [blame] | 3182 | session->s_state = CEPH_MDS_SESSION_CLOSED; |
Yan, Zheng | 1c841a9 | 2015-03-24 20:15:36 +0800 | [diff] [blame] | 3183 | cleanup_session_requests(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3184 | remove_session_caps(session); |
Yan, Zheng | 656e438 | 2014-09-11 14:25:18 +0800 | [diff] [blame] | 3185 | wake = 2; /* for good measure */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3186 | wake_up_all(&mdsc->session_close_wq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3187 | break; |
| 3188 | |
| 3189 | case CEPH_SESSION_STALE: |
| 3190 | pr_info("mds%d caps went stale, renewing\n", |
| 3191 | session->s_mds); |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 3192 | spin_lock(&session->s_gen_ttl_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3193 | session->s_cap_gen++; |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 3194 | session->s_cap_ttl = jiffies - 1; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 3195 | spin_unlock(&session->s_gen_ttl_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3196 | send_renew_caps(mdsc, session); |
| 3197 | break; |
| 3198 | |
| 3199 | case CEPH_SESSION_RECALL_STATE: |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 3200 | ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3201 | break; |
| 3202 | |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 3203 | case CEPH_SESSION_FLUSHMSG: |
| 3204 | send_flushmsg_ack(mdsc, session, seq); |
| 3205 | break; |
| 3206 | |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 3207 | case CEPH_SESSION_FORCE_RO: |
| 3208 | dout("force_session_readonly %p\n", session); |
| 3209 | spin_lock(&session->s_cap_lock); |
| 3210 | session->s_readonly = true; |
| 3211 | spin_unlock(&session->s_cap_lock); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 3212 | wake_up_session_caps(session, FORCE_RO); |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 3213 | break; |
| 3214 | |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 3215 | case CEPH_SESSION_REJECT: |
| 3216 | WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING); |
| 3217 | pr_info("mds%d rejected session\n", session->s_mds); |
| 3218 | session->s_state = CEPH_MDS_SESSION_REJECTED; |
| 3219 | cleanup_session_requests(mdsc, session); |
| 3220 | remove_session_caps(session); |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 3221 | if (blacklisted) |
| 3222 | mdsc->fsc->blacklisted = true; |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 3223 | wake = 2; /* for good measure */ |
| 3224 | break; |
| 3225 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3226 | default: |
| 3227 | pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds); |
| 3228 | WARN_ON(1); |
| 3229 | } |
| 3230 | |
| 3231 | mutex_unlock(&session->s_mutex); |
| 3232 | if (wake) { |
| 3233 | mutex_lock(&mdsc->mutex); |
| 3234 | __wake_requests(mdsc, &session->s_waiting); |
Yan, Zheng | 656e438 | 2014-09-11 14:25:18 +0800 | [diff] [blame] | 3235 | if (wake == 2) |
| 3236 | kick_requests(mdsc, mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3237 | mutex_unlock(&mdsc->mutex); |
| 3238 | } |
Yan, Zheng | 0a07fc8 | 2017-03-29 15:30:24 +0800 | [diff] [blame] | 3239 | if (op == CEPH_SESSION_CLOSE) |
| 3240 | ceph_put_mds_session(session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3241 | return; |
| 3242 | |
| 3243 | bad: |
| 3244 | pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds, |
| 3245 | (int)msg->front.iov_len); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 3246 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3247 | return; |
| 3248 | } |
| 3249 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3250 | /* |
| 3251 | * called under session->mutex. |
| 3252 | */ |
| 3253 | static void replay_unsafe_requests(struct ceph_mds_client *mdsc, |
| 3254 | struct ceph_mds_session *session) |
| 3255 | { |
| 3256 | struct ceph_mds_request *req, *nreq; |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 3257 | struct rb_node *p; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3258 | |
| 3259 | dout("replay_unsafe_requests mds%d\n", session->s_mds); |
| 3260 | |
| 3261 | mutex_lock(&mdsc->mutex); |
Xiubo Li | 9cf5456 | 2019-12-05 20:50:21 -0500 | [diff] [blame] | 3262 | list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) |
| 3263 | __send_request(mdsc, session, req, true); |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 3264 | |
| 3265 | /* |
| 3266 | * also re-send old requests when MDS enters reconnect stage. So that MDS |
| 3267 | * can process completed request in clientreplay stage. |
| 3268 | */ |
| 3269 | p = rb_first(&mdsc->request_tree); |
| 3270 | while (p) { |
| 3271 | req = rb_entry(p, struct ceph_mds_request, r_node); |
| 3272 | p = rb_next(p); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 3273 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 3274 | continue; |
| 3275 | if (req->r_attempts == 0) |
| 3276 | continue; /* only old requests */ |
| 3277 | if (req->r_session && |
Xiubo Li | 9cf5456 | 2019-12-05 20:50:21 -0500 | [diff] [blame] | 3278 | req->r_session->s_mds == session->s_mds) |
| 3279 | __send_request(mdsc, session, req, true); |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 3280 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3281 | mutex_unlock(&mdsc->mutex); |
| 3282 | } |
| 3283 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3284 | static int send_reconnect_partial(struct ceph_reconnect_state *recon_state) |
| 3285 | { |
| 3286 | struct ceph_msg *reply; |
| 3287 | struct ceph_pagelist *_pagelist; |
| 3288 | struct page *page; |
| 3289 | __le32 *addr; |
| 3290 | int err = -ENOMEM; |
| 3291 | |
| 3292 | if (!recon_state->allow_multi) |
| 3293 | return -ENOSPC; |
| 3294 | |
| 3295 | /* can't handle message that contains both caps and realm */ |
| 3296 | BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms); |
| 3297 | |
| 3298 | /* pre-allocate new pagelist */ |
| 3299 | _pagelist = ceph_pagelist_alloc(GFP_NOFS); |
| 3300 | if (!_pagelist) |
| 3301 | return -ENOMEM; |
| 3302 | |
| 3303 | reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false); |
| 3304 | if (!reply) |
| 3305 | goto fail_msg; |
| 3306 | |
| 3307 | /* placeholder for nr_caps */ |
| 3308 | err = ceph_pagelist_encode_32(_pagelist, 0); |
| 3309 | if (err < 0) |
| 3310 | goto fail; |
| 3311 | |
| 3312 | if (recon_state->nr_caps) { |
| 3313 | /* currently encoding caps */ |
| 3314 | err = ceph_pagelist_encode_32(recon_state->pagelist, 0); |
| 3315 | if (err) |
| 3316 | goto fail; |
| 3317 | } else { |
| 3318 | /* placeholder for nr_realms (currently encoding relams) */ |
| 3319 | err = ceph_pagelist_encode_32(_pagelist, 0); |
| 3320 | if (err < 0) |
| 3321 | goto fail; |
| 3322 | } |
| 3323 | |
| 3324 | err = ceph_pagelist_encode_8(recon_state->pagelist, 1); |
| 3325 | if (err) |
| 3326 | goto fail; |
| 3327 | |
| 3328 | page = list_first_entry(&recon_state->pagelist->head, struct page, lru); |
| 3329 | addr = kmap_atomic(page); |
| 3330 | if (recon_state->nr_caps) { |
| 3331 | /* currently encoding caps */ |
| 3332 | *addr = cpu_to_le32(recon_state->nr_caps); |
| 3333 | } else { |
| 3334 | /* currently encoding relams */ |
| 3335 | *(addr + 1) = cpu_to_le32(recon_state->nr_realms); |
| 3336 | } |
| 3337 | kunmap_atomic(addr); |
| 3338 | |
| 3339 | reply->hdr.version = cpu_to_le16(5); |
| 3340 | reply->hdr.compat_version = cpu_to_le16(4); |
| 3341 | |
| 3342 | reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length); |
| 3343 | ceph_msg_data_add_pagelist(reply, recon_state->pagelist); |
| 3344 | |
| 3345 | ceph_con_send(&recon_state->session->s_con, reply); |
| 3346 | ceph_pagelist_release(recon_state->pagelist); |
| 3347 | |
| 3348 | recon_state->pagelist = _pagelist; |
| 3349 | recon_state->nr_caps = 0; |
| 3350 | recon_state->nr_realms = 0; |
| 3351 | recon_state->msg_version = 5; |
| 3352 | return 0; |
| 3353 | fail: |
| 3354 | ceph_msg_put(reply); |
| 3355 | fail_msg: |
| 3356 | ceph_pagelist_release(_pagelist); |
| 3357 | return err; |
| 3358 | } |
| 3359 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3360 | /* |
| 3361 | * Encode information about a cap for a reconnect with the MDS. |
| 3362 | */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3363 | static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap, |
| 3364 | void *arg) |
| 3365 | { |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3366 | union { |
| 3367 | struct ceph_mds_cap_reconnect v2; |
| 3368 | struct ceph_mds_cap_reconnect_v1 v1; |
| 3369 | } rec; |
Yan, Zheng | b3f8d68 | 2017-09-11 10:58:55 +0800 | [diff] [blame] | 3370 | struct ceph_inode_info *ci = cap->ci; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3371 | struct ceph_reconnect_state *recon_state = arg; |
| 3372 | struct ceph_pagelist *pagelist = recon_state->pagelist; |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3373 | int err; |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3374 | u64 snap_follows; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3375 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3376 | dout(" adding %p ino %llx.%llx cap %p %lld %s\n", |
| 3377 | inode, ceph_vinop(inode), cap, cap->cap_id, |
| 3378 | ceph_cap_string(cap->issued)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3379 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3380 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3381 | cap->seq = 0; /* reset cap seq */ |
| 3382 | cap->issue_seq = 0; /* and issue_seq */ |
Yan, Zheng | 667ca05 | 2013-05-31 16:25:36 +0800 | [diff] [blame] | 3383 | cap->mseq = 0; /* and migrate_seq */ |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 3384 | cap->cap_gen = cap->session->s_cap_gen; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3385 | |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3386 | if (recon_state->msg_version >= 2) { |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3387 | rec.v2.cap_id = cpu_to_le64(cap->cap_id); |
| 3388 | rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); |
| 3389 | rec.v2.issued = cpu_to_le32(cap->issued); |
| 3390 | rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3391 | rec.v2.pathbase = 0; |
Jeff Layton | ec1dff2 | 2017-10-31 15:51:14 -0400 | [diff] [blame] | 3392 | rec.v2.flock_len = (__force __le32) |
| 3393 | ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1); |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3394 | } else { |
| 3395 | rec.v1.cap_id = cpu_to_le64(cap->cap_id); |
| 3396 | rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); |
| 3397 | rec.v1.issued = cpu_to_le32(cap->issued); |
| 3398 | rec.v1.size = cpu_to_le64(inode->i_size); |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 3399 | ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime); |
| 3400 | ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime); |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3401 | rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3402 | rec.v1.pathbase = 0; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3403 | } |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3404 | |
| 3405 | if (list_empty(&ci->i_cap_snaps)) { |
Yan, Zheng | 92776fd | 2017-08-16 21:42:39 +0800 | [diff] [blame] | 3406 | snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0; |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3407 | } else { |
| 3408 | struct ceph_cap_snap *capsnap = |
| 3409 | list_first_entry(&ci->i_cap_snaps, |
| 3410 | struct ceph_cap_snap, ci_item); |
| 3411 | snap_follows = capsnap->follows; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3412 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3413 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3414 | |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3415 | if (recon_state->msg_version >= 2) { |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 3416 | int num_fcntl_locks, num_flock_locks; |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3417 | struct ceph_filelock *flocks = NULL; |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3418 | size_t struct_len, total_len = sizeof(u64); |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3419 | u8 struct_v = 0; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 3420 | |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3421 | encode_again: |
Yan, Zheng | b3f8d68 | 2017-09-11 10:58:55 +0800 | [diff] [blame] | 3422 | if (rec.v2.flock_len) { |
| 3423 | ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks); |
| 3424 | } else { |
| 3425 | num_fcntl_locks = 0; |
| 3426 | num_flock_locks = 0; |
| 3427 | } |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3428 | if (num_fcntl_locks + num_flock_locks > 0) { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 3429 | flocks = kmalloc_array(num_fcntl_locks + num_flock_locks, |
| 3430 | sizeof(struct ceph_filelock), |
| 3431 | GFP_NOFS); |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3432 | if (!flocks) { |
| 3433 | err = -ENOMEM; |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3434 | goto out_err; |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3435 | } |
| 3436 | err = ceph_encode_locks_to_buffer(inode, flocks, |
| 3437 | num_fcntl_locks, |
| 3438 | num_flock_locks); |
| 3439 | if (err) { |
| 3440 | kfree(flocks); |
| 3441 | flocks = NULL; |
| 3442 | if (err == -ENOSPC) |
| 3443 | goto encode_again; |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3444 | goto out_err; |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3445 | } |
| 3446 | } else { |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3447 | kfree(flocks); |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3448 | flocks = NULL; |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3449 | } |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3450 | |
| 3451 | if (recon_state->msg_version >= 3) { |
| 3452 | /* version, compat_version and struct_len */ |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3453 | total_len += 2 * sizeof(u8) + sizeof(u32); |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3454 | struct_v = 2; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3455 | } |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3456 | /* |
| 3457 | * number of encoded locks is stable, so copy to pagelist |
| 3458 | */ |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3459 | struct_len = 2 * sizeof(u32) + |
| 3460 | (num_fcntl_locks + num_flock_locks) * |
| 3461 | sizeof(struct ceph_filelock); |
| 3462 | rec.v2.flock_len = cpu_to_le32(struct_len); |
| 3463 | |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3464 | struct_len += sizeof(u32) + sizeof(rec.v2); |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3465 | |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3466 | if (struct_v >= 2) |
| 3467 | struct_len += sizeof(u64); /* snap_follows */ |
| 3468 | |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3469 | total_len += struct_len; |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3470 | |
| 3471 | if (pagelist->length + total_len > RECONNECT_MAX_SIZE) { |
| 3472 | err = send_reconnect_partial(recon_state); |
| 3473 | if (err) |
| 3474 | goto out_freeflocks; |
| 3475 | pagelist = recon_state->pagelist; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3476 | } |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3477 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3478 | err = ceph_pagelist_reserve(pagelist, total_len); |
| 3479 | if (err) |
| 3480 | goto out_freeflocks; |
| 3481 | |
| 3482 | ceph_pagelist_encode_64(pagelist, ceph_ino(inode)); |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3483 | if (recon_state->msg_version >= 3) { |
| 3484 | ceph_pagelist_encode_8(pagelist, struct_v); |
| 3485 | ceph_pagelist_encode_8(pagelist, 1); |
| 3486 | ceph_pagelist_encode_32(pagelist, struct_len); |
| 3487 | } |
| 3488 | ceph_pagelist_encode_string(pagelist, NULL, 0); |
| 3489 | ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2)); |
| 3490 | ceph_locks_to_pagelist(flocks, pagelist, |
| 3491 | num_fcntl_locks, num_flock_locks); |
| 3492 | if (struct_v >= 2) |
| 3493 | ceph_pagelist_encode_64(pagelist, snap_follows); |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3494 | out_freeflocks: |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3495 | kfree(flocks); |
Sage Weil | 3612abb | 2010-09-07 15:59:27 -0700 | [diff] [blame] | 3496 | } else { |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3497 | u64 pathbase = 0; |
| 3498 | int pathlen = 0; |
| 3499 | char *path = NULL; |
| 3500 | struct dentry *dentry; |
| 3501 | |
| 3502 | dentry = d_find_alias(inode); |
| 3503 | if (dentry) { |
| 3504 | path = ceph_mdsc_build_path(dentry, |
| 3505 | &pathlen, &pathbase, 0); |
| 3506 | dput(dentry); |
| 3507 | if (IS_ERR(path)) { |
| 3508 | err = PTR_ERR(path); |
| 3509 | goto out_err; |
| 3510 | } |
| 3511 | rec.v1.pathbase = cpu_to_le64(pathbase); |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3512 | } |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3513 | |
| 3514 | err = ceph_pagelist_reserve(pagelist, |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3515 | sizeof(u64) + sizeof(u32) + |
| 3516 | pathlen + sizeof(rec.v1)); |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3517 | if (err) { |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3518 | goto out_freepath; |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3519 | } |
| 3520 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3521 | ceph_pagelist_encode_64(pagelist, ceph_ino(inode)); |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3522 | ceph_pagelist_encode_string(pagelist, path, pathlen); |
| 3523 | ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1)); |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3524 | out_freepath: |
Jeff Layton | f77f21b | 2019-04-29 12:13:14 -0400 | [diff] [blame] | 3525 | ceph_mdsc_free_path(path, pathlen); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 3526 | } |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3527 | |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3528 | out_err: |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3529 | if (err >= 0) |
| 3530 | recon_state->nr_caps++; |
| 3531 | return err; |
| 3532 | } |
| 3533 | |
| 3534 | static int encode_snap_realms(struct ceph_mds_client *mdsc, |
| 3535 | struct ceph_reconnect_state *recon_state) |
| 3536 | { |
| 3537 | struct rb_node *p; |
| 3538 | struct ceph_pagelist *pagelist = recon_state->pagelist; |
| 3539 | int err = 0; |
| 3540 | |
| 3541 | if (recon_state->msg_version >= 4) { |
| 3542 | err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms); |
| 3543 | if (err < 0) |
| 3544 | goto fail; |
| 3545 | } |
| 3546 | |
| 3547 | /* |
| 3548 | * snaprealms. we provide mds with the ino, seq (version), and |
| 3549 | * parent for all of our realms. If the mds has any newer info, |
| 3550 | * it will tell us. |
| 3551 | */ |
| 3552 | for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) { |
| 3553 | struct ceph_snap_realm *realm = |
| 3554 | rb_entry(p, struct ceph_snap_realm, node); |
| 3555 | struct ceph_mds_snaprealm_reconnect sr_rec; |
| 3556 | |
| 3557 | if (recon_state->msg_version >= 4) { |
| 3558 | size_t need = sizeof(u8) * 2 + sizeof(u32) + |
| 3559 | sizeof(sr_rec); |
| 3560 | |
| 3561 | if (pagelist->length + need > RECONNECT_MAX_SIZE) { |
| 3562 | err = send_reconnect_partial(recon_state); |
| 3563 | if (err) |
| 3564 | goto fail; |
| 3565 | pagelist = recon_state->pagelist; |
| 3566 | } |
| 3567 | |
| 3568 | err = ceph_pagelist_reserve(pagelist, need); |
| 3569 | if (err) |
| 3570 | goto fail; |
| 3571 | |
| 3572 | ceph_pagelist_encode_8(pagelist, 1); |
| 3573 | ceph_pagelist_encode_8(pagelist, 1); |
| 3574 | ceph_pagelist_encode_32(pagelist, sizeof(sr_rec)); |
| 3575 | } |
| 3576 | |
| 3577 | dout(" adding snap realm %llx seq %lld parent %llx\n", |
| 3578 | realm->ino, realm->seq, realm->parent_ino); |
| 3579 | sr_rec.ino = cpu_to_le64(realm->ino); |
| 3580 | sr_rec.seq = cpu_to_le64(realm->seq); |
| 3581 | sr_rec.parent = cpu_to_le64(realm->parent_ino); |
| 3582 | |
| 3583 | err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec)); |
| 3584 | if (err) |
| 3585 | goto fail; |
| 3586 | |
| 3587 | recon_state->nr_realms++; |
| 3588 | } |
| 3589 | fail: |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3590 | return err; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | |
| 3594 | /* |
| 3595 | * If an MDS fails and recovers, clients need to reconnect in order to |
| 3596 | * reestablish shared state. This includes all caps issued through |
| 3597 | * this session _and_ the snap_realm hierarchy. Because it's not |
| 3598 | * clear which snap realms the mds cares about, we send everything we |
| 3599 | * know about.. that ensures we'll then get any new info the |
| 3600 | * recovering MDS might have. |
| 3601 | * |
| 3602 | * This is a relatively heavyweight operation, but it's rare. |
| 3603 | * |
| 3604 | * called with mdsc->mutex held. |
| 3605 | */ |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3606 | static void send_mds_reconnect(struct ceph_mds_client *mdsc, |
| 3607 | struct ceph_mds_session *session) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3608 | { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3609 | struct ceph_msg *reply; |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3610 | int mds = session->s_mds; |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3611 | int err = -ENOMEM; |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3612 | struct ceph_reconnect_state recon_state = { |
| 3613 | .session = session, |
| 3614 | }; |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 3615 | LIST_HEAD(dispose); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3616 | |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3617 | pr_info("mds%d reconnect start\n", mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3618 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3619 | recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS); |
| 3620 | if (!recon_state.pagelist) |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3621 | goto fail_nopagelist; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3622 | |
Ilya Dryomov | 0d9c1ab | 2018-10-15 17:38:23 +0200 | [diff] [blame] | 3623 | reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 3624 | if (!reply) |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3625 | goto fail_nomsg; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3626 | |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3627 | mutex_lock(&session->s_mutex); |
| 3628 | session->s_state = CEPH_MDS_SESSION_RECONNECTING; |
| 3629 | session->s_seq = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3630 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3631 | dout("session %p state %s\n", session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 3632 | ceph_session_state_name(session->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3633 | |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 3634 | spin_lock(&session->s_gen_ttl_lock); |
| 3635 | session->s_cap_gen++; |
| 3636 | spin_unlock(&session->s_gen_ttl_lock); |
| 3637 | |
| 3638 | spin_lock(&session->s_cap_lock); |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 3639 | /* don't know if session is readonly */ |
| 3640 | session->s_readonly = 0; |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 3641 | /* |
| 3642 | * notify __ceph_remove_cap() that we are composing cap reconnect. |
| 3643 | * If a cap get released before being added to the cap reconnect, |
| 3644 | * __ceph_remove_cap() should skip queuing cap release. |
| 3645 | */ |
| 3646 | session->s_cap_reconnect = 1; |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 3647 | /* drop old cap expires; we're about to reestablish that state */ |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 3648 | detach_cap_releases(session, &dispose); |
| 3649 | spin_unlock(&session->s_cap_lock); |
| 3650 | dispose_cap_releases(mdsc, &dispose); |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 3651 | |
Yan, Zheng | 5d23371 | 2014-09-10 16:56:23 +0800 | [diff] [blame] | 3652 | /* trim unused caps to reduce MDS's cache rejoin time */ |
Yan, Zheng | c0bd50e | 2015-04-07 15:51:08 +0800 | [diff] [blame] | 3653 | if (mdsc->fsc->sb->s_root) |
| 3654 | shrink_dcache_parent(mdsc->fsc->sb->s_root); |
Yan, Zheng | 5d23371 | 2014-09-10 16:56:23 +0800 | [diff] [blame] | 3655 | |
| 3656 | ceph_con_close(&session->s_con); |
| 3657 | ceph_con_open(&session->s_con, |
| 3658 | CEPH_ENTITY_TYPE_MDS, mds, |
| 3659 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); |
| 3660 | |
| 3661 | /* replay unsafe requests */ |
| 3662 | replay_unsafe_requests(mdsc, session); |
| 3663 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3664 | ceph_early_kick_flushing_caps(mdsc, session); |
| 3665 | |
Yan, Zheng | 5d23371 | 2014-09-10 16:56:23 +0800 | [diff] [blame] | 3666 | down_read(&mdsc->snap_rwsem); |
| 3667 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3668 | /* placeholder for nr_caps */ |
| 3669 | err = ceph_pagelist_encode_32(recon_state.pagelist, 0); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3670 | if (err) |
| 3671 | goto fail; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3672 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3673 | if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) { |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3674 | recon_state.msg_version = 3; |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3675 | recon_state.allow_multi = true; |
| 3676 | } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) { |
| 3677 | recon_state.msg_version = 3; |
| 3678 | } else { |
Ilya Dryomov | 23c625c | 2018-11-08 14:55:21 +0100 | [diff] [blame] | 3679 | recon_state.msg_version = 2; |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3680 | } |
| 3681 | /* trsaverse this session's caps */ |
Jeff Layton | f5d77269 | 2019-04-24 12:09:04 -0400 | [diff] [blame] | 3682 | err = ceph_iterate_session_caps(session, encode_caps_cb, &recon_state); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3683 | |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 3684 | spin_lock(&session->s_cap_lock); |
| 3685 | session->s_cap_reconnect = 0; |
| 3686 | spin_unlock(&session->s_cap_lock); |
| 3687 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3688 | if (err < 0) |
| 3689 | goto fail; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3690 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3691 | /* check if all realms can be encoded into current message */ |
| 3692 | if (mdsc->num_snap_realms) { |
| 3693 | size_t total_len = |
| 3694 | recon_state.pagelist->length + |
| 3695 | mdsc->num_snap_realms * |
| 3696 | sizeof(struct ceph_mds_snaprealm_reconnect); |
| 3697 | if (recon_state.msg_version >= 4) { |
| 3698 | /* number of realms */ |
| 3699 | total_len += sizeof(u32); |
| 3700 | /* version, compat_version and struct_len */ |
| 3701 | total_len += mdsc->num_snap_realms * |
| 3702 | (2 * sizeof(u8) + sizeof(u32)); |
| 3703 | } |
| 3704 | if (total_len > RECONNECT_MAX_SIZE) { |
| 3705 | if (!recon_state.allow_multi) { |
| 3706 | err = -ENOSPC; |
| 3707 | goto fail; |
| 3708 | } |
| 3709 | if (recon_state.nr_caps) { |
| 3710 | err = send_reconnect_partial(&recon_state); |
| 3711 | if (err) |
| 3712 | goto fail; |
| 3713 | } |
| 3714 | recon_state.msg_version = 5; |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | err = encode_snap_realms(mdsc, &recon_state); |
| 3719 | if (err < 0) |
| 3720 | goto fail; |
| 3721 | |
| 3722 | if (recon_state.msg_version >= 5) { |
| 3723 | err = ceph_pagelist_encode_8(recon_state.pagelist, 0); |
| 3724 | if (err < 0) |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3725 | goto fail; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3726 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3727 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3728 | if (recon_state.nr_caps || recon_state.nr_realms) { |
| 3729 | struct page *page = |
| 3730 | list_first_entry(&recon_state.pagelist->head, |
| 3731 | struct page, lru); |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3732 | __le32 *addr = kmap_atomic(page); |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3733 | if (recon_state.nr_caps) { |
| 3734 | WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms); |
| 3735 | *addr = cpu_to_le32(recon_state.nr_caps); |
| 3736 | } else if (recon_state.msg_version >= 4) { |
| 3737 | *(addr + 1) = cpu_to_le32(recon_state.nr_realms); |
| 3738 | } |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3739 | kunmap_atomic(addr); |
Alex Elder | ebf18f4 | 2013-03-04 22:29:57 -0600 | [diff] [blame] | 3740 | } |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3741 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3742 | reply->hdr.version = cpu_to_le16(recon_state.msg_version); |
| 3743 | if (recon_state.msg_version >= 4) |
| 3744 | reply->hdr.compat_version = cpu_to_le16(4); |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 3745 | |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3746 | reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length); |
| 3747 | ceph_msg_data_add_pagelist(reply, recon_state.pagelist); |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 3748 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3749 | ceph_con_send(&session->s_con, reply); |
| 3750 | |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3751 | mutex_unlock(&session->s_mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3752 | |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3753 | mutex_lock(&mdsc->mutex); |
| 3754 | __wake_requests(mdsc, &session->s_waiting); |
| 3755 | mutex_unlock(&mdsc->mutex); |
| 3756 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3757 | up_read(&mdsc->snap_rwsem); |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3758 | ceph_pagelist_release(recon_state.pagelist); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3759 | return; |
| 3760 | |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3761 | fail: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3762 | ceph_msg_put(reply); |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3763 | up_read(&mdsc->snap_rwsem); |
| 3764 | mutex_unlock(&session->s_mutex); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3765 | fail_nomsg: |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 3766 | ceph_pagelist_release(recon_state.pagelist); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3767 | fail_nopagelist: |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3768 | pr_err("error %d preparing reconnect for mds%d\n", err, mds); |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3769 | return; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | |
| 3773 | /* |
| 3774 | * compare old and new mdsmaps, kicking requests |
| 3775 | * and closing out old connections as necessary |
| 3776 | * |
| 3777 | * called under mdsc->mutex. |
| 3778 | */ |
| 3779 | static void check_new_map(struct ceph_mds_client *mdsc, |
| 3780 | struct ceph_mdsmap *newmap, |
| 3781 | struct ceph_mdsmap *oldmap) |
| 3782 | { |
| 3783 | int i; |
| 3784 | int oldstate, newstate; |
| 3785 | struct ceph_mds_session *s; |
| 3786 | |
| 3787 | dout("check_new_map new %u old %u\n", |
| 3788 | newmap->m_epoch, oldmap->m_epoch); |
| 3789 | |
Xiubo Li | b38c9eb | 2019-12-04 06:57:39 -0500 | [diff] [blame] | 3790 | for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) { |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 3791 | if (!mdsc->sessions[i]) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3792 | continue; |
| 3793 | s = mdsc->sessions[i]; |
| 3794 | oldstate = ceph_mdsmap_get_state(oldmap, i); |
| 3795 | newstate = ceph_mdsmap_get_state(newmap, i); |
| 3796 | |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 3797 | dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3798 | i, ceph_mds_state_name(oldstate), |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 3799 | ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3800 | ceph_mds_state_name(newstate), |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 3801 | ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 3802 | ceph_session_state_name(s->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3803 | |
Xiubo Li | b38c9eb | 2019-12-04 06:57:39 -0500 | [diff] [blame] | 3804 | if (i >= newmap->possible_max_rank) { |
Yan, Zheng | 6f0f597 | 2019-06-10 15:45:09 +0800 | [diff] [blame] | 3805 | /* force close session for stopped mds */ |
Xiubo Li | 5b3248c | 2019-12-19 19:44:09 -0500 | [diff] [blame] | 3806 | ceph_get_mds_session(s); |
Yan, Zheng | 6f0f597 | 2019-06-10 15:45:09 +0800 | [diff] [blame] | 3807 | __unregister_session(mdsc, s); |
| 3808 | __wake_requests(mdsc, &s->s_waiting); |
| 3809 | mutex_unlock(&mdsc->mutex); |
| 3810 | |
| 3811 | mutex_lock(&s->s_mutex); |
| 3812 | cleanup_session_requests(mdsc, s); |
| 3813 | remove_session_caps(s); |
| 3814 | mutex_unlock(&s->s_mutex); |
| 3815 | |
| 3816 | ceph_put_mds_session(s); |
| 3817 | |
| 3818 | mutex_lock(&mdsc->mutex); |
| 3819 | kick_requests(mdsc, i); |
| 3820 | continue; |
| 3821 | } |
| 3822 | |
| 3823 | if (memcmp(ceph_mdsmap_get_addr(oldmap, i), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3824 | ceph_mdsmap_get_addr(newmap, i), |
| 3825 | sizeof(struct ceph_entity_addr))) { |
Yan, Zheng | 6f0f597 | 2019-06-10 15:45:09 +0800 | [diff] [blame] | 3826 | /* just close it */ |
| 3827 | mutex_unlock(&mdsc->mutex); |
| 3828 | mutex_lock(&s->s_mutex); |
| 3829 | mutex_lock(&mdsc->mutex); |
| 3830 | ceph_con_close(&s->s_con); |
| 3831 | mutex_unlock(&s->s_mutex); |
| 3832 | s->s_state = CEPH_MDS_SESSION_RESTARTING; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3833 | } else if (oldstate == newstate) { |
| 3834 | continue; /* nothing new with this mds */ |
| 3835 | } |
| 3836 | |
| 3837 | /* |
| 3838 | * send reconnect? |
| 3839 | */ |
| 3840 | if (s->s_state == CEPH_MDS_SESSION_RESTARTING && |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3841 | newstate >= CEPH_MDS_STATE_RECONNECT) { |
| 3842 | mutex_unlock(&mdsc->mutex); |
| 3843 | send_mds_reconnect(mdsc, s); |
| 3844 | mutex_lock(&mdsc->mutex); |
| 3845 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3846 | |
| 3847 | /* |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 3848 | * kick request on any mds that has gone active. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3849 | */ |
| 3850 | if (oldstate < CEPH_MDS_STATE_ACTIVE && |
| 3851 | newstate >= CEPH_MDS_STATE_ACTIVE) { |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 3852 | if (oldstate != CEPH_MDS_STATE_CREATING && |
| 3853 | oldstate != CEPH_MDS_STATE_STARTING) |
| 3854 | pr_info("mds%d recovery completed\n", s->s_mds); |
| 3855 | kick_requests(mdsc, i); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3856 | ceph_kick_flushing_caps(mdsc, s); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 3857 | wake_up_session_caps(s, RECONNECT); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3858 | } |
| 3859 | } |
Sage Weil | cb170a2 | 2010-06-21 13:38:35 -0700 | [diff] [blame] | 3860 | |
Xiubo Li | b38c9eb | 2019-12-04 06:57:39 -0500 | [diff] [blame] | 3861 | for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) { |
Sage Weil | cb170a2 | 2010-06-21 13:38:35 -0700 | [diff] [blame] | 3862 | s = mdsc->sessions[i]; |
| 3863 | if (!s) |
| 3864 | continue; |
| 3865 | if (!ceph_mdsmap_is_laggy(newmap, i)) |
| 3866 | continue; |
| 3867 | if (s->s_state == CEPH_MDS_SESSION_OPEN || |
| 3868 | s->s_state == CEPH_MDS_SESSION_HUNG || |
| 3869 | s->s_state == CEPH_MDS_SESSION_CLOSING) { |
| 3870 | dout(" connecting to export targets of laggy mds%d\n", |
| 3871 | i); |
| 3872 | __open_export_target_sessions(mdsc, s); |
| 3873 | } |
| 3874 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | |
| 3878 | |
| 3879 | /* |
| 3880 | * leases |
| 3881 | */ |
| 3882 | |
| 3883 | /* |
| 3884 | * caller must hold session s_mutex, dentry->d_lock |
| 3885 | */ |
| 3886 | void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry) |
| 3887 | { |
| 3888 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 3889 | |
| 3890 | ceph_put_mds_session(di->lease_session); |
| 3891 | di->lease_session = NULL; |
| 3892 | } |
| 3893 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3894 | static void handle_lease(struct ceph_mds_client *mdsc, |
| 3895 | struct ceph_mds_session *session, |
| 3896 | struct ceph_msg *msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3897 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3898 | struct super_block *sb = mdsc->fsc->sb; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3899 | struct inode *inode; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3900 | struct dentry *parent, *dentry; |
| 3901 | struct ceph_dentry_info *di; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3902 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3903 | struct ceph_mds_lease *h = msg->front.iov_base; |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3904 | u32 seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3905 | struct ceph_vino vino; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3906 | struct qstr dname; |
| 3907 | int release = 0; |
| 3908 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3909 | dout("handle_lease from mds%d\n", mds); |
| 3910 | |
| 3911 | /* decode */ |
| 3912 | if (msg->front.iov_len < sizeof(*h) + sizeof(u32)) |
| 3913 | goto bad; |
| 3914 | vino.ino = le64_to_cpu(h->ino); |
| 3915 | vino.snap = CEPH_NOSNAP; |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3916 | seq = le32_to_cpu(h->seq); |
Yan, Zheng | 0fcf6c0 | 2018-08-03 16:24:49 +0800 | [diff] [blame] | 3917 | dname.len = get_unaligned_le32(h + 1); |
| 3918 | if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3919 | goto bad; |
Yan, Zheng | 0fcf6c0 | 2018-08-03 16:24:49 +0800 | [diff] [blame] | 3920 | dname.name = (void *)(h + 1) + sizeof(u32); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3921 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3922 | /* lookup inode */ |
| 3923 | inode = ceph_find_inode(sb, vino); |
Sage Weil | 2f90b85 | 2011-07-26 11:28:25 -0700 | [diff] [blame] | 3924 | dout("handle_lease %s, ino %llx %p %.*s\n", |
| 3925 | ceph_lease_op_name(h->action), vino.ino, inode, |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3926 | dname.len, dname.name); |
Yan, Zheng | 6cd3bca | 2014-09-17 07:45:12 +0800 | [diff] [blame] | 3927 | |
| 3928 | mutex_lock(&session->s_mutex); |
| 3929 | session->s_seq++; |
| 3930 | |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 3931 | if (!inode) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3932 | dout("handle_lease no inode %llx\n", vino.ino); |
| 3933 | goto release; |
| 3934 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3935 | |
| 3936 | /* dentry */ |
| 3937 | parent = d_find_alias(inode); |
| 3938 | if (!parent) { |
| 3939 | dout("no parent dentry on inode %p\n", inode); |
| 3940 | WARN_ON(1); |
| 3941 | goto release; /* hrm... */ |
| 3942 | } |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 3943 | dname.hash = full_name_hash(parent, dname.name, dname.len); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3944 | dentry = d_lookup(parent, &dname); |
| 3945 | dput(parent); |
| 3946 | if (!dentry) |
| 3947 | goto release; |
| 3948 | |
| 3949 | spin_lock(&dentry->d_lock); |
| 3950 | di = ceph_dentry(dentry); |
| 3951 | switch (h->action) { |
| 3952 | case CEPH_MDS_LEASE_REVOKE: |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 3953 | if (di->lease_session == session) { |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3954 | if (ceph_seq_cmp(di->lease_seq, seq) > 0) |
| 3955 | h->seq = cpu_to_le32(di->lease_seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3956 | __ceph_mdsc_drop_dentry_lease(dentry); |
| 3957 | } |
| 3958 | release = 1; |
| 3959 | break; |
| 3960 | |
| 3961 | case CEPH_MDS_LEASE_RENEW: |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 3962 | if (di->lease_session == session && |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3963 | di->lease_gen == session->s_cap_gen && |
| 3964 | di->lease_renew_from && |
| 3965 | di->lease_renew_after == 0) { |
| 3966 | unsigned long duration = |
Nicholas Mc Guire | 3563dbdd | 2015-02-06 06:52:17 -0500 | [diff] [blame] | 3967 | msecs_to_jiffies(le32_to_cpu(h->duration_ms)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3968 | |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3969 | di->lease_seq = seq; |
Miklos Szeredi | 9b16f03c | 2016-06-22 16:35:04 +0200 | [diff] [blame] | 3970 | di->time = di->lease_renew_from + duration; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3971 | di->lease_renew_after = di->lease_renew_from + |
| 3972 | (duration >> 1); |
| 3973 | di->lease_renew_from = 0; |
| 3974 | } |
| 3975 | break; |
| 3976 | } |
| 3977 | spin_unlock(&dentry->d_lock); |
| 3978 | dput(dentry); |
| 3979 | |
| 3980 | if (!release) |
| 3981 | goto out; |
| 3982 | |
| 3983 | release: |
| 3984 | /* let's just reuse the same message */ |
| 3985 | h->action = CEPH_MDS_LEASE_REVOKE_ACK; |
| 3986 | ceph_msg_get(msg); |
| 3987 | ceph_con_send(&session->s_con, msg); |
| 3988 | |
| 3989 | out: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3990 | mutex_unlock(&session->s_mutex); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 3991 | /* avoid calling iput_final() in mds dispatch threads */ |
| 3992 | ceph_async_iput(inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3993 | return; |
| 3994 | |
| 3995 | bad: |
| 3996 | pr_err("corrupt lease message\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 3997 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4001 | struct dentry *dentry, char action, |
| 4002 | u32 seq) |
| 4003 | { |
| 4004 | struct ceph_msg *msg; |
| 4005 | struct ceph_mds_lease *lease; |
Yan, Zheng | 8f2a98ef | 2019-05-23 10:45:24 +0800 | [diff] [blame] | 4006 | struct inode *dir; |
| 4007 | int len = sizeof(*lease) + sizeof(u32) + NAME_MAX; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4008 | |
Yan, Zheng | 8f2a98ef | 2019-05-23 10:45:24 +0800 | [diff] [blame] | 4009 | dout("lease_send_msg identry %p %s to mds%d\n", |
| 4010 | dentry, ceph_lease_op_name(action), session->s_mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4011 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 4012 | msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 4013 | if (!msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4014 | return; |
| 4015 | lease = msg->front.iov_base; |
| 4016 | lease->action = action; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4017 | lease->seq = cpu_to_le32(seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4018 | |
Yan, Zheng | 8f2a98ef | 2019-05-23 10:45:24 +0800 | [diff] [blame] | 4019 | spin_lock(&dentry->d_lock); |
| 4020 | dir = d_inode(dentry->d_parent); |
| 4021 | lease->ino = cpu_to_le64(ceph_ino(dir)); |
| 4022 | lease->first = lease->last = cpu_to_le64(ceph_snap(dir)); |
| 4023 | |
| 4024 | put_unaligned_le32(dentry->d_name.len, lease + 1); |
| 4025 | memcpy((void *)(lease + 1) + 4, |
| 4026 | dentry->d_name.name, dentry->d_name.len); |
| 4027 | spin_unlock(&dentry->d_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4028 | /* |
| 4029 | * if this is a preemptive lease RELEASE, no need to |
| 4030 | * flush request stream, since the actual request will |
| 4031 | * soon follow. |
| 4032 | */ |
| 4033 | msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE); |
| 4034 | |
| 4035 | ceph_con_send(&session->s_con, msg); |
| 4036 | } |
| 4037 | |
| 4038 | /* |
Yan, Zheng | 7aac453 | 2018-03-13 19:52:20 +0800 | [diff] [blame] | 4039 | * lock unlock sessions, to wait ongoing session activities |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4040 | */ |
Yan, Zheng | 7aac453 | 2018-03-13 19:52:20 +0800 | [diff] [blame] | 4041 | static void lock_unlock_sessions(struct ceph_mds_client *mdsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4042 | { |
| 4043 | int i; |
| 4044 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4045 | mutex_lock(&mdsc->mutex); |
| 4046 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 4047 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); |
| 4048 | if (!s) |
| 4049 | continue; |
| 4050 | mutex_unlock(&mdsc->mutex); |
| 4051 | mutex_lock(&s->s_mutex); |
| 4052 | mutex_unlock(&s->s_mutex); |
| 4053 | ceph_put_mds_session(s); |
| 4054 | mutex_lock(&mdsc->mutex); |
| 4055 | } |
| 4056 | mutex_unlock(&mdsc->mutex); |
| 4057 | } |
| 4058 | |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 4059 | static void maybe_recover_session(struct ceph_mds_client *mdsc) |
| 4060 | { |
| 4061 | struct ceph_fs_client *fsc = mdsc->fsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4062 | |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 4063 | if (!ceph_test_mount_opt(fsc, CLEANRECOVER)) |
| 4064 | return; |
| 4065 | |
| 4066 | if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED) |
| 4067 | return; |
| 4068 | |
| 4069 | if (!READ_ONCE(fsc->blacklisted)) |
| 4070 | return; |
| 4071 | |
| 4072 | if (fsc->last_auto_reconnect && |
| 4073 | time_before(jiffies, fsc->last_auto_reconnect + HZ * 60 * 30)) |
| 4074 | return; |
| 4075 | |
| 4076 | pr_info("auto reconnect after blacklisted\n"); |
| 4077 | fsc->last_auto_reconnect = jiffies; |
| 4078 | ceph_force_reconnect(fsc->sb); |
| 4079 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4080 | |
| 4081 | /* |
| 4082 | * delayed work -- periodically trim expired leases, renew caps with mds |
| 4083 | */ |
| 4084 | static void schedule_delayed(struct ceph_mds_client *mdsc) |
| 4085 | { |
| 4086 | int delay = 5; |
| 4087 | unsigned hz = round_jiffies_relative(HZ * delay); |
| 4088 | schedule_delayed_work(&mdsc->delayed_work, hz); |
| 4089 | } |
| 4090 | |
| 4091 | static void delayed_work(struct work_struct *work) |
| 4092 | { |
| 4093 | int i; |
| 4094 | struct ceph_mds_client *mdsc = |
| 4095 | container_of(work, struct ceph_mds_client, delayed_work.work); |
| 4096 | int renew_interval; |
| 4097 | int renew_caps; |
| 4098 | |
| 4099 | dout("mdsc delayed_work\n"); |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 4100 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4101 | mutex_lock(&mdsc->mutex); |
| 4102 | renew_interval = mdsc->mdsmap->m_session_timeout >> 2; |
| 4103 | renew_caps = time_after_eq(jiffies, HZ*renew_interval + |
| 4104 | mdsc->last_renew_caps); |
| 4105 | if (renew_caps) |
| 4106 | mdsc->last_renew_caps = jiffies; |
| 4107 | |
| 4108 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 4109 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 4110 | if (!s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4111 | continue; |
| 4112 | if (s->s_state == CEPH_MDS_SESSION_CLOSING) { |
| 4113 | dout("resending session close request for mds%d\n", |
| 4114 | s->s_mds); |
| 4115 | request_close_session(mdsc, s); |
| 4116 | ceph_put_mds_session(s); |
| 4117 | continue; |
| 4118 | } |
| 4119 | if (s->s_ttl && time_after(jiffies, s->s_ttl)) { |
| 4120 | if (s->s_state == CEPH_MDS_SESSION_OPEN) { |
| 4121 | s->s_state = CEPH_MDS_SESSION_HUNG; |
| 4122 | pr_info("mds%d hung\n", s->s_mds); |
| 4123 | } |
| 4124 | } |
Erqi Chen | 71a228b | 2019-08-28 21:22:45 +0800 | [diff] [blame] | 4125 | if (s->s_state == CEPH_MDS_SESSION_NEW || |
| 4126 | s->s_state == CEPH_MDS_SESSION_RESTARTING || |
| 4127 | s->s_state == CEPH_MDS_SESSION_REJECTED) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4128 | /* this mds is failed or recovering, just wait */ |
| 4129 | ceph_put_mds_session(s); |
| 4130 | continue; |
| 4131 | } |
| 4132 | mutex_unlock(&mdsc->mutex); |
| 4133 | |
| 4134 | mutex_lock(&s->s_mutex); |
| 4135 | if (renew_caps) |
| 4136 | send_renew_caps(mdsc, s); |
| 4137 | else |
| 4138 | ceph_con_keepalive(&s->s_con); |
Sage Weil | aab53dd | 2010-03-17 16:30:21 -0700 | [diff] [blame] | 4139 | if (s->s_state == CEPH_MDS_SESSION_OPEN || |
| 4140 | s->s_state == CEPH_MDS_SESSION_HUNG) |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 4141 | ceph_send_cap_releases(mdsc, s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4142 | mutex_unlock(&s->s_mutex); |
| 4143 | ceph_put_mds_session(s); |
| 4144 | |
| 4145 | mutex_lock(&mdsc->mutex); |
| 4146 | } |
| 4147 | mutex_unlock(&mdsc->mutex); |
| 4148 | |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 4149 | ceph_check_delayed_caps(mdsc); |
| 4150 | |
| 4151 | ceph_queue_cap_reclaim_work(mdsc); |
| 4152 | |
| 4153 | ceph_trim_snapid_map(mdsc); |
| 4154 | |
Yan, Zheng | 131d7eb | 2019-07-25 20:16:47 +0800 | [diff] [blame] | 4155 | maybe_recover_session(mdsc); |
| 4156 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4157 | schedule_delayed(mdsc); |
| 4158 | } |
| 4159 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4160 | int ceph_mdsc_init(struct ceph_fs_client *fsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4161 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4162 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4163 | struct ceph_mds_client *mdsc; |
| 4164 | |
| 4165 | mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS); |
| 4166 | if (!mdsc) |
| 4167 | return -ENOMEM; |
| 4168 | mdsc->fsc = fsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4169 | mutex_init(&mdsc->mutex); |
| 4170 | mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 4171 | if (!mdsc->mdsmap) { |
majianpeng | fb3101b | 2013-06-25 14:48:19 +0800 | [diff] [blame] | 4172 | kfree(mdsc); |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 4173 | return -ENOMEM; |
majianpeng | fb3101b | 2013-06-25 14:48:19 +0800 | [diff] [blame] | 4174 | } |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 4175 | |
Chengguang Xu | 50c55ae | 2018-03-14 13:47:33 +0800 | [diff] [blame] | 4176 | fsc->mdsc = mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4177 | init_completion(&mdsc->safe_umount_waiters); |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4178 | init_waitqueue_head(&mdsc->session_close_wq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4179 | INIT_LIST_HEAD(&mdsc->waiting_for_map); |
| 4180 | mdsc->sessions = NULL; |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 4181 | atomic_set(&mdsc->num_sessions, 0); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4182 | mdsc->max_sessions = 0; |
| 4183 | mdsc->stopping = 0; |
Luis Henriques | d557c48 | 2018-01-12 17:19:29 +0000 | [diff] [blame] | 4184 | atomic64_set(&mdsc->quotarealms_count, 0); |
Luis Henriques | 0c44a8e | 2019-03-21 10:20:10 +0000 | [diff] [blame] | 4185 | mdsc->quotarealms_inodes = RB_ROOT; |
| 4186 | mutex_init(&mdsc->quotarealms_inodes_mutex); |
Yan, Zheng | affbc19 | 2015-05-05 21:22:13 +0800 | [diff] [blame] | 4187 | mdsc->last_snap_seq = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4188 | init_rwsem(&mdsc->snap_rwsem); |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 4189 | mdsc->snap_realms = RB_ROOT; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4190 | INIT_LIST_HEAD(&mdsc->snap_empty); |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 4191 | mdsc->num_snap_realms = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4192 | spin_lock_init(&mdsc->snap_empty_lock); |
| 4193 | mdsc->last_tid = 0; |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 4194 | mdsc->oldest_tid = 0; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4195 | mdsc->request_tree = RB_ROOT; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4196 | INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); |
| 4197 | mdsc->last_renew_caps = jiffies; |
| 4198 | INIT_LIST_HEAD(&mdsc->cap_delay_list); |
Jeff Layton | 3a3430a | 2019-11-20 12:00:59 -0500 | [diff] [blame] | 4199 | INIT_LIST_HEAD(&mdsc->cap_wait_list); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4200 | spin_lock_init(&mdsc->cap_delay_lock); |
| 4201 | INIT_LIST_HEAD(&mdsc->snap_flush_list); |
| 4202 | spin_lock_init(&mdsc->snap_flush_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 4203 | mdsc->last_cap_flush_tid = 1; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 4204 | INIT_LIST_HEAD(&mdsc->cap_flush_list); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4205 | INIT_LIST_HEAD(&mdsc->cap_dirty); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4206 | INIT_LIST_HEAD(&mdsc->cap_dirty_migrating); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4207 | mdsc->num_cap_flushing = 0; |
| 4208 | spin_lock_init(&mdsc->cap_dirty_lock); |
| 4209 | init_waitqueue_head(&mdsc->cap_flushing_wq); |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 4210 | INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work); |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 4211 | atomic_set(&mdsc->cap_reclaim_pending, 0); |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 4212 | |
| 4213 | spin_lock_init(&mdsc->dentry_list_lock); |
| 4214 | INIT_LIST_HEAD(&mdsc->dentry_leases); |
| 4215 | INIT_LIST_HEAD(&mdsc->dentry_dir_leases); |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 4216 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 4217 | ceph_caps_init(mdsc); |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 4218 | ceph_adjust_caps_max_min(mdsc, fsc->mount_options); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 4219 | |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 4220 | spin_lock_init(&mdsc->snapid_map_lock); |
| 4221 | mdsc->snapid_map_tree = RB_ROOT; |
| 4222 | INIT_LIST_HEAD(&mdsc->snapid_map_lru); |
| 4223 | |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 4224 | init_rwsem(&mdsc->pool_perm_rwsem); |
| 4225 | mdsc->pool_perm_tree = RB_ROOT; |
| 4226 | |
Yan, Zheng | dfeb84d | 2018-07-02 15:55:23 +0800 | [diff] [blame] | 4227 | strscpy(mdsc->nodename, utsname()->nodename, |
| 4228 | sizeof(mdsc->nodename)); |
Sage Weil | 5f44f14 | 2009-11-18 14:52:18 -0800 | [diff] [blame] | 4229 | return 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | /* |
| 4233 | * Wait for safe replies on open mds requests. If we time out, drop |
| 4234 | * all requests from the tree to avoid dangling dentry refs. |
| 4235 | */ |
| 4236 | static void wait_requests(struct ceph_mds_client *mdsc) |
| 4237 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 4238 | struct ceph_options *opts = mdsc->fsc->client->options; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4239 | struct ceph_mds_request *req; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4240 | |
| 4241 | mutex_lock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4242 | if (__get_oldest_req(mdsc)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4243 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4244 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4245 | dout("wait_requests waiting for requests\n"); |
| 4246 | wait_for_completion_timeout(&mdsc->safe_umount_waiters, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 4247 | ceph_timeout_jiffies(opts->mount_timeout)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4248 | |
| 4249 | /* tear down remaining requests */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4250 | mutex_lock(&mdsc->mutex); |
| 4251 | while ((req = __get_oldest_req(mdsc))) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4252 | dout("wait_requests timed out on tid %llu\n", |
| 4253 | req->r_tid); |
Yan, Zheng | 428138c | 2019-06-14 10:55:05 +0800 | [diff] [blame] | 4254 | list_del_init(&req->r_wait); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4255 | __unregister_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4256 | } |
| 4257 | } |
| 4258 | mutex_unlock(&mdsc->mutex); |
| 4259 | dout("wait_requests done\n"); |
| 4260 | } |
| 4261 | |
| 4262 | /* |
| 4263 | * called before mount is ro, and before dentries are torn down. |
| 4264 | * (hmm, does this still race with new lookups?) |
| 4265 | */ |
| 4266 | void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc) |
| 4267 | { |
| 4268 | dout("pre_umount\n"); |
| 4269 | mdsc->stopping = 1; |
| 4270 | |
Yan, Zheng | 7aac453 | 2018-03-13 19:52:20 +0800 | [diff] [blame] | 4271 | lock_unlock_sessions(mdsc); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4272 | ceph_flush_dirty_caps(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4273 | wait_requests(mdsc); |
Sage Weil | 17c688c | 2010-06-21 16:12:26 -0700 | [diff] [blame] | 4274 | |
| 4275 | /* |
| 4276 | * wait for reply handlers to drop their request refs and |
| 4277 | * their inode/dcache refs |
| 4278 | */ |
| 4279 | ceph_msgr_flush(); |
Luis Henriques | 0c44a8e | 2019-03-21 10:20:10 +0000 | [diff] [blame] | 4280 | |
| 4281 | ceph_cleanup_quotarealms_inodes(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | /* |
| 4285 | * wait for all write mds requests to flush. |
| 4286 | */ |
| 4287 | static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid) |
| 4288 | { |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 4289 | struct ceph_mds_request *req = NULL, *nextreq; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4290 | struct rb_node *n; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4291 | |
| 4292 | mutex_lock(&mdsc->mutex); |
| 4293 | dout("wait_unsafe_requests want %lld\n", want_tid); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 4294 | restart: |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4295 | req = __get_oldest_req(mdsc); |
| 4296 | while (req && req->r_tid <= want_tid) { |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 4297 | /* find next request */ |
| 4298 | n = rb_next(&req->r_node); |
| 4299 | if (n) |
| 4300 | nextreq = rb_entry(n, struct ceph_mds_request, r_node); |
| 4301 | else |
| 4302 | nextreq = NULL; |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 4303 | if (req->r_op != CEPH_MDS_OP_SETFILELOCK && |
| 4304 | (req->r_op & CEPH_MDS_OP_WRITE)) { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4305 | /* write op */ |
| 4306 | ceph_mdsc_get_request(req); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 4307 | if (nextreq) |
| 4308 | ceph_mdsc_get_request(nextreq); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4309 | mutex_unlock(&mdsc->mutex); |
| 4310 | dout("wait_unsafe_requests wait on %llu (want %llu)\n", |
| 4311 | req->r_tid, want_tid); |
| 4312 | wait_for_completion(&req->r_safe_completion); |
| 4313 | mutex_lock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4314 | ceph_mdsc_put_request(req); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 4315 | if (!nextreq) |
| 4316 | break; /* next dne before, so we're done! */ |
| 4317 | if (RB_EMPTY_NODE(&nextreq->r_node)) { |
| 4318 | /* next request was removed from tree */ |
| 4319 | ceph_mdsc_put_request(nextreq); |
| 4320 | goto restart; |
| 4321 | } |
| 4322 | ceph_mdsc_put_request(nextreq); /* won't go away */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 4323 | } |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 4324 | req = nextreq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4325 | } |
| 4326 | mutex_unlock(&mdsc->mutex); |
| 4327 | dout("wait_unsafe_requests done\n"); |
| 4328 | } |
| 4329 | |
| 4330 | void ceph_mdsc_sync(struct ceph_mds_client *mdsc) |
| 4331 | { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 4332 | u64 want_tid, want_flush; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4333 | |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 4334 | if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) |
Sage Weil | 56b7cf9 | 2010-05-03 15:22:00 -0700 | [diff] [blame] | 4335 | return; |
| 4336 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4337 | dout("sync\n"); |
| 4338 | mutex_lock(&mdsc->mutex); |
| 4339 | want_tid = mdsc->last_tid; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4340 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4341 | |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4342 | ceph_flush_dirty_caps(mdsc); |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 4343 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 4344 | want_flush = mdsc->last_cap_flush_tid; |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 4345 | if (!list_empty(&mdsc->cap_flush_list)) { |
| 4346 | struct ceph_cap_flush *cf = |
| 4347 | list_last_entry(&mdsc->cap_flush_list, |
| 4348 | struct ceph_cap_flush, g_list); |
| 4349 | cf->wake = true; |
| 4350 | } |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 4351 | spin_unlock(&mdsc->cap_dirty_lock); |
| 4352 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 4353 | dout("sync want tid %lld flush_seq %lld\n", |
| 4354 | want_tid, want_flush); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4355 | |
| 4356 | wait_unsafe_requests(mdsc, want_tid); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 4357 | wait_caps_flush(mdsc, want_flush); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4358 | } |
| 4359 | |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4360 | /* |
| 4361 | * true if all sessions are closed, or we force unmount |
| 4362 | */ |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 4363 | static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped) |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4364 | { |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 4365 | if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4366 | return true; |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 4367 | return atomic_read(&mdsc->num_sessions) <= skipped; |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4368 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4369 | |
| 4370 | /* |
| 4371 | * called after sb is ro. |
| 4372 | */ |
| 4373 | void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc) |
| 4374 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 4375 | struct ceph_options *opts = mdsc->fsc->client->options; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4376 | struct ceph_mds_session *session; |
| 4377 | int i; |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 4378 | int skipped = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4379 | |
| 4380 | dout("close_sessions\n"); |
| 4381 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4382 | /* close sessions */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4383 | mutex_lock(&mdsc->mutex); |
| 4384 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 4385 | session = __ceph_lookup_mds_session(mdsc, i); |
| 4386 | if (!session) |
| 4387 | continue; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4388 | mutex_unlock(&mdsc->mutex); |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4389 | mutex_lock(&session->s_mutex); |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 4390 | if (__close_session(mdsc, session) <= 0) |
| 4391 | skipped++; |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4392 | mutex_unlock(&session->s_mutex); |
| 4393 | ceph_put_mds_session(session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4394 | mutex_lock(&mdsc->mutex); |
| 4395 | } |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4396 | mutex_unlock(&mdsc->mutex); |
| 4397 | |
| 4398 | dout("waiting for sessions to close\n"); |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 4399 | wait_event_timeout(mdsc->session_close_wq, |
| 4400 | done_closing_sessions(mdsc, skipped), |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 4401 | ceph_timeout_jiffies(opts->mount_timeout)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4402 | |
| 4403 | /* tear down remaining sessions */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 4404 | mutex_lock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4405 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 4406 | if (mdsc->sessions[i]) { |
Xiubo Li | 5b3248c | 2019-12-19 19:44:09 -0500 | [diff] [blame] | 4407 | session = ceph_get_mds_session(mdsc->sessions[i]); |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4408 | __unregister_session(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4409 | mutex_unlock(&mdsc->mutex); |
| 4410 | mutex_lock(&session->s_mutex); |
| 4411 | remove_session_caps(session); |
| 4412 | mutex_unlock(&session->s_mutex); |
| 4413 | ceph_put_mds_session(session); |
| 4414 | mutex_lock(&mdsc->mutex); |
| 4415 | } |
| 4416 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4417 | WARN_ON(!list_empty(&mdsc->cap_delay_list)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4418 | mutex_unlock(&mdsc->mutex); |
| 4419 | |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 4420 | ceph_cleanup_snapid_map(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4421 | ceph_cleanup_empty_realms(mdsc); |
| 4422 | |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 4423 | cancel_work_sync(&mdsc->cap_reclaim_work); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4424 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ |
| 4425 | |
| 4426 | dout("stopped\n"); |
| 4427 | } |
| 4428 | |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 4429 | void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc) |
| 4430 | { |
| 4431 | struct ceph_mds_session *session; |
| 4432 | int mds; |
| 4433 | |
| 4434 | dout("force umount\n"); |
| 4435 | |
| 4436 | mutex_lock(&mdsc->mutex); |
| 4437 | for (mds = 0; mds < mdsc->max_sessions; mds++) { |
| 4438 | session = __ceph_lookup_mds_session(mdsc, mds); |
| 4439 | if (!session) |
| 4440 | continue; |
Yan, Zheng | d468e72 | 2019-07-25 20:16:44 +0800 | [diff] [blame] | 4441 | |
| 4442 | if (session->s_state == CEPH_MDS_SESSION_REJECTED) |
| 4443 | __unregister_session(mdsc, session); |
| 4444 | __wake_requests(mdsc, &session->s_waiting); |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 4445 | mutex_unlock(&mdsc->mutex); |
Yan, Zheng | d468e72 | 2019-07-25 20:16:44 +0800 | [diff] [blame] | 4446 | |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 4447 | mutex_lock(&session->s_mutex); |
| 4448 | __close_session(mdsc, session); |
| 4449 | if (session->s_state == CEPH_MDS_SESSION_CLOSING) { |
| 4450 | cleanup_session_requests(mdsc, session); |
| 4451 | remove_session_caps(session); |
| 4452 | } |
| 4453 | mutex_unlock(&session->s_mutex); |
| 4454 | ceph_put_mds_session(session); |
Yan, Zheng | d468e72 | 2019-07-25 20:16:44 +0800 | [diff] [blame] | 4455 | |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 4456 | mutex_lock(&mdsc->mutex); |
| 4457 | kick_requests(mdsc, mds); |
| 4458 | } |
| 4459 | __wake_requests(mdsc, &mdsc->waiting_for_map); |
| 4460 | mutex_unlock(&mdsc->mutex); |
| 4461 | } |
| 4462 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4463 | static void ceph_mdsc_stop(struct ceph_mds_client *mdsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4464 | { |
| 4465 | dout("stop\n"); |
| 4466 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ |
| 4467 | if (mdsc->mdsmap) |
| 4468 | ceph_mdsmap_destroy(mdsc->mdsmap); |
| 4469 | kfree(mdsc->sessions); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 4470 | ceph_caps_finalize(mdsc); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 4471 | ceph_pool_perm_destroy(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4474 | void ceph_mdsc_destroy(struct ceph_fs_client *fsc) |
| 4475 | { |
| 4476 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 4477 | dout("mdsc_destroy %p\n", mdsc); |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 4478 | |
Chengguang Xu | 50c55ae | 2018-03-14 13:47:33 +0800 | [diff] [blame] | 4479 | if (!mdsc) |
| 4480 | return; |
| 4481 | |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 4482 | /* flush out any connection work with references to us */ |
| 4483 | ceph_msgr_flush(); |
| 4484 | |
Yan, Zheng | 62a65f3 | 2017-06-22 16:26:34 +0800 | [diff] [blame] | 4485 | ceph_mdsc_stop(mdsc); |
| 4486 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4487 | fsc->mdsc = NULL; |
| 4488 | kfree(mdsc); |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 4489 | dout("mdsc_destroy %p done\n", mdsc); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4490 | } |
| 4491 | |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4492 | void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg) |
| 4493 | { |
| 4494 | struct ceph_fs_client *fsc = mdsc->fsc; |
| 4495 | const char *mds_namespace = fsc->mount_options->mds_namespace; |
| 4496 | void *p = msg->front.iov_base; |
| 4497 | void *end = p + msg->front.iov_len; |
| 4498 | u32 epoch; |
| 4499 | u32 map_len; |
| 4500 | u32 num_fs; |
| 4501 | u32 mount_fscid = (u32)-1; |
| 4502 | u8 struct_v, struct_cv; |
| 4503 | int err = -EINVAL; |
| 4504 | |
| 4505 | ceph_decode_need(&p, end, sizeof(u32), bad); |
| 4506 | epoch = ceph_decode_32(&p); |
| 4507 | |
| 4508 | dout("handle_fsmap epoch %u\n", epoch); |
| 4509 | |
| 4510 | ceph_decode_need(&p, end, 2 + sizeof(u32), bad); |
| 4511 | struct_v = ceph_decode_8(&p); |
| 4512 | struct_cv = ceph_decode_8(&p); |
| 4513 | map_len = ceph_decode_32(&p); |
| 4514 | |
| 4515 | ceph_decode_need(&p, end, sizeof(u32) * 3, bad); |
| 4516 | p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */ |
| 4517 | |
| 4518 | num_fs = ceph_decode_32(&p); |
| 4519 | while (num_fs-- > 0) { |
| 4520 | void *info_p, *info_end; |
| 4521 | u32 info_len; |
| 4522 | u8 info_v, info_cv; |
| 4523 | u32 fscid, namelen; |
| 4524 | |
| 4525 | ceph_decode_need(&p, end, 2 + sizeof(u32), bad); |
| 4526 | info_v = ceph_decode_8(&p); |
| 4527 | info_cv = ceph_decode_8(&p); |
| 4528 | info_len = ceph_decode_32(&p); |
| 4529 | ceph_decode_need(&p, end, info_len, bad); |
| 4530 | info_p = p; |
| 4531 | info_end = p + info_len; |
| 4532 | p = info_end; |
| 4533 | |
| 4534 | ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad); |
| 4535 | fscid = ceph_decode_32(&info_p); |
| 4536 | namelen = ceph_decode_32(&info_p); |
| 4537 | ceph_decode_need(&info_p, info_end, namelen, bad); |
| 4538 | |
| 4539 | if (mds_namespace && |
| 4540 | strlen(mds_namespace) == namelen && |
| 4541 | !strncmp(mds_namespace, (char *)info_p, namelen)) { |
| 4542 | mount_fscid = fscid; |
| 4543 | break; |
| 4544 | } |
| 4545 | } |
| 4546 | |
| 4547 | ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch); |
| 4548 | if (mount_fscid != (u32)-1) { |
| 4549 | fsc->client->monc.fs_cluster_id = mount_fscid; |
| 4550 | ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP, |
| 4551 | 0, true); |
| 4552 | ceph_monc_renew_subs(&fsc->client->monc); |
| 4553 | } else { |
| 4554 | err = -ENOENT; |
| 4555 | goto err_out; |
| 4556 | } |
| 4557 | return; |
Ilya Dryomov | 76bd6ec | 2017-10-16 10:32:50 +0200 | [diff] [blame] | 4558 | |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4559 | bad: |
| 4560 | pr_err("error decoding fsmap\n"); |
| 4561 | err_out: |
| 4562 | mutex_lock(&mdsc->mutex); |
Ilya Dryomov | 76bd6ec | 2017-10-16 10:32:50 +0200 | [diff] [blame] | 4563 | mdsc->mdsmap_err = err; |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4564 | __wake_requests(mdsc, &mdsc->waiting_for_map); |
| 4565 | mutex_unlock(&mdsc->mutex); |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4566 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4567 | |
| 4568 | /* |
| 4569 | * handle mds map update. |
| 4570 | */ |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4571 | void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4572 | { |
| 4573 | u32 epoch; |
| 4574 | u32 maplen; |
| 4575 | void *p = msg->front.iov_base; |
| 4576 | void *end = p + msg->front.iov_len; |
| 4577 | struct ceph_mdsmap *newmap, *oldmap; |
| 4578 | struct ceph_fsid fsid; |
| 4579 | int err = -EINVAL; |
| 4580 | |
| 4581 | ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad); |
| 4582 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4583 | if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0) |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 4584 | return; |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 4585 | epoch = ceph_decode_32(&p); |
| 4586 | maplen = ceph_decode_32(&p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4587 | dout("handle_map epoch %u len %d\n", epoch, (int)maplen); |
| 4588 | |
| 4589 | /* do we need it? */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4590 | mutex_lock(&mdsc->mutex); |
| 4591 | if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) { |
| 4592 | dout("handle_map epoch %u <= our %u\n", |
| 4593 | epoch, mdsc->mdsmap->m_epoch); |
| 4594 | mutex_unlock(&mdsc->mutex); |
| 4595 | return; |
| 4596 | } |
| 4597 | |
| 4598 | newmap = ceph_mdsmap_decode(&p, end); |
| 4599 | if (IS_ERR(newmap)) { |
| 4600 | err = PTR_ERR(newmap); |
| 4601 | goto bad_unlock; |
| 4602 | } |
| 4603 | |
| 4604 | /* swap into place */ |
| 4605 | if (mdsc->mdsmap) { |
| 4606 | oldmap = mdsc->mdsmap; |
| 4607 | mdsc->mdsmap = newmap; |
| 4608 | check_new_map(mdsc, newmap, oldmap); |
| 4609 | ceph_mdsmap_destroy(oldmap); |
| 4610 | } else { |
| 4611 | mdsc->mdsmap = newmap; /* first mds map */ |
| 4612 | } |
Chengguang Xu | 719784b | 2018-07-19 22:15:24 +0800 | [diff] [blame] | 4613 | mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size, |
| 4614 | MAX_LFS_FILESIZE); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4615 | |
| 4616 | __wake_requests(mdsc, &mdsc->waiting_for_map); |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame] | 4617 | ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP, |
| 4618 | mdsc->mdsmap->m_epoch); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4619 | |
| 4620 | mutex_unlock(&mdsc->mutex); |
| 4621 | schedule_delayed(mdsc); |
| 4622 | return; |
| 4623 | |
| 4624 | bad_unlock: |
| 4625 | mutex_unlock(&mdsc->mutex); |
| 4626 | bad: |
| 4627 | pr_err("error decoding mdsmap %d\n", err); |
| 4628 | return; |
| 4629 | } |
| 4630 | |
| 4631 | static struct ceph_connection *con_get(struct ceph_connection *con) |
| 4632 | { |
| 4633 | struct ceph_mds_session *s = con->private; |
| 4634 | |
Xiubo Li | 5b3248c | 2019-12-19 19:44:09 -0500 | [diff] [blame] | 4635 | if (ceph_get_mds_session(s)) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4636 | return con; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4637 | return NULL; |
| 4638 | } |
| 4639 | |
| 4640 | static void con_put(struct ceph_connection *con) |
| 4641 | { |
| 4642 | struct ceph_mds_session *s = con->private; |
| 4643 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4644 | ceph_put_mds_session(s); |
| 4645 | } |
| 4646 | |
| 4647 | /* |
| 4648 | * if the client is unresponsive for long enough, the mds will kill |
| 4649 | * the session entirely. |
| 4650 | */ |
| 4651 | static void peer_reset(struct ceph_connection *con) |
| 4652 | { |
| 4653 | struct ceph_mds_session *s = con->private; |
Sage Weil | 7e70f0e | 2010-03-18 13:59:12 -0700 | [diff] [blame] | 4654 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4655 | |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 4656 | pr_warn("mds%d closed our session\n", s->s_mds); |
Sage Weil | 7e70f0e | 2010-03-18 13:59:12 -0700 | [diff] [blame] | 4657 | send_mds_reconnect(mdsc, s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) |
| 4661 | { |
| 4662 | struct ceph_mds_session *s = con->private; |
| 4663 | struct ceph_mds_client *mdsc = s->s_mdsc; |
| 4664 | int type = le16_to_cpu(msg->hdr.type); |
| 4665 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4666 | mutex_lock(&mdsc->mutex); |
| 4667 | if (__verify_registered_session(mdsc, s) < 0) { |
| 4668 | mutex_unlock(&mdsc->mutex); |
| 4669 | goto out; |
| 4670 | } |
| 4671 | mutex_unlock(&mdsc->mutex); |
| 4672 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4673 | switch (type) { |
| 4674 | case CEPH_MSG_MDS_MAP: |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4675 | ceph_mdsc_handle_mdsmap(mdsc, msg); |
| 4676 | break; |
| 4677 | case CEPH_MSG_FS_MAP_USER: |
| 4678 | ceph_mdsc_handle_fsmap(mdsc, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4679 | break; |
| 4680 | case CEPH_MSG_CLIENT_SESSION: |
| 4681 | handle_session(s, msg); |
| 4682 | break; |
| 4683 | case CEPH_MSG_CLIENT_REPLY: |
| 4684 | handle_reply(s, msg); |
| 4685 | break; |
| 4686 | case CEPH_MSG_CLIENT_REQUEST_FORWARD: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4687 | handle_forward(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4688 | break; |
| 4689 | case CEPH_MSG_CLIENT_CAPS: |
| 4690 | ceph_handle_caps(s, msg); |
| 4691 | break; |
| 4692 | case CEPH_MSG_CLIENT_SNAP: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4693 | ceph_handle_snap(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4694 | break; |
| 4695 | case CEPH_MSG_CLIENT_LEASE: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4696 | handle_lease(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4697 | break; |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 4698 | case CEPH_MSG_CLIENT_QUOTA: |
| 4699 | ceph_handle_quota(mdsc, s, msg); |
| 4700 | break; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4701 | |
| 4702 | default: |
| 4703 | pr_err("received unknown message type %d %s\n", type, |
| 4704 | ceph_msg_type_name(type)); |
| 4705 | } |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4706 | out: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4707 | ceph_msg_put(msg); |
| 4708 | } |
| 4709 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4710 | /* |
| 4711 | * authentication |
| 4712 | */ |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4713 | |
| 4714 | /* |
| 4715 | * Note: returned pointer is the address of a structure that's |
| 4716 | * managed separately. Caller must *not* attempt to free it. |
| 4717 | */ |
| 4718 | static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, |
Alex Elder | 8f43fb5 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4719 | int *proto, int force_new) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4720 | { |
| 4721 | struct ceph_mds_session *s = con->private; |
| 4722 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4723 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4724 | struct ceph_auth_handshake *auth = &s->s_auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4725 | |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4726 | if (force_new && auth->authorizer) { |
Ilya Dryomov | 6c1ea26 | 2016-04-11 19:34:49 +0200 | [diff] [blame] | 4727 | ceph_auth_destroy_authorizer(auth->authorizer); |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4728 | auth->authorizer = NULL; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4729 | } |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 4730 | if (!auth->authorizer) { |
| 4731 | int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS, |
| 4732 | auth); |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 4733 | if (ret) |
| 4734 | return ERR_PTR(ret); |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 4735 | } else { |
| 4736 | int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS, |
| 4737 | auth); |
Alex Elder | a255651 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4738 | if (ret) |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4739 | return ERR_PTR(ret); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4740 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4741 | *proto = ac->protocol; |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4742 | |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4743 | return auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4744 | } |
| 4745 | |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 4746 | static int add_authorizer_challenge(struct ceph_connection *con, |
| 4747 | void *challenge_buf, int challenge_buf_len) |
| 4748 | { |
| 4749 | struct ceph_mds_session *s = con->private; |
| 4750 | struct ceph_mds_client *mdsc = s->s_mdsc; |
| 4751 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
| 4752 | |
| 4753 | return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer, |
| 4754 | challenge_buf, challenge_buf_len); |
| 4755 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4756 | |
Ilya Dryomov | 0dde584 | 2016-12-02 16:35:09 +0100 | [diff] [blame] | 4757 | static int verify_authorizer_reply(struct ceph_connection *con) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4758 | { |
| 4759 | struct ceph_mds_session *s = con->private; |
| 4760 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4761 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4762 | |
Ilya Dryomov | 0dde584 | 2016-12-02 16:35:09 +0100 | [diff] [blame] | 4763 | return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4764 | } |
| 4765 | |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4766 | static int invalidate_authorizer(struct ceph_connection *con) |
| 4767 | { |
| 4768 | struct ceph_mds_session *s = con->private; |
| 4769 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4770 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4771 | |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 4772 | ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4773 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4774 | return ceph_monc_validate_auth(&mdsc->fsc->client->monc); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4775 | } |
| 4776 | |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 4777 | static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con, |
| 4778 | struct ceph_msg_header *hdr, int *skip) |
| 4779 | { |
| 4780 | struct ceph_msg *msg; |
| 4781 | int type = (int) le16_to_cpu(hdr->type); |
| 4782 | int front_len = (int) le32_to_cpu(hdr->front_len); |
| 4783 | |
| 4784 | if (con->in_msg) |
| 4785 | return con->in_msg; |
| 4786 | |
| 4787 | *skip = 0; |
| 4788 | msg = ceph_msg_new(type, front_len, GFP_NOFS, false); |
| 4789 | if (!msg) { |
| 4790 | pr_err("unable to allocate msg type %d len %d\n", |
| 4791 | type, front_len); |
| 4792 | return NULL; |
| 4793 | } |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 4794 | |
| 4795 | return msg; |
| 4796 | } |
| 4797 | |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4798 | static int mds_sign_message(struct ceph_msg *msg) |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4799 | { |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4800 | struct ceph_mds_session *s = msg->con->private; |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4801 | struct ceph_auth_handshake *auth = &s->s_auth; |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4802 | |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4803 | return ceph_auth_sign_message(auth, msg); |
| 4804 | } |
| 4805 | |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4806 | static int mds_check_message_signature(struct ceph_msg *msg) |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4807 | { |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4808 | struct ceph_mds_session *s = msg->con->private; |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4809 | struct ceph_auth_handshake *auth = &s->s_auth; |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4810 | |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4811 | return ceph_auth_check_message_signature(auth, msg); |
| 4812 | } |
| 4813 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 4814 | static const struct ceph_connection_operations mds_con_ops = { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4815 | .get = con_get, |
| 4816 | .put = con_put, |
| 4817 | .dispatch = dispatch, |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4818 | .get_authorizer = get_authorizer, |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 4819 | .add_authorizer_challenge = add_authorizer_challenge, |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4820 | .verify_authorizer_reply = verify_authorizer_reply, |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4821 | .invalidate_authorizer = invalidate_authorizer, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4822 | .peer_reset = peer_reset, |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 4823 | .alloc_msg = mds_alloc_msg, |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4824 | .sign_message = mds_sign_message, |
| 4825 | .check_message_signature = mds_check_message_signature, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4826 | }; |
| 4827 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4828 | /* eof */ |