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