Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2 | |
Sage Weil | 496e595 | 2010-09-22 19:57:10 -0700 | [diff] [blame] | 3 | #include <linux/fs.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4 | #include <linux/wait.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 6 | #include <linux/gfp.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 7 | #include <linux/sched.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/seq_file.h> |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 10 | #include <linux/utsname.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 11 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 12 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 13 | #include "mds_client.h" |
| 14 | |
Sage Weil | 1fe60e5 | 2012-07-30 16:23:22 -0700 | [diff] [blame] | 15 | #include <linux/ceph/ceph_features.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 16 | #include <linux/ceph/messenger.h> |
| 17 | #include <linux/ceph/decode.h> |
| 18 | #include <linux/ceph/pagelist.h> |
| 19 | #include <linux/ceph/auth.h> |
| 20 | #include <linux/ceph/debugfs.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * A cluster of MDS (metadata server) daemons is responsible for |
| 24 | * managing the file system namespace (the directory hierarchy and |
| 25 | * inodes) and for coordinating shared access to storage. Metadata is |
| 26 | * partitioning hierarchically across a number of servers, and that |
| 27 | * partition varies over time as the cluster adjusts the distribution |
| 28 | * in order to balance load. |
| 29 | * |
| 30 | * The MDS client is primarily responsible to managing synchronous |
| 31 | * metadata requests for operations like open, unlink, and so forth. |
| 32 | * If there is a MDS failure, we find out about it when we (possibly |
| 33 | * request and) receive a new MDS map, and can resubmit affected |
| 34 | * requests. |
| 35 | * |
| 36 | * For the most part, though, we take advantage of a lossless |
| 37 | * communications channel to the MDS, and do not need to worry about |
| 38 | * timing out or resubmitting requests. |
| 39 | * |
| 40 | * We maintain a stateful "session" with each MDS we interact with. |
| 41 | * Within each session, we sent periodic heartbeat messages to ensure |
| 42 | * any capabilities or leases we have been issues remain valid. If |
| 43 | * the session times out and goes stale, our leases and capabilities |
| 44 | * are no longer valid. |
| 45 | */ |
| 46 | |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 47 | struct ceph_reconnect_state { |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 48 | int nr_caps; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 49 | struct ceph_pagelist *pagelist; |
| 50 | bool flock; |
| 51 | }; |
| 52 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 53 | static void __wake_requests(struct ceph_mds_client *mdsc, |
| 54 | struct list_head *head); |
| 55 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 56 | static const struct ceph_connection_operations mds_con_ops; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | /* |
| 60 | * mds reply parsing |
| 61 | */ |
| 62 | |
| 63 | /* |
| 64 | * parse individual inode info |
| 65 | */ |
| 66 | static int parse_reply_info_in(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 67 | struct ceph_mds_reply_info_in *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 68 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 69 | { |
| 70 | int err = -EIO; |
| 71 | |
| 72 | info->in = *p; |
| 73 | *p += sizeof(struct ceph_mds_reply_inode) + |
| 74 | sizeof(*info->in->fragtree.splits) * |
| 75 | le32_to_cpu(info->in->fragtree.nsplits); |
| 76 | |
| 77 | ceph_decode_32_safe(p, end, info->symlink_len, bad); |
| 78 | ceph_decode_need(p, end, info->symlink_len, bad); |
| 79 | info->symlink = *p; |
| 80 | *p += info->symlink_len; |
| 81 | |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 82 | if (features & CEPH_FEATURE_DIRLAYOUTHASH) |
| 83 | ceph_decode_copy_safe(p, end, &info->dir_layout, |
| 84 | sizeof(info->dir_layout), bad); |
| 85 | else |
| 86 | memset(&info->dir_layout, 0, sizeof(info->dir_layout)); |
| 87 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 88 | ceph_decode_32_safe(p, end, info->xattr_len, bad); |
| 89 | ceph_decode_need(p, end, info->xattr_len, bad); |
| 90 | info->xattr_data = *p; |
| 91 | *p += info->xattr_len; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 92 | |
| 93 | if (features & CEPH_FEATURE_MDS_INLINE_DATA) { |
| 94 | ceph_decode_64_safe(p, end, info->inline_version, bad); |
| 95 | ceph_decode_32_safe(p, end, info->inline_len, bad); |
| 96 | ceph_decode_need(p, end, info->inline_len, bad); |
| 97 | info->inline_data = *p; |
| 98 | *p += info->inline_len; |
| 99 | } else |
| 100 | info->inline_version = CEPH_INLINE_NONE; |
| 101 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | bad: |
| 104 | return err; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * parse a normal reply, which may contain a (dir+)dentry and/or a |
| 109 | * target inode. |
| 110 | */ |
| 111 | static int parse_reply_info_trace(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 112 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 113 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 114 | { |
| 115 | int err; |
| 116 | |
| 117 | if (info->head->is_dentry) { |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 118 | err = parse_reply_info_in(p, end, &info->diri, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 119 | if (err < 0) |
| 120 | goto out_bad; |
| 121 | |
| 122 | if (unlikely(*p + sizeof(*info->dirfrag) > end)) |
| 123 | goto bad; |
| 124 | info->dirfrag = *p; |
| 125 | *p += sizeof(*info->dirfrag) + |
| 126 | sizeof(u32)*le32_to_cpu(info->dirfrag->ndist); |
| 127 | if (unlikely(*p > end)) |
| 128 | goto bad; |
| 129 | |
| 130 | ceph_decode_32_safe(p, end, info->dname_len, bad); |
| 131 | ceph_decode_need(p, end, info->dname_len, bad); |
| 132 | info->dname = *p; |
| 133 | *p += info->dname_len; |
| 134 | info->dlease = *p; |
| 135 | *p += sizeof(*info->dlease); |
| 136 | } |
| 137 | |
| 138 | if (info->head->is_target) { |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 139 | err = parse_reply_info_in(p, end, &info->targeti, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 140 | if (err < 0) |
| 141 | goto out_bad; |
| 142 | } |
| 143 | |
| 144 | if (unlikely(*p != end)) |
| 145 | goto bad; |
| 146 | return 0; |
| 147 | |
| 148 | bad: |
| 149 | err = -EIO; |
| 150 | out_bad: |
| 151 | pr_err("problem parsing mds trace %d\n", err); |
| 152 | return err; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * parse readdir results |
| 157 | */ |
| 158 | static int parse_reply_info_dir(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 159 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 160 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 161 | { |
| 162 | u32 num, i = 0; |
| 163 | int err; |
| 164 | |
| 165 | info->dir_dir = *p; |
| 166 | if (*p + sizeof(*info->dir_dir) > end) |
| 167 | goto bad; |
| 168 | *p += sizeof(*info->dir_dir) + |
| 169 | sizeof(u32)*le32_to_cpu(info->dir_dir->ndist); |
| 170 | if (*p > end) |
| 171 | goto bad; |
| 172 | |
| 173 | ceph_decode_need(p, end, sizeof(num) + 2, bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 174 | num = ceph_decode_32(p); |
| 175 | info->dir_end = ceph_decode_8(p); |
| 176 | info->dir_complete = ceph_decode_8(p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 177 | if (num == 0) |
| 178 | goto done; |
| 179 | |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 180 | BUG_ON(!info->dir_in); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 181 | info->dir_dname = (void *)(info->dir_in + num); |
| 182 | info->dir_dname_len = (void *)(info->dir_dname + num); |
| 183 | info->dir_dlease = (void *)(info->dir_dname_len + num); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 184 | if ((unsigned long)(info->dir_dlease + num) > |
| 185 | (unsigned long)info->dir_in + info->dir_buf_size) { |
| 186 | pr_err("dir contents are larger than expected\n"); |
| 187 | WARN_ON(1); |
| 188 | goto bad; |
| 189 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 190 | |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 191 | info->dir_nr = num; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 192 | while (num) { |
| 193 | /* dentry */ |
| 194 | ceph_decode_need(p, end, sizeof(u32)*2, bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 195 | info->dir_dname_len[i] = ceph_decode_32(p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 196 | ceph_decode_need(p, end, info->dir_dname_len[i], bad); |
| 197 | info->dir_dname[i] = *p; |
| 198 | *p += info->dir_dname_len[i]; |
| 199 | dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i], |
| 200 | info->dir_dname[i]); |
| 201 | info->dir_dlease[i] = *p; |
| 202 | *p += sizeof(struct ceph_mds_reply_lease); |
| 203 | |
| 204 | /* inode */ |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 205 | err = parse_reply_info_in(p, end, &info->dir_in[i], features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 206 | if (err < 0) |
| 207 | goto out_bad; |
| 208 | i++; |
| 209 | num--; |
| 210 | } |
| 211 | |
| 212 | done: |
| 213 | if (*p != end) |
| 214 | goto bad; |
| 215 | return 0; |
| 216 | |
| 217 | bad: |
| 218 | err = -EIO; |
| 219 | out_bad: |
| 220 | pr_err("problem parsing dir contents %d\n", err); |
| 221 | return err; |
| 222 | } |
| 223 | |
| 224 | /* |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 225 | * parse fcntl F_GETLK results |
| 226 | */ |
| 227 | static int parse_reply_info_filelock(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 228 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 229 | u64 features) |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 230 | { |
| 231 | if (*p + sizeof(*info->filelock_reply) > end) |
| 232 | goto bad; |
| 233 | |
| 234 | info->filelock_reply = *p; |
| 235 | *p += sizeof(*info->filelock_reply); |
| 236 | |
| 237 | if (unlikely(*p != end)) |
| 238 | goto bad; |
| 239 | return 0; |
| 240 | |
| 241 | bad: |
| 242 | return -EIO; |
| 243 | } |
| 244 | |
| 245 | /* |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 246 | * parse create results |
| 247 | */ |
| 248 | static int parse_reply_info_create(void **p, void *end, |
| 249 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 250 | u64 features) |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 251 | { |
| 252 | if (features & CEPH_FEATURE_REPLY_CREATE_INODE) { |
| 253 | if (*p == end) { |
| 254 | info->has_create_ino = false; |
| 255 | } else { |
| 256 | info->has_create_ino = true; |
| 257 | info->ino = ceph_decode_64(p); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (unlikely(*p != end)) |
| 262 | goto bad; |
| 263 | return 0; |
| 264 | |
| 265 | bad: |
| 266 | return -EIO; |
| 267 | } |
| 268 | |
| 269 | /* |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 270 | * parse extra results |
| 271 | */ |
| 272 | static int parse_reply_info_extra(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 273 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 274 | u64 features) |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 275 | { |
| 276 | if (info->head->op == CEPH_MDS_OP_GETFILELOCK) |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 277 | return parse_reply_info_filelock(p, end, info, features); |
Yan, Zheng | 8a03449 | 2013-02-21 13:43:55 +0800 | [diff] [blame] | 278 | else if (info->head->op == CEPH_MDS_OP_READDIR || |
| 279 | info->head->op == CEPH_MDS_OP_LSSNAP) |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 280 | return parse_reply_info_dir(p, end, info, features); |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 281 | else if (info->head->op == CEPH_MDS_OP_CREATE) |
| 282 | return parse_reply_info_create(p, end, info, features); |
| 283 | else |
| 284 | return -EIO; |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 288 | * parse entire mds reply |
| 289 | */ |
| 290 | static int parse_reply_info(struct ceph_msg *msg, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 291 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 292 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 293 | { |
| 294 | void *p, *end; |
| 295 | u32 len; |
| 296 | int err; |
| 297 | |
| 298 | info->head = msg->front.iov_base; |
| 299 | p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head); |
| 300 | end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head); |
| 301 | |
| 302 | /* trace */ |
| 303 | ceph_decode_32_safe(&p, end, len, bad); |
| 304 | if (len > 0) { |
Xi Wang | 32852a8 | 2012-01-14 22:20:59 -0500 | [diff] [blame] | 305 | ceph_decode_need(&p, end, len, bad); |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 306 | err = parse_reply_info_trace(&p, p+len, info, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 307 | if (err < 0) |
| 308 | goto out_bad; |
| 309 | } |
| 310 | |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 311 | /* extra */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 312 | ceph_decode_32_safe(&p, end, len, bad); |
| 313 | if (len > 0) { |
Xi Wang | 32852a8 | 2012-01-14 22:20:59 -0500 | [diff] [blame] | 314 | ceph_decode_need(&p, end, len, bad); |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 315 | err = parse_reply_info_extra(&p, p+len, info, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 316 | if (err < 0) |
| 317 | goto out_bad; |
| 318 | } |
| 319 | |
| 320 | /* snap blob */ |
| 321 | ceph_decode_32_safe(&p, end, len, bad); |
| 322 | info->snapblob_len = len; |
| 323 | info->snapblob = p; |
| 324 | p += len; |
| 325 | |
| 326 | if (p != end) |
| 327 | goto bad; |
| 328 | return 0; |
| 329 | |
| 330 | bad: |
| 331 | err = -EIO; |
| 332 | out_bad: |
| 333 | pr_err("mds parse_reply err %d\n", err); |
| 334 | return err; |
| 335 | } |
| 336 | |
| 337 | static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info) |
| 338 | { |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 339 | if (!info->dir_in) |
| 340 | return; |
| 341 | free_pages((unsigned long)info->dir_in, get_order(info->dir_buf_size)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
| 345 | /* |
| 346 | * sessions |
| 347 | */ |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 348 | const char *ceph_session_state_name(int s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 349 | { |
| 350 | switch (s) { |
| 351 | case CEPH_MDS_SESSION_NEW: return "new"; |
| 352 | case CEPH_MDS_SESSION_OPENING: return "opening"; |
| 353 | case CEPH_MDS_SESSION_OPEN: return "open"; |
| 354 | case CEPH_MDS_SESSION_HUNG: return "hung"; |
| 355 | case CEPH_MDS_SESSION_CLOSING: return "closing"; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 356 | case CEPH_MDS_SESSION_RESTARTING: return "restarting"; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 357 | case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting"; |
| 358 | default: return "???"; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | static struct ceph_mds_session *get_session(struct ceph_mds_session *s) |
| 363 | { |
| 364 | if (atomic_inc_not_zero(&s->s_ref)) { |
| 365 | dout("mdsc get_session %p %d -> %d\n", s, |
| 366 | atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref)); |
| 367 | return s; |
| 368 | } else { |
| 369 | dout("mdsc get_session %p 0 -- FAIL", s); |
| 370 | return NULL; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void ceph_put_mds_session(struct ceph_mds_session *s) |
| 375 | { |
| 376 | dout("mdsc put_session %p %d -> %d\n", s, |
| 377 | atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 378 | if (atomic_dec_and_test(&s->s_ref)) { |
Alex Elder | 6c4a191 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 379 | if (s->s_auth.authorizer) |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 380 | ceph_auth_destroy_authorizer( |
| 381 | s->s_mdsc->fsc->client->monc.auth, |
| 382 | s->s_auth.authorizer); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 383 | kfree(s); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 384 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
| 388 | * called under mdsc->mutex |
| 389 | */ |
| 390 | struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc, |
| 391 | int mds) |
| 392 | { |
| 393 | struct ceph_mds_session *session; |
| 394 | |
| 395 | if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL) |
| 396 | return NULL; |
| 397 | session = mdsc->sessions[mds]; |
| 398 | dout("lookup_mds_session %p %d\n", session, |
| 399 | atomic_read(&session->s_ref)); |
| 400 | get_session(session); |
| 401 | return session; |
| 402 | } |
| 403 | |
| 404 | static bool __have_session(struct ceph_mds_client *mdsc, int mds) |
| 405 | { |
| 406 | if (mds >= mdsc->max_sessions) |
| 407 | return false; |
| 408 | return mdsc->sessions[mds]; |
| 409 | } |
| 410 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 411 | static int __verify_registered_session(struct ceph_mds_client *mdsc, |
| 412 | struct ceph_mds_session *s) |
| 413 | { |
| 414 | if (s->s_mds >= mdsc->max_sessions || |
| 415 | mdsc->sessions[s->s_mds] != s) |
| 416 | return -ENOENT; |
| 417 | return 0; |
| 418 | } |
| 419 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 420 | /* |
| 421 | * create+register a new session for given mds. |
| 422 | * called under mdsc->mutex. |
| 423 | */ |
| 424 | static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc, |
| 425 | int mds) |
| 426 | { |
| 427 | struct ceph_mds_session *s; |
| 428 | |
Nathaniel Yazdani | c338c07 | 2013-08-04 21:04:30 -0700 | [diff] [blame] | 429 | if (mds >= mdsc->mdsmap->m_max_mds) |
| 430 | return ERR_PTR(-EINVAL); |
| 431 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 432 | s = kzalloc(sizeof(*s), GFP_NOFS); |
Dan Carpenter | 4736b00 | 2010-03-20 15:30:16 +0300 | [diff] [blame] | 433 | if (!s) |
| 434 | return ERR_PTR(-ENOMEM); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 435 | s->s_mdsc = mdsc; |
| 436 | s->s_mds = mds; |
| 437 | s->s_state = CEPH_MDS_SESSION_NEW; |
| 438 | s->s_ttl = 0; |
| 439 | s->s_seq = 0; |
| 440 | mutex_init(&s->s_mutex); |
| 441 | |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 442 | 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] | 443 | |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 444 | spin_lock_init(&s->s_gen_ttl_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 445 | s->s_cap_gen = 0; |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 446 | s->s_cap_ttl = jiffies - 1; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 447 | |
| 448 | spin_lock_init(&s->s_cap_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 449 | s->s_renew_requested = 0; |
| 450 | s->s_renew_seq = 0; |
| 451 | INIT_LIST_HEAD(&s->s_caps); |
| 452 | s->s_nr_caps = 0; |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 453 | s->s_trim_caps = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 454 | atomic_set(&s->s_ref, 1); |
| 455 | INIT_LIST_HEAD(&s->s_waiting); |
| 456 | INIT_LIST_HEAD(&s->s_unsafe); |
| 457 | s->s_num_cap_releases = 0; |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 458 | s->s_cap_reconnect = 0; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 459 | s->s_cap_iterator = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 460 | INIT_LIST_HEAD(&s->s_cap_releases); |
| 461 | INIT_LIST_HEAD(&s->s_cap_releases_done); |
| 462 | INIT_LIST_HEAD(&s->s_cap_flushing); |
| 463 | INIT_LIST_HEAD(&s->s_cap_snaps_flushing); |
| 464 | |
| 465 | dout("register_session mds%d\n", mds); |
| 466 | if (mds >= mdsc->max_sessions) { |
| 467 | int newmax = 1 << get_count_order(mds+1); |
| 468 | struct ceph_mds_session **sa; |
| 469 | |
| 470 | dout("register_session realloc to %d\n", newmax); |
| 471 | sa = kcalloc(newmax, sizeof(void *), GFP_NOFS); |
| 472 | if (sa == NULL) |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 473 | goto fail_realloc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 474 | if (mdsc->sessions) { |
| 475 | memcpy(sa, mdsc->sessions, |
| 476 | mdsc->max_sessions * sizeof(void *)); |
| 477 | kfree(mdsc->sessions); |
| 478 | } |
| 479 | mdsc->sessions = sa; |
| 480 | mdsc->max_sessions = newmax; |
| 481 | } |
| 482 | mdsc->sessions[mds] = s; |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 483 | atomic_inc(&mdsc->num_sessions); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 484 | atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */ |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 485 | |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 486 | ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds, |
| 487 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 488 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 489 | return s; |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 490 | |
| 491 | fail_realloc: |
| 492 | kfree(s); |
| 493 | return ERR_PTR(-ENOMEM); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* |
| 497 | * called under mdsc->mutex |
| 498 | */ |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 499 | static void __unregister_session(struct ceph_mds_client *mdsc, |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 500 | struct ceph_mds_session *s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 501 | { |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 502 | dout("__unregister_session mds%d %p\n", s->s_mds, s); |
| 503 | BUG_ON(mdsc->sessions[s->s_mds] != s); |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 504 | mdsc->sessions[s->s_mds] = NULL; |
| 505 | ceph_con_close(&s->s_con); |
| 506 | ceph_put_mds_session(s); |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 507 | atomic_dec(&mdsc->num_sessions); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* |
| 511 | * drop session refs in request. |
| 512 | * |
| 513 | * should be last request ref, or hold mdsc->mutex |
| 514 | */ |
| 515 | static void put_request_session(struct ceph_mds_request *req) |
| 516 | { |
| 517 | if (req->r_session) { |
| 518 | ceph_put_mds_session(req->r_session); |
| 519 | req->r_session = NULL; |
| 520 | } |
| 521 | } |
| 522 | |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 523 | void ceph_mdsc_release_request(struct kref *kref) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 524 | { |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 525 | struct ceph_mds_request *req = container_of(kref, |
| 526 | struct ceph_mds_request, |
| 527 | r_kref); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 528 | destroy_reply_info(&req->r_reply_info); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 529 | if (req->r_request) |
| 530 | ceph_msg_put(req->r_request); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 531 | if (req->r_reply) |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 532 | ceph_msg_put(req->r_reply); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 533 | if (req->r_inode) { |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 534 | ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 535 | iput(req->r_inode); |
| 536 | } |
| 537 | if (req->r_locked_dir) |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 538 | ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 539 | iput(req->r_target_inode); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 540 | if (req->r_dentry) |
| 541 | dput(req->r_dentry); |
Sage Weil | 844d87c | 2013-02-05 13:40:09 -0800 | [diff] [blame] | 542 | if (req->r_old_dentry) |
| 543 | dput(req->r_old_dentry); |
| 544 | if (req->r_old_dentry_dir) { |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 545 | /* |
| 546 | * track (and drop pins for) r_old_dentry_dir |
| 547 | * separately, since r_old_dentry's d_parent may have |
| 548 | * changed between the dir mutex being dropped and |
| 549 | * this request being freed. |
| 550 | */ |
| 551 | ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir), |
| 552 | CEPH_CAP_PIN); |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 553 | iput(req->r_old_dentry_dir); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 554 | } |
| 555 | kfree(req->r_path1); |
| 556 | kfree(req->r_path2); |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 557 | if (req->r_pagelist) |
| 558 | ceph_pagelist_release(req->r_pagelist); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 559 | put_request_session(req); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 560 | ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 561 | kfree(req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
| 565 | * lookup session, bump ref if found. |
| 566 | * |
| 567 | * called under mdsc->mutex. |
| 568 | */ |
| 569 | static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc, |
| 570 | u64 tid) |
| 571 | { |
| 572 | struct ceph_mds_request *req; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 573 | struct rb_node *n = mdsc->request_tree.rb_node; |
| 574 | |
| 575 | while (n) { |
| 576 | req = rb_entry(n, struct ceph_mds_request, r_node); |
| 577 | if (tid < req->r_tid) |
| 578 | n = n->rb_left; |
| 579 | else if (tid > req->r_tid) |
| 580 | n = n->rb_right; |
| 581 | else { |
| 582 | ceph_mdsc_get_request(req); |
| 583 | return req; |
| 584 | } |
| 585 | } |
| 586 | return NULL; |
| 587 | } |
| 588 | |
| 589 | static void __insert_request(struct ceph_mds_client *mdsc, |
| 590 | struct ceph_mds_request *new) |
| 591 | { |
| 592 | struct rb_node **p = &mdsc->request_tree.rb_node; |
| 593 | struct rb_node *parent = NULL; |
| 594 | struct ceph_mds_request *req = NULL; |
| 595 | |
| 596 | while (*p) { |
| 597 | parent = *p; |
| 598 | req = rb_entry(parent, struct ceph_mds_request, r_node); |
| 599 | if (new->r_tid < req->r_tid) |
| 600 | p = &(*p)->rb_left; |
| 601 | else if (new->r_tid > req->r_tid) |
| 602 | p = &(*p)->rb_right; |
| 603 | else |
| 604 | BUG(); |
| 605 | } |
| 606 | |
| 607 | rb_link_node(&new->r_node, parent, p); |
| 608 | rb_insert_color(&new->r_node, &mdsc->request_tree); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Register an in-flight request, and assign a tid. Link to directory |
| 613 | * are modifying (if any). |
| 614 | * |
| 615 | * Called under mdsc->mutex. |
| 616 | */ |
| 617 | static void __register_request(struct ceph_mds_client *mdsc, |
| 618 | struct ceph_mds_request *req, |
| 619 | struct inode *dir) |
| 620 | { |
| 621 | req->r_tid = ++mdsc->last_tid; |
| 622 | if (req->r_num_caps) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 623 | ceph_reserve_caps(mdsc, &req->r_caps_reservation, |
| 624 | req->r_num_caps); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 625 | dout("__register_request %p tid %lld\n", req, req->r_tid); |
| 626 | ceph_mdsc_get_request(req); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 627 | __insert_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 628 | |
Sage Weil | cb4276c | 2010-11-08 07:28:52 -0800 | [diff] [blame] | 629 | req->r_uid = current_fsuid(); |
| 630 | req->r_gid = current_fsgid(); |
| 631 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 632 | if (dir) { |
| 633 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 634 | |
Sage Weil | 3b66378 | 2011-05-18 16:12:12 -0700 | [diff] [blame] | 635 | ihold(dir); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 636 | spin_lock(&ci->i_unsafe_lock); |
| 637 | req->r_unsafe_dir = dir; |
| 638 | list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops); |
| 639 | spin_unlock(&ci->i_unsafe_lock); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | static void __unregister_request(struct ceph_mds_client *mdsc, |
| 644 | struct ceph_mds_request *req) |
| 645 | { |
| 646 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 647 | rb_erase(&req->r_node, &mdsc->request_tree); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 648 | RB_CLEAR_NODE(&req->r_node); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 649 | |
| 650 | if (req->r_unsafe_dir) { |
| 651 | struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir); |
| 652 | |
| 653 | spin_lock(&ci->i_unsafe_lock); |
| 654 | list_del_init(&req->r_unsafe_dir_item); |
| 655 | spin_unlock(&ci->i_unsafe_lock); |
Sage Weil | 3b66378 | 2011-05-18 16:12:12 -0700 | [diff] [blame] | 656 | |
| 657 | iput(req->r_unsafe_dir); |
| 658 | req->r_unsafe_dir = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 659 | } |
Sage Weil | 94aa8ae | 2010-03-28 21:22:50 -0700 | [diff] [blame] | 660 | |
Yan, Zheng | fc55d2c | 2013-10-31 09:10:47 +0800 | [diff] [blame] | 661 | complete_all(&req->r_safe_completion); |
| 662 | |
Sage Weil | 94aa8ae | 2010-03-28 21:22:50 -0700 | [diff] [blame] | 663 | ceph_mdsc_put_request(req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Choose mds to send request to next. If there is a hint set in the |
| 668 | * request (e.g., due to a prior forward hint from the mds), use that. |
| 669 | * Otherwise, consult frag tree and/or caps to identify the |
| 670 | * appropriate mds. If all else fails, choose randomly. |
| 671 | * |
| 672 | * Called under mdsc->mutex. |
| 673 | */ |
H Hartley Sweeten | 7fd7d10 | 2011-09-23 13:22:11 -0700 | [diff] [blame] | 674 | static struct dentry *get_nonsnap_parent(struct dentry *dentry) |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 675 | { |
Sage Weil | d79698d | 2011-07-26 11:31:26 -0700 | [diff] [blame] | 676 | /* |
| 677 | * we don't need to worry about protecting the d_parent access |
| 678 | * here because we never renaming inside the snapped namespace |
| 679 | * except to resplice to another snapdir, and either the old or new |
| 680 | * result is a valid result. |
| 681 | */ |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 682 | while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP) |
| 683 | dentry = dentry->d_parent; |
| 684 | return dentry; |
| 685 | } |
| 686 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 687 | static int __choose_mds(struct ceph_mds_client *mdsc, |
| 688 | struct ceph_mds_request *req) |
| 689 | { |
| 690 | struct inode *inode; |
| 691 | struct ceph_inode_info *ci; |
| 692 | struct ceph_cap *cap; |
| 693 | int mode = req->r_direct_mode; |
| 694 | int mds = -1; |
| 695 | u32 hash = req->r_direct_hash; |
| 696 | bool is_hash = req->r_direct_is_hash; |
| 697 | |
| 698 | /* |
| 699 | * is there a specific mds we should try? ignore hint if we have |
| 700 | * no session and the mds is not up (active or recovering). |
| 701 | */ |
| 702 | if (req->r_resend_mds >= 0 && |
| 703 | (__have_session(mdsc, req->r_resend_mds) || |
| 704 | ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) { |
| 705 | dout("choose_mds using resend_mds mds%d\n", |
| 706 | req->r_resend_mds); |
| 707 | return req->r_resend_mds; |
| 708 | } |
| 709 | |
| 710 | if (mode == USE_RANDOM_MDS) |
| 711 | goto random; |
| 712 | |
| 713 | inode = NULL; |
| 714 | if (req->r_inode) { |
| 715 | inode = req->r_inode; |
| 716 | } else if (req->r_dentry) { |
Sage Weil | d79698d | 2011-07-26 11:31:26 -0700 | [diff] [blame] | 717 | /* ignore race with rename; old or new d_parent is okay */ |
| 718 | struct dentry *parent = req->r_dentry->d_parent; |
| 719 | struct inode *dir = parent->d_inode; |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 720 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 721 | if (dir->i_sb != mdsc->fsc->sb) { |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 722 | /* not this fs! */ |
| 723 | inode = req->r_dentry->d_inode; |
| 724 | } else if (ceph_snap(dir) != CEPH_NOSNAP) { |
| 725 | /* direct snapped/virtual snapdir requests |
| 726 | * based on parent dir inode */ |
Sage Weil | d79698d | 2011-07-26 11:31:26 -0700 | [diff] [blame] | 727 | struct dentry *dn = get_nonsnap_parent(parent); |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 728 | inode = dn->d_inode; |
| 729 | dout("__choose_mds using nonsnap parent %p\n", inode); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 730 | } else { |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 731 | /* dentry target */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 732 | inode = req->r_dentry->d_inode; |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 733 | if (!inode || mode == USE_AUTH_MDS) { |
| 734 | /* dir + name */ |
| 735 | inode = dir; |
| 736 | hash = ceph_dentry_hash(dir, req->r_dentry); |
| 737 | is_hash = true; |
| 738 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 741 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 742 | dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash, |
| 743 | (int)hash, mode); |
| 744 | if (!inode) |
| 745 | goto random; |
| 746 | ci = ceph_inode(inode); |
| 747 | |
| 748 | if (is_hash && S_ISDIR(inode->i_mode)) { |
| 749 | struct ceph_inode_frag frag; |
| 750 | int found; |
| 751 | |
| 752 | ceph_choose_frag(ci, hash, &frag, &found); |
| 753 | if (found) { |
| 754 | if (mode == USE_ANY_MDS && frag.ndist > 0) { |
| 755 | u8 r; |
| 756 | |
| 757 | /* choose a random replica */ |
| 758 | get_random_bytes(&r, 1); |
| 759 | r %= frag.ndist; |
| 760 | mds = frag.dist[r]; |
| 761 | dout("choose_mds %p %llx.%llx " |
| 762 | "frag %u mds%d (%d/%d)\n", |
| 763 | inode, ceph_vinop(inode), |
Sage Weil | d66bbd4 | 2011-01-21 21:16:46 -0800 | [diff] [blame] | 764 | frag.frag, mds, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 765 | (int)r, frag.ndist); |
Sage Weil | d66bbd4 | 2011-01-21 21:16:46 -0800 | [diff] [blame] | 766 | if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >= |
| 767 | CEPH_MDS_STATE_ACTIVE) |
| 768 | return mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* since this file/dir wasn't known to be |
| 772 | * replicated, then we want to look for the |
| 773 | * authoritative mds. */ |
| 774 | mode = USE_AUTH_MDS; |
| 775 | if (frag.mds >= 0) { |
| 776 | /* choose auth mds */ |
| 777 | mds = frag.mds; |
| 778 | dout("choose_mds %p %llx.%llx " |
| 779 | "frag %u mds%d (auth)\n", |
| 780 | inode, ceph_vinop(inode), frag.frag, mds); |
Sage Weil | d66bbd4 | 2011-01-21 21:16:46 -0800 | [diff] [blame] | 781 | if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >= |
| 782 | CEPH_MDS_STATE_ACTIVE) |
| 783 | return mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 788 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 789 | cap = NULL; |
| 790 | if (mode == USE_AUTH_MDS) |
| 791 | cap = ci->i_auth_cap; |
| 792 | if (!cap && !RB_EMPTY_ROOT(&ci->i_caps)) |
| 793 | cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node); |
| 794 | if (!cap) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 795 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 796 | goto random; |
| 797 | } |
| 798 | mds = cap->session->s_mds; |
| 799 | dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n", |
| 800 | inode, ceph_vinop(inode), mds, |
| 801 | cap == ci->i_auth_cap ? "auth " : "", cap); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 802 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 803 | return mds; |
| 804 | |
| 805 | random: |
| 806 | mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap); |
| 807 | dout("choose_mds chose random mds%d\n", mds); |
| 808 | return mds; |
| 809 | } |
| 810 | |
| 811 | |
| 812 | /* |
| 813 | * session messages |
| 814 | */ |
| 815 | static struct ceph_msg *create_session_msg(u32 op, u64 seq) |
| 816 | { |
| 817 | struct ceph_msg *msg; |
| 818 | struct ceph_mds_session_head *h; |
| 819 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 820 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS, |
| 821 | false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 822 | if (!msg) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 823 | pr_err("create_session_msg ENOMEM creating msg\n"); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 824 | return NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 825 | } |
| 826 | h = msg->front.iov_base; |
| 827 | h->op = cpu_to_le32(op); |
| 828 | h->seq = cpu_to_le64(seq); |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 829 | |
| 830 | return msg; |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * session message, specialization for CEPH_SESSION_REQUEST_OPEN |
| 835 | * to include additional client metadata fields. |
| 836 | */ |
| 837 | static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq) |
| 838 | { |
| 839 | struct ceph_msg *msg; |
| 840 | struct ceph_mds_session_head *h; |
| 841 | int i = -1; |
| 842 | int metadata_bytes = 0; |
| 843 | int metadata_key_count = 0; |
| 844 | struct ceph_options *opt = mdsc->fsc->client->options; |
| 845 | void *p; |
| 846 | |
Yan, Zheng | a6a5ce4 | 2015-01-16 10:54:43 +0800 | [diff] [blame] | 847 | const char* metadata[][2] = { |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 848 | {"hostname", utsname()->nodename}, |
Yan, Zheng | a6a5ce4 | 2015-01-16 10:54:43 +0800 | [diff] [blame] | 849 | {"kernel_version", utsname()->release}, |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 850 | {"entity_id", opt->name ? opt->name : ""}, |
| 851 | {NULL, NULL} |
| 852 | }; |
| 853 | |
| 854 | /* Calculate serialized length of metadata */ |
| 855 | metadata_bytes = 4; /* map length */ |
| 856 | for (i = 0; metadata[i][0] != NULL; ++i) { |
| 857 | metadata_bytes += 8 + strlen(metadata[i][0]) + |
| 858 | strlen(metadata[i][1]); |
| 859 | metadata_key_count++; |
| 860 | } |
| 861 | |
| 862 | /* Allocate the message */ |
| 863 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes, |
| 864 | GFP_NOFS, false); |
| 865 | if (!msg) { |
| 866 | pr_err("create_session_msg ENOMEM creating msg\n"); |
| 867 | return NULL; |
| 868 | } |
| 869 | h = msg->front.iov_base; |
| 870 | h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN); |
| 871 | h->seq = cpu_to_le64(seq); |
| 872 | |
| 873 | /* |
| 874 | * Serialize client metadata into waiting buffer space, using |
| 875 | * the format that userspace expects for map<string, string> |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 876 | * |
| 877 | * ClientSession messages with metadata are v2 |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 878 | */ |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 879 | msg->hdr.version = cpu_to_le16(2); |
| 880 | msg->hdr.compat_version = cpu_to_le16(1); |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 881 | |
| 882 | /* The write pointer, following the session_head structure */ |
| 883 | p = msg->front.iov_base + sizeof(*h); |
| 884 | |
| 885 | /* Number of entries in the map */ |
| 886 | ceph_encode_32(&p, metadata_key_count); |
| 887 | |
| 888 | /* Two length-prefixed strings for each entry in the map */ |
| 889 | for (i = 0; metadata[i][0] != NULL; ++i) { |
| 890 | size_t const key_len = strlen(metadata[i][0]); |
| 891 | size_t const val_len = strlen(metadata[i][1]); |
| 892 | |
| 893 | ceph_encode_32(&p, key_len); |
| 894 | memcpy(p, metadata[i][0], key_len); |
| 895 | p += key_len; |
| 896 | ceph_encode_32(&p, val_len); |
| 897 | memcpy(p, metadata[i][1], val_len); |
| 898 | p += val_len; |
| 899 | } |
| 900 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 901 | return msg; |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | * send session open request. |
| 906 | * |
| 907 | * called under mdsc->mutex |
| 908 | */ |
| 909 | static int __open_session(struct ceph_mds_client *mdsc, |
| 910 | struct ceph_mds_session *session) |
| 911 | { |
| 912 | struct ceph_msg *msg; |
| 913 | int mstate; |
| 914 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 915 | |
| 916 | /* wait for mds to go active? */ |
| 917 | mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds); |
| 918 | dout("open_session to mds%d (%s)\n", mds, |
| 919 | ceph_mds_state_name(mstate)); |
| 920 | session->s_state = CEPH_MDS_SESSION_OPENING; |
| 921 | session->s_renew_requested = jiffies; |
| 922 | |
| 923 | /* send connect message */ |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 924 | msg = create_session_open_msg(mdsc, session->s_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 925 | if (!msg) |
| 926 | return -ENOMEM; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 927 | ceph_con_send(&session->s_con, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | /* |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 932 | * open sessions for any export targets for the given mds |
| 933 | * |
| 934 | * called under mdsc->mutex |
| 935 | */ |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 936 | static struct ceph_mds_session * |
| 937 | __open_export_target_session(struct ceph_mds_client *mdsc, int target) |
| 938 | { |
| 939 | struct ceph_mds_session *session; |
| 940 | |
| 941 | session = __ceph_lookup_mds_session(mdsc, target); |
| 942 | if (!session) { |
| 943 | session = register_session(mdsc, target); |
| 944 | if (IS_ERR(session)) |
| 945 | return session; |
| 946 | } |
| 947 | if (session->s_state == CEPH_MDS_SESSION_NEW || |
| 948 | session->s_state == CEPH_MDS_SESSION_CLOSING) |
| 949 | __open_session(mdsc, session); |
| 950 | |
| 951 | return session; |
| 952 | } |
| 953 | |
| 954 | struct ceph_mds_session * |
| 955 | ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target) |
| 956 | { |
| 957 | struct ceph_mds_session *session; |
| 958 | |
| 959 | dout("open_export_target_session to mds%d\n", target); |
| 960 | |
| 961 | mutex_lock(&mdsc->mutex); |
| 962 | session = __open_export_target_session(mdsc, target); |
| 963 | mutex_unlock(&mdsc->mutex); |
| 964 | |
| 965 | return session; |
| 966 | } |
| 967 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 968 | static void __open_export_target_sessions(struct ceph_mds_client *mdsc, |
| 969 | struct ceph_mds_session *session) |
| 970 | { |
| 971 | struct ceph_mds_info *mi; |
| 972 | struct ceph_mds_session *ts; |
| 973 | int i, mds = session->s_mds; |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 974 | |
| 975 | if (mds >= mdsc->mdsmap->m_max_mds) |
| 976 | return; |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 977 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 978 | mi = &mdsc->mdsmap->m_info[mds]; |
| 979 | dout("open_export_target_sessions for mds%d (%d targets)\n", |
| 980 | session->s_mds, mi->num_export_targets); |
| 981 | |
| 982 | for (i = 0; i < mi->num_export_targets; i++) { |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 983 | ts = __open_export_target_session(mdsc, mi->export_targets[i]); |
| 984 | if (!IS_ERR(ts)) |
| 985 | ceph_put_mds_session(ts); |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 989 | void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc, |
| 990 | struct ceph_mds_session *session) |
| 991 | { |
| 992 | mutex_lock(&mdsc->mutex); |
| 993 | __open_export_target_sessions(mdsc, session); |
| 994 | mutex_unlock(&mdsc->mutex); |
| 995 | } |
| 996 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 997 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 998 | * session caps |
| 999 | */ |
| 1000 | |
| 1001 | /* |
| 1002 | * Free preallocated cap messages assigned to this session |
| 1003 | */ |
| 1004 | static void cleanup_cap_releases(struct ceph_mds_session *session) |
| 1005 | { |
| 1006 | struct ceph_msg *msg; |
| 1007 | |
| 1008 | spin_lock(&session->s_cap_lock); |
| 1009 | while (!list_empty(&session->s_cap_releases)) { |
| 1010 | msg = list_first_entry(&session->s_cap_releases, |
| 1011 | struct ceph_msg, list_head); |
| 1012 | list_del_init(&msg->list_head); |
| 1013 | ceph_msg_put(msg); |
| 1014 | } |
| 1015 | while (!list_empty(&session->s_cap_releases_done)) { |
| 1016 | msg = list_first_entry(&session->s_cap_releases_done, |
| 1017 | struct ceph_msg, list_head); |
| 1018 | list_del_init(&msg->list_head); |
| 1019 | ceph_msg_put(msg); |
| 1020 | } |
| 1021 | spin_unlock(&session->s_cap_lock); |
| 1022 | } |
| 1023 | |
| 1024 | /* |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1025 | * Helper to safely iterate over all caps associated with a session, with |
| 1026 | * special care taken to handle a racing __ceph_remove_cap(). |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1027 | * |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1028 | * Caller must hold session s_mutex. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1029 | */ |
| 1030 | static int iterate_session_caps(struct ceph_mds_session *session, |
| 1031 | int (*cb)(struct inode *, struct ceph_cap *, |
| 1032 | void *), void *arg) |
| 1033 | { |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1034 | struct list_head *p; |
| 1035 | struct ceph_cap *cap; |
| 1036 | struct inode *inode, *last_inode = NULL; |
| 1037 | struct ceph_cap *old_cap = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1038 | int ret; |
| 1039 | |
| 1040 | dout("iterate_session_caps %p mds%d\n", session, session->s_mds); |
| 1041 | spin_lock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1042 | p = session->s_caps.next; |
| 1043 | while (p != &session->s_caps) { |
| 1044 | cap = list_entry(p, struct ceph_cap, session_caps); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1045 | inode = igrab(&cap->ci->vfs_inode); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1046 | if (!inode) { |
| 1047 | p = p->next; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1048 | continue; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1049 | } |
| 1050 | session->s_cap_iterator = cap; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1051 | spin_unlock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1052 | |
| 1053 | if (last_inode) { |
| 1054 | iput(last_inode); |
| 1055 | last_inode = NULL; |
| 1056 | } |
| 1057 | if (old_cap) { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1058 | ceph_put_cap(session->s_mdsc, old_cap); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1059 | old_cap = NULL; |
| 1060 | } |
| 1061 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1062 | ret = cb(inode, cap, arg); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1063 | last_inode = inode; |
| 1064 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1065 | spin_lock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1066 | p = p->next; |
| 1067 | if (cap->ci == NULL) { |
| 1068 | dout("iterate_session_caps finishing cap %p removal\n", |
| 1069 | cap); |
| 1070 | BUG_ON(cap->session != session); |
| 1071 | list_del_init(&cap->session_caps); |
| 1072 | session->s_nr_caps--; |
| 1073 | cap->session = NULL; |
| 1074 | old_cap = cap; /* put_cap it w/o locks held */ |
| 1075 | } |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1076 | if (ret < 0) |
| 1077 | goto out; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1078 | } |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1079 | ret = 0; |
| 1080 | out: |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1081 | session->s_cap_iterator = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1082 | spin_unlock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1083 | |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 1084 | iput(last_inode); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1085 | if (old_cap) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1086 | ceph_put_cap(session->s_mdsc, old_cap); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1087 | |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1088 | return ret; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | 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] | 1092 | void *arg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1093 | { |
| 1094 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1095 | int drop = 0; |
| 1096 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1097 | dout("removing cap %p, ci is %p, inode is %p\n", |
| 1098 | cap, ci, &ci->vfs_inode); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1099 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1100 | __ceph_remove_cap(cap, false); |
Yan, Zheng | 571ade3 | 2015-03-24 11:36:08 +0800 | [diff] [blame^] | 1101 | if (!ci->i_auth_cap) { |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1102 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1103 | ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1104 | |
| 1105 | spin_lock(&mdsc->cap_dirty_lock); |
| 1106 | if (!list_empty(&ci->i_dirty_item)) { |
| 1107 | pr_info(" dropping dirty %s state for %p %lld\n", |
| 1108 | ceph_cap_string(ci->i_dirty_caps), |
| 1109 | inode, ceph_ino(inode)); |
| 1110 | ci->i_dirty_caps = 0; |
| 1111 | list_del_init(&ci->i_dirty_item); |
| 1112 | drop = 1; |
| 1113 | } |
| 1114 | if (!list_empty(&ci->i_flushing_item)) { |
| 1115 | pr_info(" dropping dirty+flushing %s state for %p %lld\n", |
| 1116 | ceph_cap_string(ci->i_flushing_caps), |
| 1117 | inode, ceph_ino(inode)); |
| 1118 | ci->i_flushing_caps = 0; |
| 1119 | list_del_init(&ci->i_flushing_item); |
| 1120 | mdsc->num_cap_flushing--; |
| 1121 | drop = 1; |
| 1122 | } |
| 1123 | if (drop && ci->i_wrbuffer_ref) { |
| 1124 | pr_info(" dropping dirty data for %p %lld\n", |
| 1125 | inode, ceph_ino(inode)); |
| 1126 | ci->i_wrbuffer_ref = 0; |
| 1127 | ci->i_wrbuffer_ref_head = 0; |
| 1128 | drop++; |
| 1129 | } |
| 1130 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1131 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1132 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1133 | while (drop--) |
| 1134 | iput(inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * caller must hold session s_mutex |
| 1140 | */ |
| 1141 | static void remove_session_caps(struct ceph_mds_session *session) |
| 1142 | { |
| 1143 | dout("remove_session_caps on %p\n", session); |
| 1144 | iterate_session_caps(session, remove_session_caps_cb, NULL); |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1145 | |
| 1146 | spin_lock(&session->s_cap_lock); |
| 1147 | if (session->s_nr_caps > 0) { |
| 1148 | struct super_block *sb = session->s_mdsc->fsc->sb; |
| 1149 | struct inode *inode; |
| 1150 | struct ceph_cap *cap, *prev = NULL; |
| 1151 | struct ceph_vino vino; |
| 1152 | /* |
| 1153 | * iterate_session_caps() skips inodes that are being |
| 1154 | * deleted, we need to wait until deletions are complete. |
| 1155 | * __wait_on_freeing_inode() is designed for the job, |
| 1156 | * but it is not exported, so use lookup inode function |
| 1157 | * to access it. |
| 1158 | */ |
| 1159 | while (!list_empty(&session->s_caps)) { |
| 1160 | cap = list_entry(session->s_caps.next, |
| 1161 | struct ceph_cap, session_caps); |
| 1162 | if (cap == prev) |
| 1163 | break; |
| 1164 | prev = cap; |
| 1165 | vino = cap->ci->i_vino; |
| 1166 | spin_unlock(&session->s_cap_lock); |
| 1167 | |
Yan, Zheng | ed284c4 | 2013-09-02 15:19:53 +0800 | [diff] [blame] | 1168 | inode = ceph_find_inode(sb, vino); |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1169 | iput(inode); |
| 1170 | |
| 1171 | spin_lock(&session->s_cap_lock); |
| 1172 | } |
| 1173 | } |
| 1174 | spin_unlock(&session->s_cap_lock); |
| 1175 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1176 | BUG_ON(session->s_nr_caps > 0); |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1177 | BUG_ON(!list_empty(&session->s_cap_flushing)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1178 | cleanup_cap_releases(session); |
| 1179 | } |
| 1180 | |
| 1181 | /* |
| 1182 | * wake up any threads waiting on this session's caps. if the cap is |
| 1183 | * old (didn't get renewed on the client reconnect), remove it now. |
| 1184 | * |
| 1185 | * caller must hold s_mutex. |
| 1186 | */ |
| 1187 | static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap, |
| 1188 | void *arg) |
| 1189 | { |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1190 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1191 | |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 1192 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1193 | if (arg) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1194 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1195 | ci->i_wanted_max_size = 0; |
| 1196 | ci->i_requested_max_size = 0; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1197 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1198 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1202 | static void wake_up_session_caps(struct ceph_mds_session *session, |
| 1203 | int reconnect) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1204 | { |
| 1205 | dout("wake_up_session_caps %p mds%d\n", session, session->s_mds); |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1206 | iterate_session_caps(session, wake_up_session_cb, |
| 1207 | (void *)(unsigned long)reconnect); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * Send periodic message to MDS renewing all currently held caps. The |
| 1212 | * ack will reset the expiration for all caps from this session. |
| 1213 | * |
| 1214 | * caller holds s_mutex |
| 1215 | */ |
| 1216 | static int send_renew_caps(struct ceph_mds_client *mdsc, |
| 1217 | struct ceph_mds_session *session) |
| 1218 | { |
| 1219 | struct ceph_msg *msg; |
| 1220 | int state; |
| 1221 | |
| 1222 | if (time_after_eq(jiffies, session->s_cap_ttl) && |
| 1223 | time_after_eq(session->s_cap_ttl, session->s_renew_requested)) |
| 1224 | pr_info("mds%d caps stale\n", session->s_mds); |
Sage Weil | e4cb4cb | 2010-03-18 13:43:09 -0700 | [diff] [blame] | 1225 | session->s_renew_requested = jiffies; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1226 | |
| 1227 | /* do not try to renew caps until a recovering mds has reconnected |
| 1228 | * with its clients. */ |
| 1229 | state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds); |
| 1230 | if (state < CEPH_MDS_STATE_RECONNECT) { |
| 1231 | dout("send_renew_caps ignoring mds%d (%s)\n", |
| 1232 | session->s_mds, ceph_mds_state_name(state)); |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | dout("send_renew_caps to mds%d (%s)\n", session->s_mds, |
| 1237 | ceph_mds_state_name(state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1238 | msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS, |
| 1239 | ++session->s_renew_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1240 | if (!msg) |
| 1241 | return -ENOMEM; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1242 | ceph_con_send(&session->s_con, msg); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 1246 | static int send_flushmsg_ack(struct ceph_mds_client *mdsc, |
| 1247 | struct ceph_mds_session *session, u64 seq) |
| 1248 | { |
| 1249 | struct ceph_msg *msg; |
| 1250 | |
| 1251 | dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 1252 | session->s_mds, ceph_session_state_name(session->s_state), seq); |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 1253 | msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq); |
| 1254 | if (!msg) |
| 1255 | return -ENOMEM; |
| 1256 | ceph_con_send(&session->s_con, msg); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
| 1260 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1261 | /* |
| 1262 | * Note new cap ttl, and any transition from stale -> not stale (fresh?). |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1263 | * |
| 1264 | * Called under session->s_mutex |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1265 | */ |
| 1266 | static void renewed_caps(struct ceph_mds_client *mdsc, |
| 1267 | struct ceph_mds_session *session, int is_renew) |
| 1268 | { |
| 1269 | int was_stale; |
| 1270 | int wake = 0; |
| 1271 | |
| 1272 | spin_lock(&session->s_cap_lock); |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 1273 | was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1274 | |
| 1275 | session->s_cap_ttl = session->s_renew_requested + |
| 1276 | mdsc->mdsmap->m_session_timeout*HZ; |
| 1277 | |
| 1278 | if (was_stale) { |
| 1279 | if (time_before(jiffies, session->s_cap_ttl)) { |
| 1280 | pr_info("mds%d caps renewed\n", session->s_mds); |
| 1281 | wake = 1; |
| 1282 | } else { |
| 1283 | pr_info("mds%d caps still stale\n", session->s_mds); |
| 1284 | } |
| 1285 | } |
| 1286 | dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n", |
| 1287 | session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh", |
| 1288 | time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh"); |
| 1289 | spin_unlock(&session->s_cap_lock); |
| 1290 | |
| 1291 | if (wake) |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1292 | wake_up_session_caps(session, 0); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * send a session close request |
| 1297 | */ |
| 1298 | static int request_close_session(struct ceph_mds_client *mdsc, |
| 1299 | struct ceph_mds_session *session) |
| 1300 | { |
| 1301 | struct ceph_msg *msg; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1302 | |
| 1303 | dout("request_close_session mds%d state %s seq %lld\n", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 1304 | session->s_mds, ceph_session_state_name(session->s_state), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1305 | session->s_seq); |
| 1306 | msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1307 | if (!msg) |
| 1308 | return -ENOMEM; |
| 1309 | ceph_con_send(&session->s_con, msg); |
| 1310 | return 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * Called with s_mutex held. |
| 1315 | */ |
| 1316 | static int __close_session(struct ceph_mds_client *mdsc, |
| 1317 | struct ceph_mds_session *session) |
| 1318 | { |
| 1319 | if (session->s_state >= CEPH_MDS_SESSION_CLOSING) |
| 1320 | return 0; |
| 1321 | session->s_state = CEPH_MDS_SESSION_CLOSING; |
| 1322 | return request_close_session(mdsc, session); |
| 1323 | } |
| 1324 | |
| 1325 | /* |
| 1326 | * Trim old(er) caps. |
| 1327 | * |
| 1328 | * Because we can't cache an inode without one or more caps, we do |
| 1329 | * this indirectly: if a cap is unused, we prune its aliases, at which |
| 1330 | * point the inode will hopefully get dropped to. |
| 1331 | * |
| 1332 | * Yes, this is a bit sloppy. Our only real goal here is to respond to |
| 1333 | * memory pressure from the MDS, though, so it needn't be perfect. |
| 1334 | */ |
| 1335 | static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg) |
| 1336 | { |
| 1337 | struct ceph_mds_session *session = arg; |
| 1338 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1339 | int used, wanted, oissued, mine; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1340 | |
| 1341 | if (session->s_trim_caps <= 0) |
| 1342 | return -1; |
| 1343 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1344 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1345 | mine = cap->issued | cap->implemented; |
| 1346 | used = __ceph_caps_used(ci); |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1347 | wanted = __ceph_caps_file_wanted(ci); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1348 | oissued = __ceph_caps_issued_other(ci, cap); |
| 1349 | |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1350 | 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] | 1351 | inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued), |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1352 | ceph_cap_string(used), ceph_cap_string(wanted)); |
| 1353 | if (cap == ci->i_auth_cap) { |
| 1354 | if (ci->i_dirty_caps | ci->i_flushing_caps) |
| 1355 | goto out; |
| 1356 | if ((used | wanted) & CEPH_CAP_ANY_WR) |
| 1357 | goto out; |
| 1358 | } |
| 1359 | if ((used | wanted) & ~oissued & mine) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1360 | goto out; /* we need these caps */ |
| 1361 | |
| 1362 | session->s_trim_caps--; |
| 1363 | if (oissued) { |
| 1364 | /* we aren't the only cap.. just remove us */ |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1365 | __ceph_remove_cap(cap, true); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1366 | } else { |
| 1367 | /* try to drop referring dentries */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1368 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1369 | d_prune_aliases(inode); |
| 1370 | dout("trim_caps_cb %p cap %p pruned, count now %d\n", |
| 1371 | inode, cap, atomic_read(&inode->i_count)); |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1376 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * Trim session cap count down to some max number. |
| 1382 | */ |
| 1383 | static int trim_caps(struct ceph_mds_client *mdsc, |
| 1384 | struct ceph_mds_session *session, |
| 1385 | int max_caps) |
| 1386 | { |
| 1387 | int trim_caps = session->s_nr_caps - max_caps; |
| 1388 | |
| 1389 | dout("trim_caps mds%d start: %d / %d, trim %d\n", |
| 1390 | session->s_mds, session->s_nr_caps, max_caps, trim_caps); |
| 1391 | if (trim_caps > 0) { |
| 1392 | session->s_trim_caps = trim_caps; |
| 1393 | iterate_session_caps(session, trim_caps_cb, session); |
| 1394 | dout("trim_caps mds%d done: %d / %d, trimmed %d\n", |
| 1395 | session->s_mds, session->s_nr_caps, max_caps, |
| 1396 | trim_caps - session->s_trim_caps); |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1397 | session->s_trim_caps = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1398 | } |
Yan, Zheng | a56371d | 2014-04-01 20:34:56 +0800 | [diff] [blame] | 1399 | |
| 1400 | ceph_add_cap_releases(mdsc, session); |
| 1401 | ceph_send_cap_releases(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * Allocate cap_release messages. If there is a partially full message |
| 1407 | * in the queue, try to allocate enough to cover it's remainder, so that |
| 1408 | * we can send it immediately. |
| 1409 | * |
| 1410 | * Called under s_mutex. |
| 1411 | */ |
Sage Weil | 2b2300d | 2010-06-09 16:52:04 -0700 | [diff] [blame] | 1412 | int ceph_add_cap_releases(struct ceph_mds_client *mdsc, |
Sage Weil | ee6b272 | 2010-06-10 12:55:52 -0700 | [diff] [blame] | 1413 | struct ceph_mds_session *session) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1414 | { |
Sage Weil | 38e8883e | 2010-06-10 12:57:11 -0700 | [diff] [blame] | 1415 | struct ceph_msg *msg, *partial = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1416 | struct ceph_mds_cap_release *head; |
| 1417 | int err = -ENOMEM; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1418 | int extra = mdsc->fsc->mount_options->cap_release_safety; |
Sage Weil | 38e8883e | 2010-06-10 12:57:11 -0700 | [diff] [blame] | 1419 | int num; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1420 | |
Sage Weil | 38e8883e | 2010-06-10 12:57:11 -0700 | [diff] [blame] | 1421 | dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds, |
| 1422 | extra); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1423 | |
| 1424 | spin_lock(&session->s_cap_lock); |
| 1425 | |
| 1426 | if (!list_empty(&session->s_cap_releases)) { |
| 1427 | msg = list_first_entry(&session->s_cap_releases, |
| 1428 | struct ceph_msg, |
| 1429 | list_head); |
| 1430 | head = msg->front.iov_base; |
Sage Weil | 38e8883e | 2010-06-10 12:57:11 -0700 | [diff] [blame] | 1431 | num = le32_to_cpu(head->num); |
| 1432 | if (num) { |
| 1433 | dout(" partial %p with (%d/%d)\n", msg, num, |
| 1434 | (int)CEPH_CAPS_PER_RELEASE); |
| 1435 | extra += CEPH_CAPS_PER_RELEASE - num; |
| 1436 | partial = msg; |
| 1437 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1438 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1439 | while (session->s_num_cap_releases < session->s_nr_caps + extra) { |
| 1440 | spin_unlock(&session->s_cap_lock); |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 1441 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE, |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 1442 | GFP_NOFS, false); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1443 | if (!msg) |
| 1444 | goto out_unlocked; |
| 1445 | dout("add_cap_releases %p msg %p now %d\n", session, msg, |
| 1446 | (int)msg->front.iov_len); |
| 1447 | head = msg->front.iov_base; |
| 1448 | head->num = cpu_to_le32(0); |
| 1449 | msg->front.iov_len = sizeof(*head); |
| 1450 | spin_lock(&session->s_cap_lock); |
| 1451 | list_add(&msg->list_head, &session->s_cap_releases); |
| 1452 | session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE; |
| 1453 | } |
| 1454 | |
Sage Weil | 38e8883e | 2010-06-10 12:57:11 -0700 | [diff] [blame] | 1455 | if (partial) { |
| 1456 | head = partial->front.iov_base; |
| 1457 | num = le32_to_cpu(head->num); |
| 1458 | dout(" queueing partial %p with %d/%d\n", partial, num, |
| 1459 | (int)CEPH_CAPS_PER_RELEASE); |
| 1460 | list_move_tail(&partial->list_head, |
| 1461 | &session->s_cap_releases_done); |
| 1462 | session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1463 | } |
| 1464 | err = 0; |
| 1465 | spin_unlock(&session->s_cap_lock); |
| 1466 | out_unlocked: |
| 1467 | return err; |
| 1468 | } |
| 1469 | |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1470 | static int check_cap_flush(struct inode *inode, u64 want_flush_seq) |
| 1471 | { |
| 1472 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1473 | int ret; |
| 1474 | spin_lock(&ci->i_ceph_lock); |
| 1475 | if (ci->i_flushing_caps) |
| 1476 | ret = ci->i_cap_flush_seq >= want_flush_seq; |
| 1477 | else |
| 1478 | ret = 1; |
| 1479 | spin_unlock(&ci->i_ceph_lock); |
| 1480 | return ret; |
| 1481 | } |
| 1482 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1483 | /* |
| 1484 | * flush all dirty inode data to disk. |
| 1485 | * |
| 1486 | * returns true if we've flushed through want_flush_seq |
| 1487 | */ |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1488 | static void wait_caps_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1489 | { |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1490 | int mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1491 | |
| 1492 | dout("check_cap_flush want %lld\n", want_flush_seq); |
| 1493 | mutex_lock(&mdsc->mutex); |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1494 | for (mds = 0; mds < mdsc->max_sessions; mds++) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1495 | struct ceph_mds_session *session = mdsc->sessions[mds]; |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1496 | struct inode *inode = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1497 | |
| 1498 | if (!session) |
| 1499 | continue; |
| 1500 | get_session(session); |
| 1501 | mutex_unlock(&mdsc->mutex); |
| 1502 | |
| 1503 | mutex_lock(&session->s_mutex); |
| 1504 | if (!list_empty(&session->s_cap_flushing)) { |
| 1505 | struct ceph_inode_info *ci = |
| 1506 | list_entry(session->s_cap_flushing.next, |
| 1507 | struct ceph_inode_info, |
| 1508 | i_flushing_item); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1509 | |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1510 | if (!check_cap_flush(&ci->vfs_inode, want_flush_seq)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1511 | dout("check_cap_flush still flushing %p " |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1512 | "seq %lld <= %lld to mds%d\n", |
| 1513 | &ci->vfs_inode, ci->i_cap_flush_seq, |
| 1514 | want_flush_seq, session->s_mds); |
| 1515 | inode = igrab(&ci->vfs_inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1516 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1517 | } |
| 1518 | mutex_unlock(&session->s_mutex); |
| 1519 | ceph_put_mds_session(session); |
| 1520 | |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1521 | if (inode) { |
| 1522 | wait_event(mdsc->cap_flushing_wq, |
| 1523 | check_cap_flush(inode, want_flush_seq)); |
| 1524 | iput(inode); |
| 1525 | } |
| 1526 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1527 | mutex_lock(&mdsc->mutex); |
| 1528 | } |
| 1529 | |
| 1530 | mutex_unlock(&mdsc->mutex); |
| 1531 | dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * called under s_mutex |
| 1536 | */ |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 1537 | void ceph_send_cap_releases(struct ceph_mds_client *mdsc, |
| 1538 | struct ceph_mds_session *session) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1539 | { |
| 1540 | struct ceph_msg *msg; |
| 1541 | |
| 1542 | dout("send_cap_releases mds%d\n", session->s_mds); |
Sage Weil | 0f8605f | 2010-05-05 15:51:35 -0700 | [diff] [blame] | 1543 | spin_lock(&session->s_cap_lock); |
| 1544 | while (!list_empty(&session->s_cap_releases_done)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1545 | msg = list_first_entry(&session->s_cap_releases_done, |
| 1546 | struct ceph_msg, list_head); |
| 1547 | list_del_init(&msg->list_head); |
| 1548 | spin_unlock(&session->s_cap_lock); |
| 1549 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 1550 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); |
| 1551 | ceph_con_send(&session->s_con, msg); |
Sage Weil | 0f8605f | 2010-05-05 15:51:35 -0700 | [diff] [blame] | 1552 | spin_lock(&session->s_cap_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1553 | } |
| 1554 | spin_unlock(&session->s_cap_lock); |
| 1555 | } |
| 1556 | |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 1557 | static void discard_cap_releases(struct ceph_mds_client *mdsc, |
| 1558 | struct ceph_mds_session *session) |
| 1559 | { |
| 1560 | struct ceph_msg *msg; |
| 1561 | struct ceph_mds_cap_release *head; |
| 1562 | unsigned num; |
| 1563 | |
| 1564 | dout("discard_cap_releases mds%d\n", session->s_mds); |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 1565 | |
Yan, Zheng | 00bd8ed | 2014-03-24 09:56:43 +0800 | [diff] [blame] | 1566 | if (!list_empty(&session->s_cap_releases)) { |
| 1567 | /* zero out the in-progress message */ |
| 1568 | msg = list_first_entry(&session->s_cap_releases, |
| 1569 | struct ceph_msg, list_head); |
| 1570 | head = msg->front.iov_base; |
| 1571 | num = le32_to_cpu(head->num); |
| 1572 | dout("discard_cap_releases mds%d %p %u\n", |
| 1573 | session->s_mds, msg, num); |
| 1574 | head->num = cpu_to_le32(0); |
| 1575 | msg->front.iov_len = sizeof(*head); |
| 1576 | session->s_num_cap_releases += num; |
| 1577 | } |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 1578 | |
| 1579 | /* requeue completed messages */ |
| 1580 | while (!list_empty(&session->s_cap_releases_done)) { |
| 1581 | msg = list_first_entry(&session->s_cap_releases_done, |
| 1582 | struct ceph_msg, list_head); |
| 1583 | list_del_init(&msg->list_head); |
| 1584 | |
| 1585 | head = msg->front.iov_base; |
| 1586 | num = le32_to_cpu(head->num); |
| 1587 | dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, |
| 1588 | num); |
| 1589 | session->s_num_cap_releases += num; |
| 1590 | head->num = cpu_to_le32(0); |
| 1591 | msg->front.iov_len = sizeof(*head); |
| 1592 | list_add(&msg->list_head, &session->s_cap_releases); |
| 1593 | } |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1596 | /* |
| 1597 | * requests |
| 1598 | */ |
| 1599 | |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 1600 | int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req, |
| 1601 | struct inode *dir) |
| 1602 | { |
| 1603 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 1604 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
| 1605 | struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options; |
| 1606 | size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) + |
| 1607 | sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease); |
| 1608 | int order, num_entries; |
| 1609 | |
| 1610 | spin_lock(&ci->i_ceph_lock); |
| 1611 | num_entries = ci->i_files + ci->i_subdirs; |
| 1612 | spin_unlock(&ci->i_ceph_lock); |
| 1613 | num_entries = max(num_entries, 1); |
| 1614 | num_entries = min(num_entries, opt->max_readdir); |
| 1615 | |
| 1616 | order = get_order(size * num_entries); |
| 1617 | while (order >= 0) { |
| 1618 | rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN, |
| 1619 | order); |
| 1620 | if (rinfo->dir_in) |
| 1621 | break; |
| 1622 | order--; |
| 1623 | } |
| 1624 | if (!rinfo->dir_in) |
| 1625 | return -ENOMEM; |
| 1626 | |
| 1627 | num_entries = (PAGE_SIZE << order) / size; |
| 1628 | num_entries = min(num_entries, opt->max_readdir); |
| 1629 | |
| 1630 | rinfo->dir_buf_size = PAGE_SIZE << order; |
| 1631 | req->r_num_caps = num_entries + 1; |
| 1632 | req->r_args.readdir.max_entries = cpu_to_le32(num_entries); |
| 1633 | req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes); |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1637 | /* |
| 1638 | * Create an mds request. |
| 1639 | */ |
| 1640 | struct ceph_mds_request * |
| 1641 | ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode) |
| 1642 | { |
| 1643 | struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS); |
| 1644 | |
| 1645 | if (!req) |
| 1646 | return ERR_PTR(-ENOMEM); |
| 1647 | |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 1648 | mutex_init(&req->r_fill_mutex); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1649 | req->r_mdsc = mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1650 | req->r_started = jiffies; |
| 1651 | req->r_resend_mds = -1; |
| 1652 | INIT_LIST_HEAD(&req->r_unsafe_dir_item); |
| 1653 | req->r_fmode = -1; |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 1654 | kref_init(&req->r_kref); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1655 | INIT_LIST_HEAD(&req->r_wait); |
| 1656 | init_completion(&req->r_completion); |
| 1657 | init_completion(&req->r_safe_completion); |
| 1658 | INIT_LIST_HEAD(&req->r_unsafe_item); |
| 1659 | |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 1660 | req->r_stamp = CURRENT_TIME; |
| 1661 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1662 | req->r_op = op; |
| 1663 | req->r_direct_mode = mode; |
| 1664 | return req; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 1668 | * return oldest (lowest) request, tid in request tree, 0 if none. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1669 | * |
| 1670 | * called under mdsc->mutex. |
| 1671 | */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 1672 | static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc) |
| 1673 | { |
| 1674 | if (RB_EMPTY_ROOT(&mdsc->request_tree)) |
| 1675 | return NULL; |
| 1676 | return rb_entry(rb_first(&mdsc->request_tree), |
| 1677 | struct ceph_mds_request, r_node); |
| 1678 | } |
| 1679 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1680 | static u64 __get_oldest_tid(struct ceph_mds_client *mdsc) |
| 1681 | { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 1682 | struct ceph_mds_request *req = __get_oldest_req(mdsc); |
| 1683 | |
| 1684 | if (req) |
| 1685 | return req->r_tid; |
| 1686 | return 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /* |
| 1690 | * Build a dentry's path. Allocate on heap; caller must kfree. Based |
| 1691 | * on build_path_from_dentry in fs/cifs/dir.c. |
| 1692 | * |
| 1693 | * If @stop_on_nosnap, generate path relative to the first non-snapped |
| 1694 | * inode. |
| 1695 | * |
| 1696 | * Encode hidden .snap dirs as a double /, i.e. |
| 1697 | * foo/.snap/bar -> foo//bar |
| 1698 | */ |
| 1699 | char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base, |
| 1700 | int stop_on_nosnap) |
| 1701 | { |
| 1702 | struct dentry *temp; |
| 1703 | char *path; |
| 1704 | int len, pos; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1705 | unsigned seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1706 | |
| 1707 | if (dentry == NULL) |
| 1708 | return ERR_PTR(-EINVAL); |
| 1709 | |
| 1710 | retry: |
| 1711 | len = 0; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1712 | seq = read_seqbegin(&rename_lock); |
| 1713 | rcu_read_lock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1714 | for (temp = dentry; !IS_ROOT(temp);) { |
| 1715 | struct inode *inode = temp->d_inode; |
| 1716 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) |
| 1717 | len++; /* slash only */ |
| 1718 | else if (stop_on_nosnap && inode && |
| 1719 | ceph_snap(inode) == CEPH_NOSNAP) |
| 1720 | break; |
| 1721 | else |
| 1722 | len += 1 + temp->d_name.len; |
| 1723 | temp = temp->d_parent; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1724 | } |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1725 | rcu_read_unlock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1726 | if (len) |
| 1727 | len--; /* no leading '/' */ |
| 1728 | |
| 1729 | path = kmalloc(len+1, GFP_NOFS); |
| 1730 | if (path == NULL) |
| 1731 | return ERR_PTR(-ENOMEM); |
| 1732 | pos = len; |
| 1733 | path[pos] = 0; /* trailing null */ |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1734 | rcu_read_lock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1735 | for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) { |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1736 | struct inode *inode; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1737 | |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1738 | spin_lock(&temp->d_lock); |
| 1739 | inode = temp->d_inode; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1740 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) { |
Sage Weil | 104648a | 2010-03-18 10:14:30 -0700 | [diff] [blame] | 1741 | dout("build_path path+%d: %p SNAPDIR\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1742 | pos, temp); |
| 1743 | } else if (stop_on_nosnap && inode && |
| 1744 | ceph_snap(inode) == CEPH_NOSNAP) { |
Yehuda Sadeh | 9d5a09e | 2011-12-13 09:57:44 -0800 | [diff] [blame] | 1745 | spin_unlock(&temp->d_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1746 | break; |
| 1747 | } else { |
| 1748 | pos -= temp->d_name.len; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1749 | if (pos < 0) { |
| 1750 | spin_unlock(&temp->d_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1751 | break; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1752 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1753 | strncpy(path + pos, temp->d_name.name, |
| 1754 | temp->d_name.len); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1755 | } |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1756 | spin_unlock(&temp->d_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1757 | if (pos) |
| 1758 | path[--pos] = '/'; |
| 1759 | temp = temp->d_parent; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1760 | } |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1761 | rcu_read_unlock(); |
| 1762 | if (pos != 0 || read_seqretry(&rename_lock, seq)) { |
Sage Weil | 104648a | 2010-03-18 10:14:30 -0700 | [diff] [blame] | 1763 | pr_err("build_path did not end path lookup where " |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1764 | "expected, namelen is %d, pos is %d\n", len, pos); |
| 1765 | /* presumably this is only possible if racing with a |
| 1766 | rename of one of the parent directories (we can not |
| 1767 | lock the dentries above us to prevent this, but |
| 1768 | retrying should be harmless) */ |
| 1769 | kfree(path); |
| 1770 | goto retry; |
| 1771 | } |
| 1772 | |
| 1773 | *base = ceph_ino(temp->d_inode); |
| 1774 | *plen = len; |
Sage Weil | 104648a | 2010-03-18 10:14:30 -0700 | [diff] [blame] | 1775 | dout("build_path on %p %d built %llx '%.*s'\n", |
Al Viro | 84d08fa | 2013-07-05 18:59:33 +0400 | [diff] [blame] | 1776 | dentry, d_count(dentry), *base, len, path); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1777 | return path; |
| 1778 | } |
| 1779 | |
| 1780 | static int build_dentry_path(struct dentry *dentry, |
| 1781 | const char **ppath, int *ppathlen, u64 *pino, |
| 1782 | int *pfreepath) |
| 1783 | { |
| 1784 | char *path; |
| 1785 | |
| 1786 | if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) { |
| 1787 | *pino = ceph_ino(dentry->d_parent->d_inode); |
| 1788 | *ppath = dentry->d_name.name; |
| 1789 | *ppathlen = dentry->d_name.len; |
| 1790 | return 0; |
| 1791 | } |
| 1792 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); |
| 1793 | if (IS_ERR(path)) |
| 1794 | return PTR_ERR(path); |
| 1795 | *ppath = path; |
| 1796 | *pfreepath = 1; |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | static int build_inode_path(struct inode *inode, |
| 1801 | const char **ppath, int *ppathlen, u64 *pino, |
| 1802 | int *pfreepath) |
| 1803 | { |
| 1804 | struct dentry *dentry; |
| 1805 | char *path; |
| 1806 | |
| 1807 | if (ceph_snap(inode) == CEPH_NOSNAP) { |
| 1808 | *pino = ceph_ino(inode); |
| 1809 | *ppathlen = 0; |
| 1810 | return 0; |
| 1811 | } |
| 1812 | dentry = d_find_alias(inode); |
| 1813 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); |
| 1814 | dput(dentry); |
| 1815 | if (IS_ERR(path)) |
| 1816 | return PTR_ERR(path); |
| 1817 | *ppath = path; |
| 1818 | *pfreepath = 1; |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * request arguments may be specified via an inode *, a dentry *, or |
| 1824 | * an explicit ino+path. |
| 1825 | */ |
| 1826 | static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry, |
| 1827 | const char *rpath, u64 rino, |
| 1828 | const char **ppath, int *pathlen, |
| 1829 | u64 *ino, int *freepath) |
| 1830 | { |
| 1831 | int r = 0; |
| 1832 | |
| 1833 | if (rinode) { |
| 1834 | r = build_inode_path(rinode, ppath, pathlen, ino, freepath); |
| 1835 | dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode), |
| 1836 | ceph_snap(rinode)); |
| 1837 | } else if (rdentry) { |
| 1838 | r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath); |
| 1839 | dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen, |
| 1840 | *ppath); |
Sage Weil | 795858d | 2011-08-15 13:02:37 -0700 | [diff] [blame] | 1841 | } else if (rpath || rino) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1842 | *ino = rino; |
| 1843 | *ppath = rpath; |
David Zafman | b000056 | 2012-10-25 10:23:46 -0700 | [diff] [blame] | 1844 | *pathlen = rpath ? strlen(rpath) : 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1845 | dout(" path %.*s\n", *pathlen, rpath); |
| 1846 | } |
| 1847 | |
| 1848 | return r; |
| 1849 | } |
| 1850 | |
| 1851 | /* |
| 1852 | * called under mdsc->mutex |
| 1853 | */ |
| 1854 | static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc, |
| 1855 | struct ceph_mds_request *req, |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 1856 | int mds, bool drop_cap_releases) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1857 | { |
| 1858 | struct ceph_msg *msg; |
| 1859 | struct ceph_mds_request_head *head; |
| 1860 | const char *path1 = NULL; |
| 1861 | const char *path2 = NULL; |
| 1862 | u64 ino1 = 0, ino2 = 0; |
| 1863 | int pathlen1 = 0, pathlen2 = 0; |
| 1864 | int freepath1 = 0, freepath2 = 0; |
| 1865 | int len; |
| 1866 | u16 releases; |
| 1867 | void *p, *end; |
| 1868 | int ret; |
| 1869 | |
| 1870 | ret = set_request_path_attr(req->r_inode, req->r_dentry, |
| 1871 | req->r_path1, req->r_ino1.ino, |
| 1872 | &path1, &pathlen1, &ino1, &freepath1); |
| 1873 | if (ret < 0) { |
| 1874 | msg = ERR_PTR(ret); |
| 1875 | goto out; |
| 1876 | } |
| 1877 | |
| 1878 | ret = set_request_path_attr(NULL, req->r_old_dentry, |
| 1879 | req->r_path2, req->r_ino2.ino, |
| 1880 | &path2, &pathlen2, &ino2, &freepath2); |
| 1881 | if (ret < 0) { |
| 1882 | msg = ERR_PTR(ret); |
| 1883 | goto out_free1; |
| 1884 | } |
| 1885 | |
| 1886 | len = sizeof(*head) + |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 1887 | pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) + |
| 1888 | sizeof(struct timespec); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1889 | |
| 1890 | /* calculate (max) length for cap releases */ |
| 1891 | len += sizeof(struct ceph_mds_request_release) * |
| 1892 | (!!req->r_inode_drop + !!req->r_dentry_drop + |
| 1893 | !!req->r_old_inode_drop + !!req->r_old_dentry_drop); |
| 1894 | if (req->r_dentry_drop) |
| 1895 | len += req->r_dentry->d_name.len; |
| 1896 | if (req->r_old_dentry_drop) |
| 1897 | len += req->r_old_dentry->d_name.len; |
| 1898 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 1899 | msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1900 | if (!msg) { |
| 1901 | msg = ERR_PTR(-ENOMEM); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1902 | goto out_free2; |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1903 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1904 | |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 1905 | msg->hdr.version = cpu_to_le16(2); |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 1906 | msg->hdr.tid = cpu_to_le64(req->r_tid); |
| 1907 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1908 | head = msg->front.iov_base; |
| 1909 | p = msg->front.iov_base + sizeof(*head); |
| 1910 | end = msg->front.iov_base + msg->front.iov_len; |
| 1911 | |
| 1912 | head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch); |
| 1913 | head->op = cpu_to_le32(req->r_op); |
Eric W. Biederman | ff3d004 | 2013-01-31 04:01:53 -0800 | [diff] [blame] | 1914 | head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid)); |
| 1915 | head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1916 | head->args = req->r_args; |
| 1917 | |
| 1918 | ceph_encode_filepath(&p, end, ino1, path1); |
| 1919 | ceph_encode_filepath(&p, end, ino2, path2); |
| 1920 | |
Sage Weil | e979cf5 | 2010-07-15 14:58:39 -0700 | [diff] [blame] | 1921 | /* make note of release offset, in case we need to replay */ |
| 1922 | req->r_request_release_offset = p - msg->front.iov_base; |
| 1923 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1924 | /* cap releases */ |
| 1925 | releases = 0; |
| 1926 | if (req->r_inode_drop) |
| 1927 | releases += ceph_encode_inode_release(&p, |
| 1928 | req->r_inode ? req->r_inode : req->r_dentry->d_inode, |
| 1929 | mds, req->r_inode_drop, req->r_inode_unless, 0); |
| 1930 | if (req->r_dentry_drop) |
| 1931 | releases += ceph_encode_dentry_release(&p, req->r_dentry, |
| 1932 | mds, req->r_dentry_drop, req->r_dentry_unless); |
| 1933 | if (req->r_old_dentry_drop) |
| 1934 | releases += ceph_encode_dentry_release(&p, req->r_old_dentry, |
| 1935 | mds, req->r_old_dentry_drop, req->r_old_dentry_unless); |
| 1936 | if (req->r_old_inode_drop) |
| 1937 | releases += ceph_encode_inode_release(&p, |
| 1938 | req->r_old_dentry->d_inode, |
| 1939 | mds, req->r_old_inode_drop, req->r_old_inode_unless, 0); |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 1940 | |
| 1941 | if (drop_cap_releases) { |
| 1942 | releases = 0; |
| 1943 | p = msg->front.iov_base + req->r_request_release_offset; |
| 1944 | } |
| 1945 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1946 | head->num_releases = cpu_to_le16(releases); |
| 1947 | |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 1948 | /* time stamp */ |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 1949 | { |
| 1950 | struct ceph_timespec ts; |
| 1951 | ceph_encode_timespec(&ts, &req->r_stamp); |
| 1952 | ceph_encode_copy(&p, &ts, sizeof(ts)); |
| 1953 | } |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 1954 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1955 | BUG_ON(p > end); |
| 1956 | msg->front.iov_len = p - msg->front.iov_base; |
| 1957 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 1958 | |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 1959 | if (req->r_pagelist) { |
| 1960 | struct ceph_pagelist *pagelist = req->r_pagelist; |
| 1961 | atomic_inc(&pagelist->refcnt); |
| 1962 | ceph_msg_data_add_pagelist(msg, pagelist); |
| 1963 | msg->hdr.data_len = cpu_to_le32(pagelist->length); |
| 1964 | } else { |
| 1965 | msg->hdr.data_len = 0; |
Alex Elder | ebf18f4 | 2013-03-04 22:29:57 -0600 | [diff] [blame] | 1966 | } |
Alex Elder | 02afca6 | 2013-02-14 12:16:43 -0600 | [diff] [blame] | 1967 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1968 | msg->hdr.data_off = cpu_to_le16(0); |
| 1969 | |
| 1970 | out_free2: |
| 1971 | if (freepath2) |
| 1972 | kfree((char *)path2); |
| 1973 | out_free1: |
| 1974 | if (freepath1) |
| 1975 | kfree((char *)path1); |
| 1976 | out: |
| 1977 | return msg; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * called under mdsc->mutex if error, under no mutex if |
| 1982 | * success. |
| 1983 | */ |
| 1984 | static void complete_request(struct ceph_mds_client *mdsc, |
| 1985 | struct ceph_mds_request *req) |
| 1986 | { |
| 1987 | if (req->r_callback) |
| 1988 | req->r_callback(mdsc, req); |
| 1989 | else |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 1990 | complete_all(&req->r_completion); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * called under mdsc->mutex |
| 1995 | */ |
| 1996 | static int __prepare_send_request(struct ceph_mds_client *mdsc, |
| 1997 | struct ceph_mds_request *req, |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 1998 | int mds, bool drop_cap_releases) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1999 | { |
| 2000 | struct ceph_mds_request_head *rhead; |
| 2001 | struct ceph_msg *msg; |
| 2002 | int flags = 0; |
| 2003 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2004 | req->r_attempts++; |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2005 | if (req->r_inode) { |
| 2006 | struct ceph_cap *cap = |
| 2007 | ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds); |
| 2008 | |
| 2009 | if (cap) |
| 2010 | req->r_sent_on_mseq = cap->mseq; |
| 2011 | else |
| 2012 | req->r_sent_on_mseq = -1; |
| 2013 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2014 | dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req, |
| 2015 | req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts); |
| 2016 | |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2017 | if (req->r_got_unsafe) { |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2018 | void *p; |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2019 | /* |
| 2020 | * Replay. Do not regenerate message (and rebuild |
| 2021 | * paths, etc.); just use the original message. |
| 2022 | * Rebuilding paths will break for renames because |
| 2023 | * d_move mangles the src name. |
| 2024 | */ |
| 2025 | msg = req->r_request; |
| 2026 | rhead = msg->front.iov_base; |
| 2027 | |
| 2028 | flags = le32_to_cpu(rhead->flags); |
| 2029 | flags |= CEPH_MDS_FLAG_REPLAY; |
| 2030 | rhead->flags = cpu_to_le32(flags); |
| 2031 | |
| 2032 | if (req->r_target_inode) |
| 2033 | rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode)); |
| 2034 | |
| 2035 | rhead->num_retry = req->r_attempts - 1; |
Sage Weil | e979cf5 | 2010-07-15 14:58:39 -0700 | [diff] [blame] | 2036 | |
| 2037 | /* remove cap/dentry releases from message */ |
| 2038 | rhead->num_releases = 0; |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2039 | |
| 2040 | /* time stamp */ |
| 2041 | p = msg->front.iov_base + req->r_request_release_offset; |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2042 | { |
| 2043 | struct ceph_timespec ts; |
| 2044 | ceph_encode_timespec(&ts, &req->r_stamp); |
| 2045 | ceph_encode_copy(&p, &ts, sizeof(ts)); |
| 2046 | } |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2047 | |
| 2048 | msg->front.iov_len = p - msg->front.iov_base; |
| 2049 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2050 | return 0; |
| 2051 | } |
| 2052 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2053 | if (req->r_request) { |
| 2054 | ceph_msg_put(req->r_request); |
| 2055 | req->r_request = NULL; |
| 2056 | } |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2057 | msg = create_request_message(mdsc, req, mds, drop_cap_releases); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2058 | if (IS_ERR(msg)) { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2059 | req->r_err = PTR_ERR(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2060 | complete_request(mdsc, req); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2061 | return PTR_ERR(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2062 | } |
| 2063 | req->r_request = msg; |
| 2064 | |
| 2065 | rhead = msg->front.iov_base; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2066 | rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc)); |
| 2067 | if (req->r_got_unsafe) |
| 2068 | flags |= CEPH_MDS_FLAG_REPLAY; |
| 2069 | if (req->r_locked_dir) |
| 2070 | flags |= CEPH_MDS_FLAG_WANT_DENTRY; |
| 2071 | rhead->flags = cpu_to_le32(flags); |
| 2072 | rhead->num_fwd = req->r_num_fwd; |
| 2073 | rhead->num_retry = req->r_attempts - 1; |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2074 | rhead->ino = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2075 | |
| 2076 | dout(" r_locked_dir = %p\n", req->r_locked_dir); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2077 | return 0; |
| 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * send request, or put it on the appropriate wait list. |
| 2082 | */ |
| 2083 | static int __do_request(struct ceph_mds_client *mdsc, |
| 2084 | struct ceph_mds_request *req) |
| 2085 | { |
| 2086 | struct ceph_mds_session *session = NULL; |
| 2087 | int mds = -1; |
| 2088 | int err = -EAGAIN; |
| 2089 | |
Yan, Zheng | eb1b8af | 2013-09-26 14:25:36 +0800 | [diff] [blame] | 2090 | if (req->r_err || req->r_got_result) { |
| 2091 | if (req->r_aborted) |
| 2092 | __unregister_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2093 | goto out; |
Yan, Zheng | eb1b8af | 2013-09-26 14:25:36 +0800 | [diff] [blame] | 2094 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2095 | |
| 2096 | if (req->r_timeout && |
| 2097 | time_after_eq(jiffies, req->r_started + req->r_timeout)) { |
| 2098 | dout("do_request timed out\n"); |
| 2099 | err = -EIO; |
| 2100 | goto finish; |
| 2101 | } |
| 2102 | |
Sage Weil | dc69e2e | 2010-11-02 13:49:00 -0700 | [diff] [blame] | 2103 | put_request_session(req); |
| 2104 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2105 | mds = __choose_mds(mdsc, req); |
| 2106 | if (mds < 0 || |
| 2107 | ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) { |
| 2108 | dout("do_request no mds or not active, waiting for map\n"); |
| 2109 | list_add(&req->r_wait, &mdsc->waiting_for_map); |
| 2110 | goto out; |
| 2111 | } |
| 2112 | |
| 2113 | /* get, open session */ |
| 2114 | session = __ceph_lookup_mds_session(mdsc, mds); |
Sage Weil | 9c423956 | 2010-03-20 20:43:28 -0700 | [diff] [blame] | 2115 | if (!session) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2116 | session = register_session(mdsc, mds); |
Sage Weil | 9c423956 | 2010-03-20 20:43:28 -0700 | [diff] [blame] | 2117 | if (IS_ERR(session)) { |
| 2118 | err = PTR_ERR(session); |
| 2119 | goto finish; |
| 2120 | } |
| 2121 | } |
Sage Weil | dc69e2e | 2010-11-02 13:49:00 -0700 | [diff] [blame] | 2122 | req->r_session = get_session(session); |
| 2123 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2124 | dout("do_request mds%d session %p state %s\n", mds, session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 2125 | ceph_session_state_name(session->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2126 | if (session->s_state != CEPH_MDS_SESSION_OPEN && |
| 2127 | session->s_state != CEPH_MDS_SESSION_HUNG) { |
| 2128 | if (session->s_state == CEPH_MDS_SESSION_NEW || |
| 2129 | session->s_state == CEPH_MDS_SESSION_CLOSING) |
| 2130 | __open_session(mdsc, session); |
| 2131 | list_add(&req->r_wait, &session->s_waiting); |
| 2132 | goto out_session; |
| 2133 | } |
| 2134 | |
| 2135 | /* send request */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2136 | req->r_resend_mds = -1; /* forget any previous mds hint */ |
| 2137 | |
| 2138 | if (req->r_request_started == 0) /* note request start time */ |
| 2139 | req->r_request_started = jiffies; |
| 2140 | |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2141 | err = __prepare_send_request(mdsc, req, mds, false); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2142 | if (!err) { |
| 2143 | ceph_msg_get(req->r_request); |
| 2144 | ceph_con_send(&session->s_con, req->r_request); |
| 2145 | } |
| 2146 | |
| 2147 | out_session: |
| 2148 | ceph_put_mds_session(session); |
| 2149 | out: |
| 2150 | return err; |
| 2151 | |
| 2152 | finish: |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2153 | req->r_err = err; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2154 | complete_request(mdsc, req); |
| 2155 | goto out; |
| 2156 | } |
| 2157 | |
| 2158 | /* |
| 2159 | * called under mdsc->mutex |
| 2160 | */ |
| 2161 | static void __wake_requests(struct ceph_mds_client *mdsc, |
| 2162 | struct list_head *head) |
| 2163 | { |
Yan, Zheng | ed75ec2 | 2012-11-19 10:49:06 +0800 | [diff] [blame] | 2164 | struct ceph_mds_request *req; |
| 2165 | LIST_HEAD(tmp_list); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2166 | |
Yan, Zheng | ed75ec2 | 2012-11-19 10:49:06 +0800 | [diff] [blame] | 2167 | list_splice_init(head, &tmp_list); |
| 2168 | |
| 2169 | while (!list_empty(&tmp_list)) { |
| 2170 | req = list_entry(tmp_list.next, |
| 2171 | struct ceph_mds_request, r_wait); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2172 | list_del_init(&req->r_wait); |
Sage Weil | 7971bd9 | 2013-05-01 21:15:58 -0700 | [diff] [blame] | 2173 | dout(" wake request %p tid %llu\n", req, req->r_tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2174 | __do_request(mdsc, req); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | /* |
| 2179 | * Wake up threads with requests pending for @mds, so that they can |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2180 | * resubmit their requests to a possibly different mds. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2181 | */ |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2182 | static void kick_requests(struct ceph_mds_client *mdsc, int mds) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2183 | { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2184 | struct ceph_mds_request *req; |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2185 | struct rb_node *p = rb_first(&mdsc->request_tree); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2186 | |
| 2187 | dout("kick_requests mds%d\n", mds); |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2188 | while (p) { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2189 | req = rb_entry(p, struct ceph_mds_request, r_node); |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2190 | p = rb_next(p); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2191 | if (req->r_got_unsafe) |
| 2192 | continue; |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2193 | if (req->r_attempts > 0) |
| 2194 | continue; /* only new requests */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2195 | if (req->r_session && |
| 2196 | req->r_session->s_mds == mds) { |
| 2197 | dout(" kicking tid %llu\n", req->r_tid); |
Yan, Zheng | 03974e8 | 2014-09-11 14:28:56 +0800 | [diff] [blame] | 2198 | list_del_init(&req->r_wait); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2199 | __do_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, |
| 2205 | struct ceph_mds_request *req) |
| 2206 | { |
| 2207 | dout("submit_request on %p\n", req); |
| 2208 | mutex_lock(&mdsc->mutex); |
| 2209 | __register_request(mdsc, req, NULL); |
| 2210 | __do_request(mdsc, req); |
| 2211 | mutex_unlock(&mdsc->mutex); |
| 2212 | } |
| 2213 | |
| 2214 | /* |
| 2215 | * Synchrously perform an mds request. Take care of all of the |
| 2216 | * session setup, forwarding, retry details. |
| 2217 | */ |
| 2218 | int ceph_mdsc_do_request(struct ceph_mds_client *mdsc, |
| 2219 | struct inode *dir, |
| 2220 | struct ceph_mds_request *req) |
| 2221 | { |
| 2222 | int err; |
| 2223 | |
| 2224 | dout("do_request on %p\n", req); |
| 2225 | |
| 2226 | /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */ |
| 2227 | if (req->r_inode) |
| 2228 | ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); |
| 2229 | if (req->r_locked_dir) |
| 2230 | ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN); |
Sage Weil | 844d87c | 2013-02-05 13:40:09 -0800 | [diff] [blame] | 2231 | if (req->r_old_dentry_dir) |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 2232 | ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir), |
| 2233 | CEPH_CAP_PIN); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2234 | |
| 2235 | /* issue */ |
| 2236 | mutex_lock(&mdsc->mutex); |
| 2237 | __register_request(mdsc, req, dir); |
| 2238 | __do_request(mdsc, req); |
| 2239 | |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2240 | if (req->r_err) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2241 | err = req->r_err; |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2242 | __unregister_request(mdsc, req); |
| 2243 | dout("do_request early error %d\n", err); |
| 2244 | goto out; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2245 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2246 | |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2247 | /* wait */ |
| 2248 | mutex_unlock(&mdsc->mutex); |
| 2249 | dout("do_request waiting\n"); |
| 2250 | if (req->r_timeout) { |
Sage Weil | aa91647 | 2010-05-24 11:15:51 -0700 | [diff] [blame] | 2251 | err = (long)wait_for_completion_killable_timeout( |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2252 | &req->r_completion, req->r_timeout); |
| 2253 | if (err == 0) |
| 2254 | err = -EIO; |
Yan, Zheng | 9280be2 | 2014-10-14 10:33:35 +0800 | [diff] [blame] | 2255 | } else if (req->r_wait_for_completion) { |
| 2256 | err = req->r_wait_for_completion(mdsc, req); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2257 | } else { |
Sage Weil | aa91647 | 2010-05-24 11:15:51 -0700 | [diff] [blame] | 2258 | err = wait_for_completion_killable(&req->r_completion); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2259 | } |
| 2260 | dout("do_request waited, got %d\n", err); |
| 2261 | mutex_lock(&mdsc->mutex); |
| 2262 | |
| 2263 | /* only abort if we didn't race with a real reply */ |
| 2264 | if (req->r_got_result) { |
| 2265 | err = le32_to_cpu(req->r_reply_info.head->result); |
| 2266 | } else if (err < 0) { |
| 2267 | dout("aborted request %lld with %d\n", req->r_tid, err); |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2268 | |
| 2269 | /* |
| 2270 | * ensure we aren't running concurrently with |
| 2271 | * ceph_fill_trace or ceph_readdir_prepopulate, which |
| 2272 | * rely on locks (dir mutex) held by our caller. |
| 2273 | */ |
| 2274 | mutex_lock(&req->r_fill_mutex); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2275 | req->r_err = err; |
| 2276 | req->r_aborted = true; |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2277 | mutex_unlock(&req->r_fill_mutex); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2278 | |
| 2279 | if (req->r_locked_dir && |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2280 | (req->r_op & CEPH_MDS_OP_WRITE)) |
| 2281 | ceph_invalidate_dir_request(req); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2282 | } else { |
| 2283 | err = req->r_err; |
| 2284 | } |
| 2285 | |
| 2286 | out: |
| 2287 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2288 | dout("do_request %p done, result %d\n", req, err); |
| 2289 | return err; |
| 2290 | } |
| 2291 | |
| 2292 | /* |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 2293 | * Invalidate dir's completeness, dentry lease state on an aborted MDS |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2294 | * namespace request. |
| 2295 | */ |
| 2296 | void ceph_invalidate_dir_request(struct ceph_mds_request *req) |
| 2297 | { |
| 2298 | struct inode *inode = req->r_locked_dir; |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2299 | |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 2300 | dout("invalidate_dir_request %p (complete, lease(s))\n", inode); |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2301 | |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 2302 | ceph_dir_clear_complete(inode); |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2303 | if (req->r_dentry) |
| 2304 | ceph_invalidate_dentry_lease(req->r_dentry); |
| 2305 | if (req->r_old_dentry) |
| 2306 | ceph_invalidate_dentry_lease(req->r_old_dentry); |
| 2307 | } |
| 2308 | |
| 2309 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2310 | * Handle mds reply. |
| 2311 | * |
| 2312 | * We take the session mutex and parse and process the reply immediately. |
| 2313 | * This preserves the logical ordering of replies, capabilities, etc., sent |
| 2314 | * by the MDS as they are applied to our local cache. |
| 2315 | */ |
| 2316 | static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) |
| 2317 | { |
| 2318 | struct ceph_mds_client *mdsc = session->s_mdsc; |
| 2319 | struct ceph_mds_request *req; |
| 2320 | struct ceph_mds_reply_head *head = msg->front.iov_base; |
| 2321 | struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2322 | struct ceph_snap_realm *realm; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2323 | u64 tid; |
| 2324 | int err, result; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2325 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2326 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2327 | if (msg->front.iov_len < sizeof(*head)) { |
| 2328 | pr_err("mdsc_handle_reply got corrupt (short) reply\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2329 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2330 | return; |
| 2331 | } |
| 2332 | |
| 2333 | /* get request, session */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2334 | tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2335 | mutex_lock(&mdsc->mutex); |
| 2336 | req = __lookup_request(mdsc, tid); |
| 2337 | if (!req) { |
| 2338 | dout("handle_reply on unknown tid %llu\n", tid); |
| 2339 | mutex_unlock(&mdsc->mutex); |
| 2340 | return; |
| 2341 | } |
| 2342 | dout("handle_reply %p\n", req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2343 | |
| 2344 | /* correct session? */ |
Sage Weil | d96d604 | 2010-03-20 20:50:58 -0700 | [diff] [blame] | 2345 | if (req->r_session != session) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2346 | pr_err("mdsc_handle_reply got %llu on session mds%d" |
| 2347 | " not mds%d\n", tid, session->s_mds, |
| 2348 | req->r_session ? req->r_session->s_mds : -1); |
| 2349 | mutex_unlock(&mdsc->mutex); |
| 2350 | goto out; |
| 2351 | } |
| 2352 | |
| 2353 | /* dup? */ |
| 2354 | if ((req->r_got_unsafe && !head->safe) || |
| 2355 | (req->r_got_safe && head->safe)) { |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 2356 | 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] | 2357 | head->safe ? "safe" : "unsafe", tid, mds); |
| 2358 | mutex_unlock(&mdsc->mutex); |
| 2359 | goto out; |
| 2360 | } |
Sage Weil | 85792d0d | 2010-05-13 09:06:02 -0700 | [diff] [blame] | 2361 | if (req->r_got_safe && !head->safe) { |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 2362 | pr_warn("got unsafe after safe on %llu from mds%d\n", |
Sage Weil | 85792d0d | 2010-05-13 09:06:02 -0700 | [diff] [blame] | 2363 | tid, mds); |
| 2364 | mutex_unlock(&mdsc->mutex); |
| 2365 | goto out; |
| 2366 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2367 | |
| 2368 | result = le32_to_cpu(head->result); |
| 2369 | |
| 2370 | /* |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2371 | * Handle an ESTALE |
| 2372 | * if we're not talking to the authority, send to them |
| 2373 | * if the authority has changed while we weren't looking, |
| 2374 | * send to new authority |
| 2375 | * Otherwise we just have to return an ESTALE |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2376 | */ |
| 2377 | if (result == -ESTALE) { |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2378 | dout("got ESTALE on request %llu", req->r_tid); |
Yan, Zheng | 51da8e8 | 2014-07-14 10:48:29 +0800 | [diff] [blame] | 2379 | req->r_resend_mds = -1; |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 2380 | if (req->r_direct_mode != USE_AUTH_MDS) { |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2381 | dout("not using auth, setting for that now"); |
| 2382 | req->r_direct_mode = USE_AUTH_MDS; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2383 | __do_request(mdsc, req); |
| 2384 | mutex_unlock(&mdsc->mutex); |
| 2385 | goto out; |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2386 | } else { |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 2387 | int mds = __choose_mds(mdsc, req); |
| 2388 | if (mds >= 0 && mds != req->r_session->s_mds) { |
| 2389 | dout("but auth changed, so resending"); |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2390 | __do_request(mdsc, req); |
| 2391 | mutex_unlock(&mdsc->mutex); |
| 2392 | goto out; |
| 2393 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2394 | } |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2395 | dout("have to return ESTALE on request %llu", req->r_tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2398 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2399 | if (head->safe) { |
| 2400 | req->r_got_safe = true; |
| 2401 | __unregister_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2402 | |
| 2403 | if (req->r_got_unsafe) { |
| 2404 | /* |
| 2405 | * We already handled the unsafe response, now do the |
| 2406 | * cleanup. No need to examine the response; the MDS |
| 2407 | * doesn't include any result info in the safe |
| 2408 | * response. And even if it did, there is nothing |
| 2409 | * useful we could do with a revised return value. |
| 2410 | */ |
| 2411 | dout("got safe reply %llu, mds%d\n", tid, mds); |
| 2412 | list_del_init(&req->r_unsafe_item); |
| 2413 | |
| 2414 | /* last unsafe request during umount? */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2415 | if (mdsc->stopping && !__get_oldest_req(mdsc)) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2416 | complete_all(&mdsc->safe_umount_waiters); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2417 | mutex_unlock(&mdsc->mutex); |
| 2418 | goto out; |
| 2419 | } |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2420 | } else { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2421 | req->r_got_unsafe = true; |
| 2422 | list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe); |
| 2423 | } |
| 2424 | |
| 2425 | dout("handle_reply tid %lld result %d\n", tid, result); |
| 2426 | rinfo = &req->r_reply_info; |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 2427 | err = parse_reply_info(msg, rinfo, session->s_con.peer_features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2428 | mutex_unlock(&mdsc->mutex); |
| 2429 | |
| 2430 | mutex_lock(&session->s_mutex); |
| 2431 | if (err < 0) { |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 2432 | 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] | 2433 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2434 | goto out_err; |
| 2435 | } |
| 2436 | |
| 2437 | /* snap trace */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2438 | realm = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2439 | if (rinfo->snapblob_len) { |
| 2440 | down_write(&mdsc->snap_rwsem); |
| 2441 | ceph_update_snap_trace(mdsc, rinfo->snapblob, |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2442 | rinfo->snapblob + rinfo->snapblob_len, |
| 2443 | le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP, |
| 2444 | &realm); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2445 | downgrade_write(&mdsc->snap_rwsem); |
| 2446 | } else { |
| 2447 | down_read(&mdsc->snap_rwsem); |
| 2448 | } |
| 2449 | |
| 2450 | /* insert trace into our cache */ |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2451 | mutex_lock(&req->r_fill_mutex); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2452 | err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2453 | if (err == 0) { |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 2454 | if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR || |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 2455 | req->r_op == CEPH_MDS_OP_LSSNAP)) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2456 | ceph_readdir_prepopulate(req, req->r_session); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 2457 | ceph_unreserve_caps(mdsc, &req->r_caps_reservation); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2458 | } |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2459 | mutex_unlock(&req->r_fill_mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2460 | |
| 2461 | up_read(&mdsc->snap_rwsem); |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2462 | if (realm) |
| 2463 | ceph_put_snap_realm(mdsc, realm); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2464 | out_err: |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2465 | mutex_lock(&mdsc->mutex); |
| 2466 | if (!req->r_aborted) { |
| 2467 | if (err) { |
| 2468 | req->r_err = err; |
| 2469 | } else { |
| 2470 | req->r_reply = msg; |
| 2471 | ceph_msg_get(msg); |
| 2472 | req->r_got_result = true; |
| 2473 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2474 | } else { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2475 | dout("reply arrived after request %lld was aborted\n", tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2476 | } |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2477 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2478 | |
Sage Weil | ee6b272 | 2010-06-10 12:55:52 -0700 | [diff] [blame] | 2479 | ceph_add_cap_releases(mdsc, req->r_session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2480 | mutex_unlock(&session->s_mutex); |
| 2481 | |
| 2482 | /* kick calling process */ |
| 2483 | complete_request(mdsc, req); |
| 2484 | out: |
| 2485 | ceph_mdsc_put_request(req); |
| 2486 | return; |
| 2487 | } |
| 2488 | |
| 2489 | |
| 2490 | |
| 2491 | /* |
| 2492 | * handle mds notification that our request has been forwarded. |
| 2493 | */ |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2494 | static void handle_forward(struct ceph_mds_client *mdsc, |
| 2495 | struct ceph_mds_session *session, |
| 2496 | struct ceph_msg *msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2497 | { |
| 2498 | struct ceph_mds_request *req; |
Sage Weil | a1ea787 | 2010-02-23 14:02:44 -0800 | [diff] [blame] | 2499 | u64 tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2500 | u32 next_mds; |
| 2501 | u32 fwd_seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2502 | int err = -EINVAL; |
| 2503 | void *p = msg->front.iov_base; |
| 2504 | void *end = p + msg->front.iov_len; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2505 | |
Sage Weil | a1ea787 | 2010-02-23 14:02:44 -0800 | [diff] [blame] | 2506 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 2507 | next_mds = ceph_decode_32(&p); |
| 2508 | fwd_seq = ceph_decode_32(&p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2509 | |
| 2510 | mutex_lock(&mdsc->mutex); |
| 2511 | req = __lookup_request(mdsc, tid); |
| 2512 | if (!req) { |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 2513 | 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] | 2514 | goto out; /* dup reply? */ |
| 2515 | } |
| 2516 | |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 2517 | if (req->r_aborted) { |
| 2518 | dout("forward tid %llu aborted, unregistering\n", tid); |
| 2519 | __unregister_request(mdsc, req); |
| 2520 | } else if (fwd_seq <= req->r_num_fwd) { |
| 2521 | dout("forward tid %llu to mds%d - old seq %d <= %d\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2522 | tid, next_mds, req->r_num_fwd, fwd_seq); |
| 2523 | } else { |
| 2524 | /* resend. forward race not possible; mds would drop */ |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 2525 | dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds); |
| 2526 | BUG_ON(req->r_err); |
| 2527 | BUG_ON(req->r_got_result); |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2528 | req->r_attempts = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2529 | req->r_num_fwd = fwd_seq; |
| 2530 | req->r_resend_mds = next_mds; |
| 2531 | put_request_session(req); |
| 2532 | __do_request(mdsc, req); |
| 2533 | } |
| 2534 | ceph_mdsc_put_request(req); |
| 2535 | out: |
| 2536 | mutex_unlock(&mdsc->mutex); |
| 2537 | return; |
| 2538 | |
| 2539 | bad: |
| 2540 | pr_err("mdsc_handle_forward decode error err=%d\n", err); |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * handle a mds session control message |
| 2545 | */ |
| 2546 | static void handle_session(struct ceph_mds_session *session, |
| 2547 | struct ceph_msg *msg) |
| 2548 | { |
| 2549 | struct ceph_mds_client *mdsc = session->s_mdsc; |
| 2550 | u32 op; |
| 2551 | u64 seq; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2552 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2553 | struct ceph_mds_session_head *h = msg->front.iov_base; |
| 2554 | int wake = 0; |
| 2555 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2556 | /* decode */ |
| 2557 | if (msg->front.iov_len != sizeof(*h)) |
| 2558 | goto bad; |
| 2559 | op = le32_to_cpu(h->op); |
| 2560 | seq = le64_to_cpu(h->seq); |
| 2561 | |
| 2562 | mutex_lock(&mdsc->mutex); |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2563 | if (op == CEPH_SESSION_CLOSE) |
| 2564 | __unregister_session(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2565 | /* FIXME: this ttl calculation is generous */ |
| 2566 | session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose; |
| 2567 | mutex_unlock(&mdsc->mutex); |
| 2568 | |
| 2569 | mutex_lock(&session->s_mutex); |
| 2570 | |
| 2571 | dout("handle_session mds%d %s %p state %s seq %llu\n", |
| 2572 | mds, ceph_session_op_name(op), session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 2573 | ceph_session_state_name(session->s_state), seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2574 | |
| 2575 | if (session->s_state == CEPH_MDS_SESSION_HUNG) { |
| 2576 | session->s_state = CEPH_MDS_SESSION_OPEN; |
| 2577 | pr_info("mds%d came back\n", session->s_mds); |
| 2578 | } |
| 2579 | |
| 2580 | switch (op) { |
| 2581 | case CEPH_SESSION_OPEN: |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2582 | if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) |
| 2583 | pr_info("mds%d reconnect success\n", session->s_mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2584 | session->s_state = CEPH_MDS_SESSION_OPEN; |
| 2585 | renewed_caps(mdsc, session, 0); |
| 2586 | wake = 1; |
| 2587 | if (mdsc->stopping) |
| 2588 | __close_session(mdsc, session); |
| 2589 | break; |
| 2590 | |
| 2591 | case CEPH_SESSION_RENEWCAPS: |
| 2592 | if (session->s_renew_seq == seq) |
| 2593 | renewed_caps(mdsc, session, 1); |
| 2594 | break; |
| 2595 | |
| 2596 | case CEPH_SESSION_CLOSE: |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2597 | if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) |
| 2598 | pr_info("mds%d reconnect denied\n", session->s_mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2599 | remove_session_caps(session); |
Yan, Zheng | 656e438 | 2014-09-11 14:25:18 +0800 | [diff] [blame] | 2600 | wake = 2; /* for good measure */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 2601 | wake_up_all(&mdsc->session_close_wq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2602 | break; |
| 2603 | |
| 2604 | case CEPH_SESSION_STALE: |
| 2605 | pr_info("mds%d caps went stale, renewing\n", |
| 2606 | session->s_mds); |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 2607 | spin_lock(&session->s_gen_ttl_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2608 | session->s_cap_gen++; |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 2609 | session->s_cap_ttl = jiffies - 1; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 2610 | spin_unlock(&session->s_gen_ttl_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2611 | send_renew_caps(mdsc, session); |
| 2612 | break; |
| 2613 | |
| 2614 | case CEPH_SESSION_RECALL_STATE: |
| 2615 | trim_caps(mdsc, session, le32_to_cpu(h->max_caps)); |
| 2616 | break; |
| 2617 | |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 2618 | case CEPH_SESSION_FLUSHMSG: |
| 2619 | send_flushmsg_ack(mdsc, session, seq); |
| 2620 | break; |
| 2621 | |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 2622 | case CEPH_SESSION_FORCE_RO: |
| 2623 | dout("force_session_readonly %p\n", session); |
| 2624 | spin_lock(&session->s_cap_lock); |
| 2625 | session->s_readonly = true; |
| 2626 | spin_unlock(&session->s_cap_lock); |
| 2627 | wake_up_session_caps(session, 0); |
| 2628 | break; |
| 2629 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2630 | default: |
| 2631 | pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds); |
| 2632 | WARN_ON(1); |
| 2633 | } |
| 2634 | |
| 2635 | mutex_unlock(&session->s_mutex); |
| 2636 | if (wake) { |
| 2637 | mutex_lock(&mdsc->mutex); |
| 2638 | __wake_requests(mdsc, &session->s_waiting); |
Yan, Zheng | 656e438 | 2014-09-11 14:25:18 +0800 | [diff] [blame] | 2639 | if (wake == 2) |
| 2640 | kick_requests(mdsc, mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2641 | mutex_unlock(&mdsc->mutex); |
| 2642 | } |
| 2643 | return; |
| 2644 | |
| 2645 | bad: |
| 2646 | pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds, |
| 2647 | (int)msg->front.iov_len); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2648 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2649 | return; |
| 2650 | } |
| 2651 | |
| 2652 | |
| 2653 | /* |
| 2654 | * called under session->mutex. |
| 2655 | */ |
| 2656 | static void replay_unsafe_requests(struct ceph_mds_client *mdsc, |
| 2657 | struct ceph_mds_session *session) |
| 2658 | { |
| 2659 | struct ceph_mds_request *req, *nreq; |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2660 | struct rb_node *p; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2661 | int err; |
| 2662 | |
| 2663 | dout("replay_unsafe_requests mds%d\n", session->s_mds); |
| 2664 | |
| 2665 | mutex_lock(&mdsc->mutex); |
| 2666 | list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) { |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2667 | err = __prepare_send_request(mdsc, req, session->s_mds, true); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2668 | if (!err) { |
| 2669 | ceph_msg_get(req->r_request); |
| 2670 | ceph_con_send(&session->s_con, req->r_request); |
| 2671 | } |
| 2672 | } |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2673 | |
| 2674 | /* |
| 2675 | * also re-send old requests when MDS enters reconnect stage. So that MDS |
| 2676 | * can process completed request in clientreplay stage. |
| 2677 | */ |
| 2678 | p = rb_first(&mdsc->request_tree); |
| 2679 | while (p) { |
| 2680 | req = rb_entry(p, struct ceph_mds_request, r_node); |
| 2681 | p = rb_next(p); |
| 2682 | if (req->r_got_unsafe) |
| 2683 | continue; |
| 2684 | if (req->r_attempts == 0) |
| 2685 | continue; /* only old requests */ |
| 2686 | if (req->r_session && |
| 2687 | req->r_session->s_mds == session->s_mds) { |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2688 | err = __prepare_send_request(mdsc, req, |
| 2689 | session->s_mds, true); |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2690 | if (!err) { |
| 2691 | ceph_msg_get(req->r_request); |
| 2692 | ceph_con_send(&session->s_con, req->r_request); |
| 2693 | } |
| 2694 | } |
| 2695 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2696 | mutex_unlock(&mdsc->mutex); |
| 2697 | } |
| 2698 | |
| 2699 | /* |
| 2700 | * Encode information about a cap for a reconnect with the MDS. |
| 2701 | */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2702 | static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap, |
| 2703 | void *arg) |
| 2704 | { |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2705 | union { |
| 2706 | struct ceph_mds_cap_reconnect v2; |
| 2707 | struct ceph_mds_cap_reconnect_v1 v1; |
| 2708 | } rec; |
| 2709 | size_t reclen; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2710 | struct ceph_inode_info *ci; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2711 | struct ceph_reconnect_state *recon_state = arg; |
| 2712 | struct ceph_pagelist *pagelist = recon_state->pagelist; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2713 | char *path; |
| 2714 | int pathlen, err; |
| 2715 | u64 pathbase; |
| 2716 | struct dentry *dentry; |
| 2717 | |
| 2718 | ci = cap->ci; |
| 2719 | |
| 2720 | dout(" adding %p ino %llx.%llx cap %p %lld %s\n", |
| 2721 | inode, ceph_vinop(inode), cap, cap->cap_id, |
| 2722 | ceph_cap_string(cap->issued)); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2723 | err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode)); |
| 2724 | if (err) |
| 2725 | return err; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2726 | |
| 2727 | dentry = d_find_alias(inode); |
| 2728 | if (dentry) { |
| 2729 | path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0); |
| 2730 | if (IS_ERR(path)) { |
| 2731 | err = PTR_ERR(path); |
Sage Weil | e072f8a | 2010-08-26 09:26:37 -0700 | [diff] [blame] | 2732 | goto out_dput; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2733 | } |
| 2734 | } else { |
| 2735 | path = NULL; |
| 2736 | pathlen = 0; |
| 2737 | } |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2738 | err = ceph_pagelist_encode_string(pagelist, path, pathlen); |
| 2739 | if (err) |
Sage Weil | e072f8a | 2010-08-26 09:26:37 -0700 | [diff] [blame] | 2740 | goto out_free; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2741 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2742 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2743 | cap->seq = 0; /* reset cap seq */ |
| 2744 | cap->issue_seq = 0; /* and issue_seq */ |
Yan, Zheng | 667ca05 | 2013-05-31 16:25:36 +0800 | [diff] [blame] | 2745 | cap->mseq = 0; /* and migrate_seq */ |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 2746 | cap->cap_gen = cap->session->s_cap_gen; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2747 | |
| 2748 | if (recon_state->flock) { |
| 2749 | rec.v2.cap_id = cpu_to_le64(cap->cap_id); |
| 2750 | rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); |
| 2751 | rec.v2.issued = cpu_to_le32(cap->issued); |
| 2752 | rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); |
| 2753 | rec.v2.pathbase = cpu_to_le64(pathbase); |
| 2754 | rec.v2.flock_len = 0; |
| 2755 | reclen = sizeof(rec.v2); |
| 2756 | } else { |
| 2757 | rec.v1.cap_id = cpu_to_le64(cap->cap_id); |
| 2758 | rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); |
| 2759 | rec.v1.issued = cpu_to_le32(cap->issued); |
| 2760 | rec.v1.size = cpu_to_le64(inode->i_size); |
| 2761 | ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime); |
| 2762 | ceph_encode_timespec(&rec.v1.atime, &inode->i_atime); |
| 2763 | rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); |
| 2764 | rec.v1.pathbase = cpu_to_le64(pathbase); |
| 2765 | reclen = sizeof(rec.v1); |
| 2766 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2767 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2768 | |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 2769 | if (recon_state->flock) { |
| 2770 | int num_fcntl_locks, num_flock_locks; |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 2771 | struct ceph_filelock *flocks; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 2772 | |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 2773 | encode_again: |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 2774 | ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks); |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 2775 | flocks = kmalloc((num_fcntl_locks+num_flock_locks) * |
| 2776 | sizeof(struct ceph_filelock), GFP_NOFS); |
| 2777 | if (!flocks) { |
| 2778 | err = -ENOMEM; |
| 2779 | goto out_free; |
| 2780 | } |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 2781 | err = ceph_encode_locks_to_buffer(inode, flocks, |
| 2782 | num_fcntl_locks, |
| 2783 | num_flock_locks); |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 2784 | if (err) { |
| 2785 | kfree(flocks); |
| 2786 | if (err == -ENOSPC) |
| 2787 | goto encode_again; |
| 2788 | goto out_free; |
| 2789 | } |
| 2790 | /* |
| 2791 | * number of encoded locks is stable, so copy to pagelist |
| 2792 | */ |
| 2793 | rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) + |
| 2794 | (num_fcntl_locks+num_flock_locks) * |
| 2795 | sizeof(struct ceph_filelock)); |
| 2796 | err = ceph_pagelist_append(pagelist, &rec, reclen); |
| 2797 | if (!err) |
| 2798 | err = ceph_locks_to_pagelist(flocks, pagelist, |
| 2799 | num_fcntl_locks, |
| 2800 | num_flock_locks); |
| 2801 | kfree(flocks); |
Sage Weil | 3612abb | 2010-09-07 15:59:27 -0700 | [diff] [blame] | 2802 | } else { |
| 2803 | err = ceph_pagelist_append(pagelist, &rec, reclen); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 2804 | } |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 2805 | |
| 2806 | recon_state->nr_caps++; |
Sage Weil | e072f8a | 2010-08-26 09:26:37 -0700 | [diff] [blame] | 2807 | out_free: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2808 | kfree(path); |
Sage Weil | e072f8a | 2010-08-26 09:26:37 -0700 | [diff] [blame] | 2809 | out_dput: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2810 | dput(dentry); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2811 | return err; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | |
| 2815 | /* |
| 2816 | * If an MDS fails and recovers, clients need to reconnect in order to |
| 2817 | * reestablish shared state. This includes all caps issued through |
| 2818 | * this session _and_ the snap_realm hierarchy. Because it's not |
| 2819 | * clear which snap realms the mds cares about, we send everything we |
| 2820 | * know about.. that ensures we'll then get any new info the |
| 2821 | * recovering MDS might have. |
| 2822 | * |
| 2823 | * This is a relatively heavyweight operation, but it's rare. |
| 2824 | * |
| 2825 | * called with mdsc->mutex held. |
| 2826 | */ |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 2827 | static void send_mds_reconnect(struct ceph_mds_client *mdsc, |
| 2828 | struct ceph_mds_session *session) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2829 | { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2830 | struct ceph_msg *reply; |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 2831 | struct rb_node *p; |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 2832 | int mds = session->s_mds; |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 2833 | int err = -ENOMEM; |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 2834 | int s_nr_caps; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2835 | struct ceph_pagelist *pagelist; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2836 | struct ceph_reconnect_state recon_state; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2837 | |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 2838 | pr_info("mds%d reconnect start\n", mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2839 | |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2840 | pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS); |
| 2841 | if (!pagelist) |
| 2842 | goto fail_nopagelist; |
| 2843 | ceph_pagelist_init(pagelist); |
| 2844 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2845 | reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2846 | if (!reply) |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2847 | goto fail_nomsg; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2848 | |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 2849 | mutex_lock(&session->s_mutex); |
| 2850 | session->s_state = CEPH_MDS_SESSION_RECONNECTING; |
| 2851 | session->s_seq = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2852 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2853 | dout("session %p state %s\n", session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 2854 | ceph_session_state_name(session->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2855 | |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 2856 | spin_lock(&session->s_gen_ttl_lock); |
| 2857 | session->s_cap_gen++; |
| 2858 | spin_unlock(&session->s_gen_ttl_lock); |
| 2859 | |
| 2860 | spin_lock(&session->s_cap_lock); |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 2861 | /* don't know if session is readonly */ |
| 2862 | session->s_readonly = 0; |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 2863 | /* |
| 2864 | * notify __ceph_remove_cap() that we are composing cap reconnect. |
| 2865 | * If a cap get released before being added to the cap reconnect, |
| 2866 | * __ceph_remove_cap() should skip queuing cap release. |
| 2867 | */ |
| 2868 | session->s_cap_reconnect = 1; |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 2869 | /* drop old cap expires; we're about to reestablish that state */ |
| 2870 | discard_cap_releases(mdsc, session); |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 2871 | spin_unlock(&session->s_cap_lock); |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 2872 | |
Yan, Zheng | 5d23371 | 2014-09-10 16:56:23 +0800 | [diff] [blame] | 2873 | /* trim unused caps to reduce MDS's cache rejoin time */ |
| 2874 | shrink_dcache_parent(mdsc->fsc->sb->s_root); |
| 2875 | |
| 2876 | ceph_con_close(&session->s_con); |
| 2877 | ceph_con_open(&session->s_con, |
| 2878 | CEPH_ENTITY_TYPE_MDS, mds, |
| 2879 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); |
| 2880 | |
| 2881 | /* replay unsafe requests */ |
| 2882 | replay_unsafe_requests(mdsc, session); |
| 2883 | |
| 2884 | down_read(&mdsc->snap_rwsem); |
| 2885 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2886 | /* traverse this session's caps */ |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 2887 | s_nr_caps = session->s_nr_caps; |
| 2888 | err = ceph_pagelist_encode_32(pagelist, s_nr_caps); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2889 | if (err) |
| 2890 | goto fail; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2891 | |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 2892 | recon_state.nr_caps = 0; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2893 | recon_state.pagelist = pagelist; |
| 2894 | recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK; |
| 2895 | err = iterate_session_caps(session, encode_caps_cb, &recon_state); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2896 | if (err < 0) |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 2897 | goto fail; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2898 | |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 2899 | spin_lock(&session->s_cap_lock); |
| 2900 | session->s_cap_reconnect = 0; |
| 2901 | spin_unlock(&session->s_cap_lock); |
| 2902 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2903 | /* |
| 2904 | * snaprealms. we provide mds with the ino, seq (version), and |
| 2905 | * parent for all of our realms. If the mds has any newer info, |
| 2906 | * it will tell us. |
| 2907 | */ |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 2908 | for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) { |
| 2909 | struct ceph_snap_realm *realm = |
| 2910 | rb_entry(p, struct ceph_snap_realm, node); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2911 | struct ceph_mds_snaprealm_reconnect sr_rec; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2912 | |
| 2913 | dout(" adding snap realm %llx seq %lld parent %llx\n", |
| 2914 | realm->ino, realm->seq, realm->parent_ino); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2915 | sr_rec.ino = cpu_to_le64(realm->ino); |
| 2916 | sr_rec.seq = cpu_to_le64(realm->seq); |
| 2917 | sr_rec.parent = cpu_to_le64(realm->parent_ino); |
| 2918 | err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec)); |
| 2919 | if (err) |
| 2920 | goto fail; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2921 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2922 | |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2923 | if (recon_state.flock) |
| 2924 | reply->hdr.version = cpu_to_le16(2); |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 2925 | |
| 2926 | /* raced with cap release? */ |
| 2927 | if (s_nr_caps != recon_state.nr_caps) { |
| 2928 | struct page *page = list_first_entry(&pagelist->head, |
| 2929 | struct page, lru); |
| 2930 | __le32 *addr = kmap_atomic(page); |
| 2931 | *addr = cpu_to_le32(recon_state.nr_caps); |
| 2932 | kunmap_atomic(addr); |
Alex Elder | ebf18f4 | 2013-03-04 22:29:57 -0600 | [diff] [blame] | 2933 | } |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 2934 | |
| 2935 | reply->hdr.data_len = cpu_to_le32(pagelist->length); |
| 2936 | ceph_msg_data_add_pagelist(reply, pagelist); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2937 | ceph_con_send(&session->s_con, reply); |
| 2938 | |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 2939 | mutex_unlock(&session->s_mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2940 | |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 2941 | mutex_lock(&mdsc->mutex); |
| 2942 | __wake_requests(mdsc, &session->s_waiting); |
| 2943 | mutex_unlock(&mdsc->mutex); |
| 2944 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2945 | up_read(&mdsc->snap_rwsem); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2946 | return; |
| 2947 | |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2948 | fail: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2949 | ceph_msg_put(reply); |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 2950 | up_read(&mdsc->snap_rwsem); |
| 2951 | mutex_unlock(&session->s_mutex); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2952 | fail_nomsg: |
| 2953 | ceph_pagelist_release(pagelist); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 2954 | fail_nopagelist: |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 2955 | pr_err("error %d preparing reconnect for mds%d\n", err, mds); |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 2956 | return; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | |
| 2960 | /* |
| 2961 | * compare old and new mdsmaps, kicking requests |
| 2962 | * and closing out old connections as necessary |
| 2963 | * |
| 2964 | * called under mdsc->mutex. |
| 2965 | */ |
| 2966 | static void check_new_map(struct ceph_mds_client *mdsc, |
| 2967 | struct ceph_mdsmap *newmap, |
| 2968 | struct ceph_mdsmap *oldmap) |
| 2969 | { |
| 2970 | int i; |
| 2971 | int oldstate, newstate; |
| 2972 | struct ceph_mds_session *s; |
| 2973 | |
| 2974 | dout("check_new_map new %u old %u\n", |
| 2975 | newmap->m_epoch, oldmap->m_epoch); |
| 2976 | |
| 2977 | for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) { |
| 2978 | if (mdsc->sessions[i] == NULL) |
| 2979 | continue; |
| 2980 | s = mdsc->sessions[i]; |
| 2981 | oldstate = ceph_mdsmap_get_state(oldmap, i); |
| 2982 | newstate = ceph_mdsmap_get_state(newmap, i); |
| 2983 | |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 2984 | 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] | 2985 | i, ceph_mds_state_name(oldstate), |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 2986 | ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2987 | ceph_mds_state_name(newstate), |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 2988 | ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 2989 | ceph_session_state_name(s->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2990 | |
Yan, Zheng | 3e8f43a | 2012-09-20 17:42:25 +0800 | [diff] [blame] | 2991 | if (i >= newmap->m_max_mds || |
| 2992 | memcmp(ceph_mdsmap_get_addr(oldmap, i), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2993 | ceph_mdsmap_get_addr(newmap, i), |
| 2994 | sizeof(struct ceph_entity_addr))) { |
| 2995 | if (s->s_state == CEPH_MDS_SESSION_OPENING) { |
| 2996 | /* the session never opened, just close it |
| 2997 | * out now */ |
| 2998 | __wake_requests(mdsc, &s->s_waiting); |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2999 | __unregister_session(mdsc, s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3000 | } else { |
| 3001 | /* just close it */ |
| 3002 | mutex_unlock(&mdsc->mutex); |
| 3003 | mutex_lock(&s->s_mutex); |
| 3004 | mutex_lock(&mdsc->mutex); |
| 3005 | ceph_con_close(&s->s_con); |
| 3006 | mutex_unlock(&s->s_mutex); |
| 3007 | s->s_state = CEPH_MDS_SESSION_RESTARTING; |
| 3008 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3009 | } else if (oldstate == newstate) { |
| 3010 | continue; /* nothing new with this mds */ |
| 3011 | } |
| 3012 | |
| 3013 | /* |
| 3014 | * send reconnect? |
| 3015 | */ |
| 3016 | if (s->s_state == CEPH_MDS_SESSION_RESTARTING && |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3017 | newstate >= CEPH_MDS_STATE_RECONNECT) { |
| 3018 | mutex_unlock(&mdsc->mutex); |
| 3019 | send_mds_reconnect(mdsc, s); |
| 3020 | mutex_lock(&mdsc->mutex); |
| 3021 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3022 | |
| 3023 | /* |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 3024 | * kick request on any mds that has gone active. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3025 | */ |
| 3026 | if (oldstate < CEPH_MDS_STATE_ACTIVE && |
| 3027 | newstate >= CEPH_MDS_STATE_ACTIVE) { |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 3028 | if (oldstate != CEPH_MDS_STATE_CREATING && |
| 3029 | oldstate != CEPH_MDS_STATE_STARTING) |
| 3030 | pr_info("mds%d recovery completed\n", s->s_mds); |
| 3031 | kick_requests(mdsc, i); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3032 | ceph_kick_flushing_caps(mdsc, s); |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 3033 | wake_up_session_caps(s, 1); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3034 | } |
| 3035 | } |
Sage Weil | cb170a2 | 2010-06-21 13:38:35 -0700 | [diff] [blame] | 3036 | |
| 3037 | for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) { |
| 3038 | s = mdsc->sessions[i]; |
| 3039 | if (!s) |
| 3040 | continue; |
| 3041 | if (!ceph_mdsmap_is_laggy(newmap, i)) |
| 3042 | continue; |
| 3043 | if (s->s_state == CEPH_MDS_SESSION_OPEN || |
| 3044 | s->s_state == CEPH_MDS_SESSION_HUNG || |
| 3045 | s->s_state == CEPH_MDS_SESSION_CLOSING) { |
| 3046 | dout(" connecting to export targets of laggy mds%d\n", |
| 3047 | i); |
| 3048 | __open_export_target_sessions(mdsc, s); |
| 3049 | } |
| 3050 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | |
| 3054 | |
| 3055 | /* |
| 3056 | * leases |
| 3057 | */ |
| 3058 | |
| 3059 | /* |
| 3060 | * caller must hold session s_mutex, dentry->d_lock |
| 3061 | */ |
| 3062 | void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry) |
| 3063 | { |
| 3064 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 3065 | |
| 3066 | ceph_put_mds_session(di->lease_session); |
| 3067 | di->lease_session = NULL; |
| 3068 | } |
| 3069 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3070 | static void handle_lease(struct ceph_mds_client *mdsc, |
| 3071 | struct ceph_mds_session *session, |
| 3072 | struct ceph_msg *msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3073 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3074 | struct super_block *sb = mdsc->fsc->sb; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3075 | struct inode *inode; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3076 | struct dentry *parent, *dentry; |
| 3077 | struct ceph_dentry_info *di; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3078 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3079 | struct ceph_mds_lease *h = msg->front.iov_base; |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3080 | u32 seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3081 | struct ceph_vino vino; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3082 | struct qstr dname; |
| 3083 | int release = 0; |
| 3084 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3085 | dout("handle_lease from mds%d\n", mds); |
| 3086 | |
| 3087 | /* decode */ |
| 3088 | if (msg->front.iov_len < sizeof(*h) + sizeof(u32)) |
| 3089 | goto bad; |
| 3090 | vino.ino = le64_to_cpu(h->ino); |
| 3091 | vino.snap = CEPH_NOSNAP; |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3092 | seq = le32_to_cpu(h->seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3093 | dname.name = (void *)h + sizeof(*h) + sizeof(u32); |
| 3094 | dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32); |
| 3095 | if (dname.len != get_unaligned_le32(h+1)) |
| 3096 | goto bad; |
| 3097 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3098 | /* lookup inode */ |
| 3099 | inode = ceph_find_inode(sb, vino); |
Sage Weil | 2f90b85 | 2011-07-26 11:28:25 -0700 | [diff] [blame] | 3100 | dout("handle_lease %s, ino %llx %p %.*s\n", |
| 3101 | ceph_lease_op_name(h->action), vino.ino, inode, |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3102 | dname.len, dname.name); |
Yan, Zheng | 6cd3bca | 2014-09-17 07:45:12 +0800 | [diff] [blame] | 3103 | |
| 3104 | mutex_lock(&session->s_mutex); |
| 3105 | session->s_seq++; |
| 3106 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3107 | if (inode == NULL) { |
| 3108 | dout("handle_lease no inode %llx\n", vino.ino); |
| 3109 | goto release; |
| 3110 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3111 | |
| 3112 | /* dentry */ |
| 3113 | parent = d_find_alias(inode); |
| 3114 | if (!parent) { |
| 3115 | dout("no parent dentry on inode %p\n", inode); |
| 3116 | WARN_ON(1); |
| 3117 | goto release; /* hrm... */ |
| 3118 | } |
| 3119 | dname.hash = full_name_hash(dname.name, dname.len); |
| 3120 | dentry = d_lookup(parent, &dname); |
| 3121 | dput(parent); |
| 3122 | if (!dentry) |
| 3123 | goto release; |
| 3124 | |
| 3125 | spin_lock(&dentry->d_lock); |
| 3126 | di = ceph_dentry(dentry); |
| 3127 | switch (h->action) { |
| 3128 | case CEPH_MDS_LEASE_REVOKE: |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 3129 | if (di->lease_session == session) { |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3130 | if (ceph_seq_cmp(di->lease_seq, seq) > 0) |
| 3131 | h->seq = cpu_to_le32(di->lease_seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3132 | __ceph_mdsc_drop_dentry_lease(dentry); |
| 3133 | } |
| 3134 | release = 1; |
| 3135 | break; |
| 3136 | |
| 3137 | case CEPH_MDS_LEASE_RENEW: |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 3138 | if (di->lease_session == session && |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3139 | di->lease_gen == session->s_cap_gen && |
| 3140 | di->lease_renew_from && |
| 3141 | di->lease_renew_after == 0) { |
| 3142 | unsigned long duration = |
Nicholas Mc Guire | 3563dbdd | 2015-02-06 06:52:17 -0500 | [diff] [blame] | 3143 | msecs_to_jiffies(le32_to_cpu(h->duration_ms)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3144 | |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3145 | di->lease_seq = seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3146 | dentry->d_time = di->lease_renew_from + duration; |
| 3147 | di->lease_renew_after = di->lease_renew_from + |
| 3148 | (duration >> 1); |
| 3149 | di->lease_renew_from = 0; |
| 3150 | } |
| 3151 | break; |
| 3152 | } |
| 3153 | spin_unlock(&dentry->d_lock); |
| 3154 | dput(dentry); |
| 3155 | |
| 3156 | if (!release) |
| 3157 | goto out; |
| 3158 | |
| 3159 | release: |
| 3160 | /* let's just reuse the same message */ |
| 3161 | h->action = CEPH_MDS_LEASE_REVOKE_ACK; |
| 3162 | ceph_msg_get(msg); |
| 3163 | ceph_con_send(&session->s_con, msg); |
| 3164 | |
| 3165 | out: |
| 3166 | iput(inode); |
| 3167 | mutex_unlock(&session->s_mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3168 | return; |
| 3169 | |
| 3170 | bad: |
| 3171 | pr_err("corrupt lease message\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 3172 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session, |
| 3176 | struct inode *inode, |
| 3177 | struct dentry *dentry, char action, |
| 3178 | u32 seq) |
| 3179 | { |
| 3180 | struct ceph_msg *msg; |
| 3181 | struct ceph_mds_lease *lease; |
| 3182 | int len = sizeof(*lease) + sizeof(u32); |
| 3183 | int dnamelen = 0; |
| 3184 | |
| 3185 | dout("lease_send_msg inode %p dentry %p %s to mds%d\n", |
| 3186 | inode, dentry, ceph_lease_op_name(action), session->s_mds); |
| 3187 | dnamelen = dentry->d_name.len; |
| 3188 | len += dnamelen; |
| 3189 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 3190 | msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 3191 | if (!msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3192 | return; |
| 3193 | lease = msg->front.iov_base; |
| 3194 | lease->action = action; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3195 | lease->ino = cpu_to_le64(ceph_vino(inode).ino); |
| 3196 | lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap); |
| 3197 | lease->seq = cpu_to_le32(seq); |
| 3198 | put_unaligned_le32(dnamelen, lease + 1); |
| 3199 | memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen); |
| 3200 | |
| 3201 | /* |
| 3202 | * if this is a preemptive lease RELEASE, no need to |
| 3203 | * flush request stream, since the actual request will |
| 3204 | * soon follow. |
| 3205 | */ |
| 3206 | msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE); |
| 3207 | |
| 3208 | ceph_con_send(&session->s_con, msg); |
| 3209 | } |
| 3210 | |
| 3211 | /* |
| 3212 | * Preemptively release a lease we expect to invalidate anyway. |
| 3213 | * Pass @inode always, @dentry is optional. |
| 3214 | */ |
| 3215 | void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode, |
Sage Weil | 2f90b85 | 2011-07-26 11:28:25 -0700 | [diff] [blame] | 3216 | struct dentry *dentry) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3217 | { |
| 3218 | struct ceph_dentry_info *di; |
| 3219 | struct ceph_mds_session *session; |
| 3220 | u32 seq; |
| 3221 | |
| 3222 | BUG_ON(inode == NULL); |
| 3223 | BUG_ON(dentry == NULL); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3224 | |
| 3225 | /* is dentry lease valid? */ |
| 3226 | spin_lock(&dentry->d_lock); |
| 3227 | di = ceph_dentry(dentry); |
| 3228 | if (!di || !di->lease_session || |
| 3229 | di->lease_session->s_mds < 0 || |
| 3230 | di->lease_gen != di->lease_session->s_cap_gen || |
| 3231 | !time_before(jiffies, dentry->d_time)) { |
| 3232 | dout("lease_release inode %p dentry %p -- " |
Sage Weil | 2f90b85 | 2011-07-26 11:28:25 -0700 | [diff] [blame] | 3233 | "no lease\n", |
| 3234 | inode, dentry); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3235 | spin_unlock(&dentry->d_lock); |
| 3236 | return; |
| 3237 | } |
| 3238 | |
| 3239 | /* we do have a lease on this dentry; note mds and seq */ |
| 3240 | session = ceph_get_mds_session(di->lease_session); |
| 3241 | seq = di->lease_seq; |
| 3242 | __ceph_mdsc_drop_dentry_lease(dentry); |
| 3243 | spin_unlock(&dentry->d_lock); |
| 3244 | |
Sage Weil | 2f90b85 | 2011-07-26 11:28:25 -0700 | [diff] [blame] | 3245 | dout("lease_release inode %p dentry %p to mds%d\n", |
| 3246 | inode, dentry, session->s_mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3247 | ceph_mdsc_lease_send_msg(session, inode, dentry, |
| 3248 | CEPH_MDS_LEASE_RELEASE, seq); |
| 3249 | ceph_put_mds_session(session); |
| 3250 | } |
| 3251 | |
| 3252 | /* |
| 3253 | * drop all leases (and dentry refs) in preparation for umount |
| 3254 | */ |
| 3255 | static void drop_leases(struct ceph_mds_client *mdsc) |
| 3256 | { |
| 3257 | int i; |
| 3258 | |
| 3259 | dout("drop_leases\n"); |
| 3260 | mutex_lock(&mdsc->mutex); |
| 3261 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 3262 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); |
| 3263 | if (!s) |
| 3264 | continue; |
| 3265 | mutex_unlock(&mdsc->mutex); |
| 3266 | mutex_lock(&s->s_mutex); |
| 3267 | mutex_unlock(&s->s_mutex); |
| 3268 | ceph_put_mds_session(s); |
| 3269 | mutex_lock(&mdsc->mutex); |
| 3270 | } |
| 3271 | mutex_unlock(&mdsc->mutex); |
| 3272 | } |
| 3273 | |
| 3274 | |
| 3275 | |
| 3276 | /* |
| 3277 | * delayed work -- periodically trim expired leases, renew caps with mds |
| 3278 | */ |
| 3279 | static void schedule_delayed(struct ceph_mds_client *mdsc) |
| 3280 | { |
| 3281 | int delay = 5; |
| 3282 | unsigned hz = round_jiffies_relative(HZ * delay); |
| 3283 | schedule_delayed_work(&mdsc->delayed_work, hz); |
| 3284 | } |
| 3285 | |
| 3286 | static void delayed_work(struct work_struct *work) |
| 3287 | { |
| 3288 | int i; |
| 3289 | struct ceph_mds_client *mdsc = |
| 3290 | container_of(work, struct ceph_mds_client, delayed_work.work); |
| 3291 | int renew_interval; |
| 3292 | int renew_caps; |
| 3293 | |
| 3294 | dout("mdsc delayed_work\n"); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3295 | ceph_check_delayed_caps(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3296 | |
| 3297 | mutex_lock(&mdsc->mutex); |
| 3298 | renew_interval = mdsc->mdsmap->m_session_timeout >> 2; |
| 3299 | renew_caps = time_after_eq(jiffies, HZ*renew_interval + |
| 3300 | mdsc->last_renew_caps); |
| 3301 | if (renew_caps) |
| 3302 | mdsc->last_renew_caps = jiffies; |
| 3303 | |
| 3304 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 3305 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); |
| 3306 | if (s == NULL) |
| 3307 | continue; |
| 3308 | if (s->s_state == CEPH_MDS_SESSION_CLOSING) { |
| 3309 | dout("resending session close request for mds%d\n", |
| 3310 | s->s_mds); |
| 3311 | request_close_session(mdsc, s); |
| 3312 | ceph_put_mds_session(s); |
| 3313 | continue; |
| 3314 | } |
| 3315 | if (s->s_ttl && time_after(jiffies, s->s_ttl)) { |
| 3316 | if (s->s_state == CEPH_MDS_SESSION_OPEN) { |
| 3317 | s->s_state = CEPH_MDS_SESSION_HUNG; |
| 3318 | pr_info("mds%d hung\n", s->s_mds); |
| 3319 | } |
| 3320 | } |
| 3321 | if (s->s_state < CEPH_MDS_SESSION_OPEN) { |
| 3322 | /* this mds is failed or recovering, just wait */ |
| 3323 | ceph_put_mds_session(s); |
| 3324 | continue; |
| 3325 | } |
| 3326 | mutex_unlock(&mdsc->mutex); |
| 3327 | |
| 3328 | mutex_lock(&s->s_mutex); |
| 3329 | if (renew_caps) |
| 3330 | send_renew_caps(mdsc, s); |
| 3331 | else |
| 3332 | ceph_con_keepalive(&s->s_con); |
Sage Weil | ee6b272 | 2010-06-10 12:55:52 -0700 | [diff] [blame] | 3333 | ceph_add_cap_releases(mdsc, s); |
Sage Weil | aab53dd | 2010-03-17 16:30:21 -0700 | [diff] [blame] | 3334 | if (s->s_state == CEPH_MDS_SESSION_OPEN || |
| 3335 | s->s_state == CEPH_MDS_SESSION_HUNG) |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 3336 | ceph_send_cap_releases(mdsc, s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3337 | mutex_unlock(&s->s_mutex); |
| 3338 | ceph_put_mds_session(s); |
| 3339 | |
| 3340 | mutex_lock(&mdsc->mutex); |
| 3341 | } |
| 3342 | mutex_unlock(&mdsc->mutex); |
| 3343 | |
| 3344 | schedule_delayed(mdsc); |
| 3345 | } |
| 3346 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3347 | int ceph_mdsc_init(struct ceph_fs_client *fsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3348 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3349 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3350 | struct ceph_mds_client *mdsc; |
| 3351 | |
| 3352 | mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS); |
| 3353 | if (!mdsc) |
| 3354 | return -ENOMEM; |
| 3355 | mdsc->fsc = fsc; |
| 3356 | fsc->mdsc = mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3357 | mutex_init(&mdsc->mutex); |
| 3358 | mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS); |
majianpeng | fb3101b | 2013-06-25 14:48:19 +0800 | [diff] [blame] | 3359 | if (mdsc->mdsmap == NULL) { |
| 3360 | kfree(mdsc); |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 3361 | return -ENOMEM; |
majianpeng | fb3101b | 2013-06-25 14:48:19 +0800 | [diff] [blame] | 3362 | } |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 3363 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3364 | init_completion(&mdsc->safe_umount_waiters); |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3365 | init_waitqueue_head(&mdsc->session_close_wq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3366 | INIT_LIST_HEAD(&mdsc->waiting_for_map); |
| 3367 | mdsc->sessions = NULL; |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 3368 | atomic_set(&mdsc->num_sessions, 0); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3369 | mdsc->max_sessions = 0; |
| 3370 | mdsc->stopping = 0; |
| 3371 | init_rwsem(&mdsc->snap_rwsem); |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 3372 | mdsc->snap_realms = RB_ROOT; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3373 | INIT_LIST_HEAD(&mdsc->snap_empty); |
| 3374 | spin_lock_init(&mdsc->snap_empty_lock); |
| 3375 | mdsc->last_tid = 0; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3376 | mdsc->request_tree = RB_ROOT; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3377 | INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); |
| 3378 | mdsc->last_renew_caps = jiffies; |
| 3379 | INIT_LIST_HEAD(&mdsc->cap_delay_list); |
| 3380 | spin_lock_init(&mdsc->cap_delay_lock); |
| 3381 | INIT_LIST_HEAD(&mdsc->snap_flush_list); |
| 3382 | spin_lock_init(&mdsc->snap_flush_lock); |
| 3383 | mdsc->cap_flush_seq = 0; |
| 3384 | INIT_LIST_HEAD(&mdsc->cap_dirty); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 3385 | INIT_LIST_HEAD(&mdsc->cap_dirty_migrating); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3386 | mdsc->num_cap_flushing = 0; |
| 3387 | spin_lock_init(&mdsc->cap_dirty_lock); |
| 3388 | init_waitqueue_head(&mdsc->cap_flushing_wq); |
| 3389 | spin_lock_init(&mdsc->dentry_lru_lock); |
| 3390 | INIT_LIST_HEAD(&mdsc->dentry_lru); |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 3391 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 3392 | ceph_caps_init(mdsc); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3393 | ceph_adjust_min_caps(mdsc, fsc->min_caps); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 3394 | |
Sage Weil | 5f44f14 | 2009-11-18 14:52:18 -0800 | [diff] [blame] | 3395 | return 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | /* |
| 3399 | * Wait for safe replies on open mds requests. If we time out, drop |
| 3400 | * all requests from the tree to avoid dangling dentry refs. |
| 3401 | */ |
| 3402 | static void wait_requests(struct ceph_mds_client *mdsc) |
| 3403 | { |
| 3404 | struct ceph_mds_request *req; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3405 | struct ceph_fs_client *fsc = mdsc->fsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3406 | |
| 3407 | mutex_lock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3408 | if (__get_oldest_req(mdsc)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3409 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3410 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3411 | dout("wait_requests waiting for requests\n"); |
| 3412 | wait_for_completion_timeout(&mdsc->safe_umount_waiters, |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3413 | fsc->client->options->mount_timeout * HZ); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3414 | |
| 3415 | /* tear down remaining requests */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3416 | mutex_lock(&mdsc->mutex); |
| 3417 | while ((req = __get_oldest_req(mdsc))) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3418 | dout("wait_requests timed out on tid %llu\n", |
| 3419 | req->r_tid); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3420 | __unregister_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | mutex_unlock(&mdsc->mutex); |
| 3424 | dout("wait_requests done\n"); |
| 3425 | } |
| 3426 | |
| 3427 | /* |
| 3428 | * called before mount is ro, and before dentries are torn down. |
| 3429 | * (hmm, does this still race with new lookups?) |
| 3430 | */ |
| 3431 | void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc) |
| 3432 | { |
| 3433 | dout("pre_umount\n"); |
| 3434 | mdsc->stopping = 1; |
| 3435 | |
| 3436 | drop_leases(mdsc); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3437 | ceph_flush_dirty_caps(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3438 | wait_requests(mdsc); |
Sage Weil | 17c688c | 2010-06-21 16:12:26 -0700 | [diff] [blame] | 3439 | |
| 3440 | /* |
| 3441 | * wait for reply handlers to drop their request refs and |
| 3442 | * their inode/dcache refs |
| 3443 | */ |
| 3444 | ceph_msgr_flush(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | /* |
| 3448 | * wait for all write mds requests to flush. |
| 3449 | */ |
| 3450 | static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid) |
| 3451 | { |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3452 | struct ceph_mds_request *req = NULL, *nextreq; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3453 | struct rb_node *n; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3454 | |
| 3455 | mutex_lock(&mdsc->mutex); |
| 3456 | dout("wait_unsafe_requests want %lld\n", want_tid); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3457 | restart: |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3458 | req = __get_oldest_req(mdsc); |
| 3459 | while (req && req->r_tid <= want_tid) { |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3460 | /* find next request */ |
| 3461 | n = rb_next(&req->r_node); |
| 3462 | if (n) |
| 3463 | nextreq = rb_entry(n, struct ceph_mds_request, r_node); |
| 3464 | else |
| 3465 | nextreq = NULL; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3466 | if ((req->r_op & CEPH_MDS_OP_WRITE)) { |
| 3467 | /* write op */ |
| 3468 | ceph_mdsc_get_request(req); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3469 | if (nextreq) |
| 3470 | ceph_mdsc_get_request(nextreq); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3471 | mutex_unlock(&mdsc->mutex); |
| 3472 | dout("wait_unsafe_requests wait on %llu (want %llu)\n", |
| 3473 | req->r_tid, want_tid); |
| 3474 | wait_for_completion(&req->r_safe_completion); |
| 3475 | mutex_lock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3476 | ceph_mdsc_put_request(req); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3477 | if (!nextreq) |
| 3478 | break; /* next dne before, so we're done! */ |
| 3479 | if (RB_EMPTY_NODE(&nextreq->r_node)) { |
| 3480 | /* next request was removed from tree */ |
| 3481 | ceph_mdsc_put_request(nextreq); |
| 3482 | goto restart; |
| 3483 | } |
| 3484 | ceph_mdsc_put_request(nextreq); /* won't go away */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3485 | } |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3486 | req = nextreq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3487 | } |
| 3488 | mutex_unlock(&mdsc->mutex); |
| 3489 | dout("wait_unsafe_requests done\n"); |
| 3490 | } |
| 3491 | |
| 3492 | void ceph_mdsc_sync(struct ceph_mds_client *mdsc) |
| 3493 | { |
| 3494 | u64 want_tid, want_flush; |
| 3495 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3496 | if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN) |
Sage Weil | 56b7cf9 | 2010-05-03 15:22:00 -0700 | [diff] [blame] | 3497 | return; |
| 3498 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3499 | dout("sync\n"); |
| 3500 | mutex_lock(&mdsc->mutex); |
| 3501 | want_tid = mdsc->last_tid; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3502 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3503 | |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3504 | ceph_flush_dirty_caps(mdsc); |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 3505 | spin_lock(&mdsc->cap_dirty_lock); |
| 3506 | want_flush = mdsc->cap_flush_seq; |
| 3507 | spin_unlock(&mdsc->cap_dirty_lock); |
| 3508 | |
| 3509 | dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3510 | |
| 3511 | wait_unsafe_requests(mdsc, want_tid); |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 3512 | wait_caps_flush(mdsc, want_flush); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3513 | } |
| 3514 | |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3515 | /* |
| 3516 | * true if all sessions are closed, or we force unmount |
| 3517 | */ |
H Hartley Sweeten | 7fd7d10 | 2011-09-23 13:22:11 -0700 | [diff] [blame] | 3518 | static bool done_closing_sessions(struct ceph_mds_client *mdsc) |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3519 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3520 | if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN) |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3521 | return true; |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 3522 | return atomic_read(&mdsc->num_sessions) == 0; |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3523 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3524 | |
| 3525 | /* |
| 3526 | * called after sb is ro. |
| 3527 | */ |
| 3528 | void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc) |
| 3529 | { |
| 3530 | struct ceph_mds_session *session; |
| 3531 | int i; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3532 | struct ceph_fs_client *fsc = mdsc->fsc; |
| 3533 | unsigned long timeout = fsc->client->options->mount_timeout * HZ; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3534 | |
| 3535 | dout("close_sessions\n"); |
| 3536 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3537 | /* close sessions */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3538 | mutex_lock(&mdsc->mutex); |
| 3539 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 3540 | session = __ceph_lookup_mds_session(mdsc, i); |
| 3541 | if (!session) |
| 3542 | continue; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3543 | mutex_unlock(&mdsc->mutex); |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3544 | mutex_lock(&session->s_mutex); |
| 3545 | __close_session(mdsc, session); |
| 3546 | mutex_unlock(&session->s_mutex); |
| 3547 | ceph_put_mds_session(session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3548 | mutex_lock(&mdsc->mutex); |
| 3549 | } |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3550 | mutex_unlock(&mdsc->mutex); |
| 3551 | |
| 3552 | dout("waiting for sessions to close\n"); |
| 3553 | wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc), |
| 3554 | timeout); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3555 | |
| 3556 | /* tear down remaining sessions */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3557 | mutex_lock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3558 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 3559 | if (mdsc->sessions[i]) { |
| 3560 | session = get_session(mdsc->sessions[i]); |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3561 | __unregister_session(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3562 | mutex_unlock(&mdsc->mutex); |
| 3563 | mutex_lock(&session->s_mutex); |
| 3564 | remove_session_caps(session); |
| 3565 | mutex_unlock(&session->s_mutex); |
| 3566 | ceph_put_mds_session(session); |
| 3567 | mutex_lock(&mdsc->mutex); |
| 3568 | } |
| 3569 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3570 | WARN_ON(!list_empty(&mdsc->cap_delay_list)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3571 | mutex_unlock(&mdsc->mutex); |
| 3572 | |
| 3573 | ceph_cleanup_empty_realms(mdsc); |
| 3574 | |
| 3575 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ |
| 3576 | |
| 3577 | dout("stopped\n"); |
| 3578 | } |
| 3579 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3580 | static void ceph_mdsc_stop(struct ceph_mds_client *mdsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3581 | { |
| 3582 | dout("stop\n"); |
| 3583 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ |
| 3584 | if (mdsc->mdsmap) |
| 3585 | ceph_mdsmap_destroy(mdsc->mdsmap); |
| 3586 | kfree(mdsc->sessions); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 3587 | ceph_caps_finalize(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3588 | } |
| 3589 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3590 | void ceph_mdsc_destroy(struct ceph_fs_client *fsc) |
| 3591 | { |
| 3592 | struct ceph_mds_client *mdsc = fsc->mdsc; |
| 3593 | |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 3594 | dout("mdsc_destroy %p\n", mdsc); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3595 | ceph_mdsc_stop(mdsc); |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 3596 | |
| 3597 | /* flush out any connection work with references to us */ |
| 3598 | ceph_msgr_flush(); |
| 3599 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3600 | fsc->mdsc = NULL; |
| 3601 | kfree(mdsc); |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 3602 | dout("mdsc_destroy %p done\n", mdsc); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3603 | } |
| 3604 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3605 | |
| 3606 | /* |
| 3607 | * handle mds map update. |
| 3608 | */ |
| 3609 | void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg) |
| 3610 | { |
| 3611 | u32 epoch; |
| 3612 | u32 maplen; |
| 3613 | void *p = msg->front.iov_base; |
| 3614 | void *end = p + msg->front.iov_len; |
| 3615 | struct ceph_mdsmap *newmap, *oldmap; |
| 3616 | struct ceph_fsid fsid; |
| 3617 | int err = -EINVAL; |
| 3618 | |
| 3619 | ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad); |
| 3620 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3621 | if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0) |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 3622 | return; |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 3623 | epoch = ceph_decode_32(&p); |
| 3624 | maplen = ceph_decode_32(&p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3625 | dout("handle_map epoch %u len %d\n", epoch, (int)maplen); |
| 3626 | |
| 3627 | /* do we need it? */ |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3628 | ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3629 | mutex_lock(&mdsc->mutex); |
| 3630 | if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) { |
| 3631 | dout("handle_map epoch %u <= our %u\n", |
| 3632 | epoch, mdsc->mdsmap->m_epoch); |
| 3633 | mutex_unlock(&mdsc->mutex); |
| 3634 | return; |
| 3635 | } |
| 3636 | |
| 3637 | newmap = ceph_mdsmap_decode(&p, end); |
| 3638 | if (IS_ERR(newmap)) { |
| 3639 | err = PTR_ERR(newmap); |
| 3640 | goto bad_unlock; |
| 3641 | } |
| 3642 | |
| 3643 | /* swap into place */ |
| 3644 | if (mdsc->mdsmap) { |
| 3645 | oldmap = mdsc->mdsmap; |
| 3646 | mdsc->mdsmap = newmap; |
| 3647 | check_new_map(mdsc, newmap, oldmap); |
| 3648 | ceph_mdsmap_destroy(oldmap); |
| 3649 | } else { |
| 3650 | mdsc->mdsmap = newmap; /* first mds map */ |
| 3651 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3652 | mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3653 | |
| 3654 | __wake_requests(mdsc, &mdsc->waiting_for_map); |
| 3655 | |
| 3656 | mutex_unlock(&mdsc->mutex); |
| 3657 | schedule_delayed(mdsc); |
| 3658 | return; |
| 3659 | |
| 3660 | bad_unlock: |
| 3661 | mutex_unlock(&mdsc->mutex); |
| 3662 | bad: |
| 3663 | pr_err("error decoding mdsmap %d\n", err); |
| 3664 | return; |
| 3665 | } |
| 3666 | |
| 3667 | static struct ceph_connection *con_get(struct ceph_connection *con) |
| 3668 | { |
| 3669 | struct ceph_mds_session *s = con->private; |
| 3670 | |
| 3671 | if (get_session(s)) { |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3672 | dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3673 | return con; |
| 3674 | } |
| 3675 | dout("mdsc con_get %p FAIL\n", s); |
| 3676 | return NULL; |
| 3677 | } |
| 3678 | |
| 3679 | static void con_put(struct ceph_connection *con) |
| 3680 | { |
| 3681 | struct ceph_mds_session *s = con->private; |
| 3682 | |
Henry C Chang | 7d8e18a | 2011-05-11 10:29:52 +0000 | [diff] [blame] | 3683 | dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3684 | ceph_put_mds_session(s); |
| 3685 | } |
| 3686 | |
| 3687 | /* |
| 3688 | * if the client is unresponsive for long enough, the mds will kill |
| 3689 | * the session entirely. |
| 3690 | */ |
| 3691 | static void peer_reset(struct ceph_connection *con) |
| 3692 | { |
| 3693 | struct ceph_mds_session *s = con->private; |
Sage Weil | 7e70f0e | 2010-03-18 13:59:12 -0700 | [diff] [blame] | 3694 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3695 | |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 3696 | pr_warn("mds%d closed our session\n", s->s_mds); |
Sage Weil | 7e70f0e | 2010-03-18 13:59:12 -0700 | [diff] [blame] | 3697 | send_mds_reconnect(mdsc, s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) |
| 3701 | { |
| 3702 | struct ceph_mds_session *s = con->private; |
| 3703 | struct ceph_mds_client *mdsc = s->s_mdsc; |
| 3704 | int type = le16_to_cpu(msg->hdr.type); |
| 3705 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3706 | mutex_lock(&mdsc->mutex); |
| 3707 | if (__verify_registered_session(mdsc, s) < 0) { |
| 3708 | mutex_unlock(&mdsc->mutex); |
| 3709 | goto out; |
| 3710 | } |
| 3711 | mutex_unlock(&mdsc->mutex); |
| 3712 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3713 | switch (type) { |
| 3714 | case CEPH_MSG_MDS_MAP: |
| 3715 | ceph_mdsc_handle_map(mdsc, msg); |
| 3716 | break; |
| 3717 | case CEPH_MSG_CLIENT_SESSION: |
| 3718 | handle_session(s, msg); |
| 3719 | break; |
| 3720 | case CEPH_MSG_CLIENT_REPLY: |
| 3721 | handle_reply(s, msg); |
| 3722 | break; |
| 3723 | case CEPH_MSG_CLIENT_REQUEST_FORWARD: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3724 | handle_forward(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3725 | break; |
| 3726 | case CEPH_MSG_CLIENT_CAPS: |
| 3727 | ceph_handle_caps(s, msg); |
| 3728 | break; |
| 3729 | case CEPH_MSG_CLIENT_SNAP: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3730 | ceph_handle_snap(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3731 | break; |
| 3732 | case CEPH_MSG_CLIENT_LEASE: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3733 | handle_lease(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3734 | break; |
| 3735 | |
| 3736 | default: |
| 3737 | pr_err("received unknown message type %d %s\n", type, |
| 3738 | ceph_msg_type_name(type)); |
| 3739 | } |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3740 | out: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3741 | ceph_msg_put(msg); |
| 3742 | } |
| 3743 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3744 | /* |
| 3745 | * authentication |
| 3746 | */ |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3747 | |
| 3748 | /* |
| 3749 | * Note: returned pointer is the address of a structure that's |
| 3750 | * managed separately. Caller must *not* attempt to free it. |
| 3751 | */ |
| 3752 | static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, |
Alex Elder | 8f43fb5 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3753 | int *proto, int force_new) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3754 | { |
| 3755 | struct ceph_mds_session *s = con->private; |
| 3756 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3757 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3758 | struct ceph_auth_handshake *auth = &s->s_auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3759 | |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3760 | if (force_new && auth->authorizer) { |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 3761 | ceph_auth_destroy_authorizer(ac, auth->authorizer); |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3762 | auth->authorizer = NULL; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3763 | } |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 3764 | if (!auth->authorizer) { |
| 3765 | int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS, |
| 3766 | auth); |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 3767 | if (ret) |
| 3768 | return ERR_PTR(ret); |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 3769 | } else { |
| 3770 | int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS, |
| 3771 | auth); |
Alex Elder | a255651 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3772 | if (ret) |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3773 | return ERR_PTR(ret); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3774 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3775 | *proto = ac->protocol; |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3776 | |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 3777 | return auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | |
| 3781 | static int verify_authorizer_reply(struct ceph_connection *con, int len) |
| 3782 | { |
| 3783 | struct ceph_mds_session *s = con->private; |
| 3784 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3785 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3786 | |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 3787 | return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3788 | } |
| 3789 | |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 3790 | static int invalidate_authorizer(struct ceph_connection *con) |
| 3791 | { |
| 3792 | struct ceph_mds_session *s = con->private; |
| 3793 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3794 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 3795 | |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 3796 | ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 3797 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3798 | return ceph_monc_validate_auth(&mdsc->fsc->client->monc); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 3799 | } |
| 3800 | |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 3801 | static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con, |
| 3802 | struct ceph_msg_header *hdr, int *skip) |
| 3803 | { |
| 3804 | struct ceph_msg *msg; |
| 3805 | int type = (int) le16_to_cpu(hdr->type); |
| 3806 | int front_len = (int) le32_to_cpu(hdr->front_len); |
| 3807 | |
| 3808 | if (con->in_msg) |
| 3809 | return con->in_msg; |
| 3810 | |
| 3811 | *skip = 0; |
| 3812 | msg = ceph_msg_new(type, front_len, GFP_NOFS, false); |
| 3813 | if (!msg) { |
| 3814 | pr_err("unable to allocate msg type %d len %d\n", |
| 3815 | type, front_len); |
| 3816 | return NULL; |
| 3817 | } |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 3818 | |
| 3819 | return msg; |
| 3820 | } |
| 3821 | |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 3822 | static int sign_message(struct ceph_connection *con, struct ceph_msg *msg) |
| 3823 | { |
| 3824 | struct ceph_mds_session *s = con->private; |
| 3825 | struct ceph_auth_handshake *auth = &s->s_auth; |
| 3826 | return ceph_auth_sign_message(auth, msg); |
| 3827 | } |
| 3828 | |
| 3829 | static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg) |
| 3830 | { |
| 3831 | struct ceph_mds_session *s = con->private; |
| 3832 | struct ceph_auth_handshake *auth = &s->s_auth; |
| 3833 | return ceph_auth_check_message_signature(auth, msg); |
| 3834 | } |
| 3835 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 3836 | static const struct ceph_connection_operations mds_con_ops = { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3837 | .get = con_get, |
| 3838 | .put = con_put, |
| 3839 | .dispatch = dispatch, |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 3840 | .get_authorizer = get_authorizer, |
| 3841 | .verify_authorizer_reply = verify_authorizer_reply, |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 3842 | .invalidate_authorizer = invalidate_authorizer, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3843 | .peer_reset = peer_reset, |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 3844 | .alloc_msg = mds_alloc_msg, |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 3845 | .sign_message = sign_message, |
| 3846 | .check_message_signature = check_message_signature, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3847 | }; |
| 3848 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3849 | /* eof */ |