Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3 | |
| 4 | #include <linux/fs.h> |
| 5 | #include <linux/kernel.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 6 | #include <linux/sched/signal.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 8 | #include <linux/vmalloc.h> |
| 9 | #include <linux/wait.h> |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 10 | #include <linux/writeback.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 11 | |
| 12 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 13 | #include "mds_client.h" |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 14 | #include "cache.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 15 | #include <linux/ceph/decode.h> |
| 16 | #include <linux/ceph/messenger.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Capability management |
| 20 | * |
| 21 | * The Ceph metadata servers control client access to inode metadata |
| 22 | * and file data by issuing capabilities, granting clients permission |
| 23 | * to read and/or write both inode field and file data to OSDs |
| 24 | * (storage nodes). Each capability consists of a set of bits |
| 25 | * indicating which operations are allowed. |
| 26 | * |
| 27 | * If the client holds a *_SHARED cap, the client has a coherent value |
| 28 | * that can be safely read from the cached inode. |
| 29 | * |
| 30 | * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the |
| 31 | * client is allowed to change inode attributes (e.g., file size, |
| 32 | * mtime), note its dirty state in the ceph_cap, and asynchronously |
| 33 | * flush that metadata change to the MDS. |
| 34 | * |
| 35 | * In the event of a conflicting operation (perhaps by another |
| 36 | * client), the MDS will revoke the conflicting client capabilities. |
| 37 | * |
| 38 | * In order for a client to cache an inode, it must hold a capability |
| 39 | * with at least one MDS server. When inodes are released, release |
| 40 | * notifications are batched and periodically sent en masse to the MDS |
| 41 | * cluster to release server state. |
| 42 | */ |
| 43 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 44 | static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc); |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 45 | static void __kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 46 | struct ceph_mds_session *session, |
| 47 | struct ceph_inode_info *ci, |
| 48 | u64 oldest_flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Generate readable cap strings for debugging output. |
| 52 | */ |
| 53 | #define MAX_CAP_STR 20 |
| 54 | static char cap_str[MAX_CAP_STR][40]; |
| 55 | static DEFINE_SPINLOCK(cap_str_lock); |
| 56 | static int last_cap_str; |
| 57 | |
| 58 | static char *gcap_string(char *s, int c) |
| 59 | { |
| 60 | if (c & CEPH_CAP_GSHARED) |
| 61 | *s++ = 's'; |
| 62 | if (c & CEPH_CAP_GEXCL) |
| 63 | *s++ = 'x'; |
| 64 | if (c & CEPH_CAP_GCACHE) |
| 65 | *s++ = 'c'; |
| 66 | if (c & CEPH_CAP_GRD) |
| 67 | *s++ = 'r'; |
| 68 | if (c & CEPH_CAP_GWR) |
| 69 | *s++ = 'w'; |
| 70 | if (c & CEPH_CAP_GBUFFER) |
| 71 | *s++ = 'b'; |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 72 | if (c & CEPH_CAP_GWREXTEND) |
| 73 | *s++ = 'a'; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 74 | if (c & CEPH_CAP_GLAZYIO) |
| 75 | *s++ = 'l'; |
| 76 | return s; |
| 77 | } |
| 78 | |
| 79 | const char *ceph_cap_string(int caps) |
| 80 | { |
| 81 | int i; |
| 82 | char *s; |
| 83 | int c; |
| 84 | |
| 85 | spin_lock(&cap_str_lock); |
| 86 | i = last_cap_str++; |
| 87 | if (last_cap_str == MAX_CAP_STR) |
| 88 | last_cap_str = 0; |
| 89 | spin_unlock(&cap_str_lock); |
| 90 | |
| 91 | s = cap_str[i]; |
| 92 | |
| 93 | if (caps & CEPH_CAP_PIN) |
| 94 | *s++ = 'p'; |
| 95 | |
| 96 | c = (caps >> CEPH_CAP_SAUTH) & 3; |
| 97 | if (c) { |
| 98 | *s++ = 'A'; |
| 99 | s = gcap_string(s, c); |
| 100 | } |
| 101 | |
| 102 | c = (caps >> CEPH_CAP_SLINK) & 3; |
| 103 | if (c) { |
| 104 | *s++ = 'L'; |
| 105 | s = gcap_string(s, c); |
| 106 | } |
| 107 | |
| 108 | c = (caps >> CEPH_CAP_SXATTR) & 3; |
| 109 | if (c) { |
| 110 | *s++ = 'X'; |
| 111 | s = gcap_string(s, c); |
| 112 | } |
| 113 | |
| 114 | c = caps >> CEPH_CAP_SFILE; |
| 115 | if (c) { |
| 116 | *s++ = 'F'; |
| 117 | s = gcap_string(s, c); |
| 118 | } |
| 119 | |
| 120 | if (s == cap_str[i]) |
| 121 | *s++ = '-'; |
| 122 | *s = 0; |
| 123 | return cap_str[i]; |
| 124 | } |
| 125 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 126 | void ceph_caps_init(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 127 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 128 | INIT_LIST_HEAD(&mdsc->caps_list); |
| 129 | spin_lock_init(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 132 | void ceph_caps_finalize(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 133 | { |
| 134 | struct ceph_cap *cap; |
| 135 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 136 | spin_lock(&mdsc->caps_list_lock); |
| 137 | while (!list_empty(&mdsc->caps_list)) { |
| 138 | cap = list_first_entry(&mdsc->caps_list, |
| 139 | struct ceph_cap, caps_item); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 140 | list_del(&cap->caps_item); |
| 141 | kmem_cache_free(ceph_cap_cachep, cap); |
| 142 | } |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 143 | mdsc->caps_total_count = 0; |
| 144 | mdsc->caps_avail_count = 0; |
| 145 | mdsc->caps_use_count = 0; |
| 146 | mdsc->caps_reserve_count = 0; |
| 147 | mdsc->caps_min_count = 0; |
| 148 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 151 | void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta) |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 152 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 153 | spin_lock(&mdsc->caps_list_lock); |
| 154 | mdsc->caps_min_count += delta; |
| 155 | BUG_ON(mdsc->caps_min_count < 0); |
| 156 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Chengguang Xu | 7bf8f73 | 2018-07-28 23:15:35 +0800 | [diff] [blame] | 159 | static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps) |
| 160 | { |
| 161 | struct ceph_cap *cap; |
| 162 | int i; |
| 163 | |
| 164 | if (nr_caps) { |
| 165 | BUG_ON(mdsc->caps_reserve_count < nr_caps); |
| 166 | mdsc->caps_reserve_count -= nr_caps; |
| 167 | if (mdsc->caps_avail_count >= |
| 168 | mdsc->caps_reserve_count + mdsc->caps_min_count) { |
| 169 | mdsc->caps_total_count -= nr_caps; |
| 170 | for (i = 0; i < nr_caps; i++) { |
| 171 | cap = list_first_entry(&mdsc->caps_list, |
| 172 | struct ceph_cap, caps_item); |
| 173 | list_del(&cap->caps_item); |
| 174 | kmem_cache_free(ceph_cap_cachep, cap); |
| 175 | } |
| 176 | } else { |
| 177 | mdsc->caps_avail_count += nr_caps; |
| 178 | } |
| 179 | |
| 180 | dout("%s: caps %d = %d used + %d resv + %d avail\n", |
| 181 | __func__, |
| 182 | mdsc->caps_total_count, mdsc->caps_use_count, |
| 183 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
| 184 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 185 | mdsc->caps_reserve_count + |
| 186 | mdsc->caps_avail_count); |
| 187 | } |
| 188 | } |
| 189 | |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 190 | /* |
| 191 | * Called under mdsc->mutex. |
| 192 | */ |
| 193 | int ceph_reserve_caps(struct ceph_mds_client *mdsc, |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 194 | struct ceph_cap_reservation *ctx, int need) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 195 | { |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 196 | int i, j; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 197 | struct ceph_cap *cap; |
| 198 | int have; |
| 199 | int alloc = 0; |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 200 | int max_caps; |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame^] | 201 | int err = 0; |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 202 | bool trimmed = false; |
| 203 | struct ceph_mds_session *s; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 204 | LIST_HEAD(newcaps); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 205 | |
| 206 | dout("reserve caps ctx=%p need=%d\n", ctx, need); |
| 207 | |
| 208 | /* first reserve any caps that are already allocated */ |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 209 | spin_lock(&mdsc->caps_list_lock); |
| 210 | if (mdsc->caps_avail_count >= need) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 211 | have = need; |
| 212 | else |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 213 | have = mdsc->caps_avail_count; |
| 214 | mdsc->caps_avail_count -= have; |
| 215 | mdsc->caps_reserve_count += have; |
| 216 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 217 | mdsc->caps_reserve_count + |
| 218 | mdsc->caps_avail_count); |
| 219 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 220 | |
Chengguang Xu | 79cd674 | 2018-02-24 18:36:02 +0800 | [diff] [blame] | 221 | for (i = have; i < need; ) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 222 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
Chengguang Xu | 79cd674 | 2018-02-24 18:36:02 +0800 | [diff] [blame] | 223 | if (cap) { |
| 224 | list_add(&cap->caps_item, &newcaps); |
| 225 | alloc++; |
| 226 | i++; |
| 227 | continue; |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 228 | } |
Chengguang Xu | 79cd674 | 2018-02-24 18:36:02 +0800 | [diff] [blame] | 229 | |
| 230 | if (!trimmed) { |
| 231 | for (j = 0; j < mdsc->max_sessions; j++) { |
| 232 | s = __ceph_lookup_mds_session(mdsc, j); |
| 233 | if (!s) |
| 234 | continue; |
| 235 | mutex_unlock(&mdsc->mutex); |
| 236 | |
| 237 | mutex_lock(&s->s_mutex); |
| 238 | max_caps = s->s_nr_caps - (need - i); |
| 239 | ceph_trim_caps(mdsc, s, max_caps); |
| 240 | mutex_unlock(&s->s_mutex); |
| 241 | |
| 242 | ceph_put_mds_session(s); |
| 243 | mutex_lock(&mdsc->mutex); |
| 244 | } |
| 245 | trimmed = true; |
| 246 | |
| 247 | spin_lock(&mdsc->caps_list_lock); |
| 248 | if (mdsc->caps_avail_count) { |
| 249 | int more_have; |
| 250 | if (mdsc->caps_avail_count >= need - i) |
| 251 | more_have = need - i; |
| 252 | else |
| 253 | more_have = mdsc->caps_avail_count; |
| 254 | |
| 255 | i += more_have; |
| 256 | have += more_have; |
| 257 | mdsc->caps_avail_count -= more_have; |
| 258 | mdsc->caps_reserve_count += more_have; |
| 259 | |
| 260 | } |
| 261 | spin_unlock(&mdsc->caps_list_lock); |
| 262 | |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n", |
| 267 | ctx, need, have + alloc); |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame^] | 268 | err = -ENOMEM; |
| 269 | break; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 270 | } |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame^] | 271 | |
| 272 | if (!err) { |
| 273 | BUG_ON(have + alloc != need); |
| 274 | ctx->count = need; |
| 275 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 276 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 277 | spin_lock(&mdsc->caps_list_lock); |
| 278 | mdsc->caps_total_count += alloc; |
| 279 | mdsc->caps_reserve_count += alloc; |
| 280 | list_splice(&newcaps, &mdsc->caps_list); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 281 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 282 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 283 | mdsc->caps_reserve_count + |
| 284 | mdsc->caps_avail_count); |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame^] | 285 | |
| 286 | if (err) |
| 287 | __ceph_unreserve_caps(mdsc, have + alloc); |
| 288 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 289 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 290 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 291 | dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 292 | ctx, mdsc->caps_total_count, mdsc->caps_use_count, |
| 293 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame^] | 294 | return err; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Chengguang Xu | 7bf8f73 | 2018-07-28 23:15:35 +0800 | [diff] [blame] | 297 | void ceph_unreserve_caps(struct ceph_mds_client *mdsc, |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 298 | struct ceph_cap_reservation *ctx) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 299 | { |
| 300 | dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count); |
Chengguang Xu | 7bf8f73 | 2018-07-28 23:15:35 +0800 | [diff] [blame] | 301 | spin_lock(&mdsc->caps_list_lock); |
| 302 | __ceph_unreserve_caps(mdsc, ctx->count); |
| 303 | ctx->count = 0; |
| 304 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 307 | struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc, |
| 308 | struct ceph_cap_reservation *ctx) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 309 | { |
| 310 | struct ceph_cap *cap = NULL; |
| 311 | |
| 312 | /* temporary, until we do something about cap import/export */ |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 313 | if (!ctx) { |
| 314 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
| 315 | if (cap) { |
Yan, Zheng | 4d1d0534 | 2012-11-03 10:32:37 +0800 | [diff] [blame] | 316 | spin_lock(&mdsc->caps_list_lock); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 317 | mdsc->caps_use_count++; |
| 318 | mdsc->caps_total_count++; |
Yan, Zheng | 4d1d0534 | 2012-11-03 10:32:37 +0800 | [diff] [blame] | 319 | spin_unlock(&mdsc->caps_list_lock); |
Chengguang Xu | e327ce0 | 2018-02-24 18:35:29 +0800 | [diff] [blame] | 320 | } else { |
| 321 | spin_lock(&mdsc->caps_list_lock); |
| 322 | if (mdsc->caps_avail_count) { |
| 323 | BUG_ON(list_empty(&mdsc->caps_list)); |
| 324 | |
| 325 | mdsc->caps_avail_count--; |
| 326 | mdsc->caps_use_count++; |
| 327 | cap = list_first_entry(&mdsc->caps_list, |
| 328 | struct ceph_cap, caps_item); |
| 329 | list_del(&cap->caps_item); |
| 330 | |
| 331 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 332 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 333 | } |
| 334 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 335 | } |
Chengguang Xu | e327ce0 | 2018-02-24 18:35:29 +0800 | [diff] [blame] | 336 | |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 337 | return cap; |
| 338 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 339 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 340 | spin_lock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 341 | dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 342 | ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count, |
| 343 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 344 | BUG_ON(!ctx->count); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 345 | BUG_ON(ctx->count > mdsc->caps_reserve_count); |
| 346 | BUG_ON(list_empty(&mdsc->caps_list)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 347 | |
| 348 | ctx->count--; |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 349 | mdsc->caps_reserve_count--; |
| 350 | mdsc->caps_use_count++; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 351 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 352 | cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 353 | list_del(&cap->caps_item); |
| 354 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 355 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 356 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 357 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 358 | return cap; |
| 359 | } |
| 360 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 361 | void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 362 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 363 | spin_lock(&mdsc->caps_list_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 364 | dout("put_cap %p %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 365 | cap, mdsc->caps_total_count, mdsc->caps_use_count, |
| 366 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
| 367 | mdsc->caps_use_count--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 368 | /* |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 369 | * Keep some preallocated caps around (ceph_min_count), to |
| 370 | * avoid lots of free/alloc churn. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 371 | */ |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 372 | if (mdsc->caps_avail_count >= mdsc->caps_reserve_count + |
| 373 | mdsc->caps_min_count) { |
| 374 | mdsc->caps_total_count--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 375 | kmem_cache_free(ceph_cap_cachep, cap); |
| 376 | } else { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 377 | mdsc->caps_avail_count++; |
| 378 | list_add(&cap->caps_item, &mdsc->caps_list); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 381 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 382 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 383 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 386 | void ceph_reservation_status(struct ceph_fs_client *fsc, |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 387 | int *total, int *avail, int *used, int *reserved, |
| 388 | int *min) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 389 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 390 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 391 | |
Chengguang Xu | b884014 | 2018-02-23 17:09:38 +0800 | [diff] [blame] | 392 | spin_lock(&mdsc->caps_list_lock); |
| 393 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 394 | if (total) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 395 | *total = mdsc->caps_total_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 396 | if (avail) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 397 | *avail = mdsc->caps_avail_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 398 | if (used) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 399 | *used = mdsc->caps_use_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 400 | if (reserved) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 401 | *reserved = mdsc->caps_reserve_count; |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 402 | if (min) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 403 | *min = mdsc->caps_min_count; |
Chengguang Xu | b884014 | 2018-02-23 17:09:38 +0800 | [diff] [blame] | 404 | |
| 405 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Find ceph_cap for given mds, if any. |
| 410 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 411 | * Called with i_ceph_lock held. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 412 | */ |
| 413 | static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
| 414 | { |
| 415 | struct ceph_cap *cap; |
| 416 | struct rb_node *n = ci->i_caps.rb_node; |
| 417 | |
| 418 | while (n) { |
| 419 | cap = rb_entry(n, struct ceph_cap, ci_node); |
| 420 | if (mds < cap->mds) |
| 421 | n = n->rb_left; |
| 422 | else if (mds > cap->mds) |
| 423 | n = n->rb_right; |
| 424 | else |
| 425 | return cap; |
| 426 | } |
| 427 | return NULL; |
| 428 | } |
| 429 | |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 430 | struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
| 431 | { |
| 432 | struct ceph_cap *cap; |
| 433 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 434 | spin_lock(&ci->i_ceph_lock); |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 435 | cap = __get_cap_for_mds(ci, mds); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 436 | spin_unlock(&ci->i_ceph_lock); |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 437 | return cap; |
| 438 | } |
| 439 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 440 | /* |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 441 | * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 442 | */ |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 443 | static int __ceph_get_cap_mds(struct ceph_inode_info *ci) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 444 | { |
| 445 | struct ceph_cap *cap; |
| 446 | int mds = -1; |
| 447 | struct rb_node *p; |
| 448 | |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 449 | /* prefer mds with WR|BUFFER|EXCL caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 450 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 451 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 452 | mds = cap->mds; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 453 | if (cap->issued & (CEPH_CAP_FILE_WR | |
| 454 | CEPH_CAP_FILE_BUFFER | |
| 455 | CEPH_CAP_FILE_EXCL)) |
| 456 | break; |
| 457 | } |
| 458 | return mds; |
| 459 | } |
| 460 | |
| 461 | int ceph_get_cap_mds(struct inode *inode) |
| 462 | { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 463 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 464 | int mds; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 465 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 466 | mds = __ceph_get_cap_mds(ceph_inode(inode)); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 467 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 468 | return mds; |
| 469 | } |
| 470 | |
| 471 | /* |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 472 | * Called under i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 473 | */ |
| 474 | static void __insert_cap_node(struct ceph_inode_info *ci, |
| 475 | struct ceph_cap *new) |
| 476 | { |
| 477 | struct rb_node **p = &ci->i_caps.rb_node; |
| 478 | struct rb_node *parent = NULL; |
| 479 | struct ceph_cap *cap = NULL; |
| 480 | |
| 481 | while (*p) { |
| 482 | parent = *p; |
| 483 | cap = rb_entry(parent, struct ceph_cap, ci_node); |
| 484 | if (new->mds < cap->mds) |
| 485 | p = &(*p)->rb_left; |
| 486 | else if (new->mds > cap->mds) |
| 487 | p = &(*p)->rb_right; |
| 488 | else |
| 489 | BUG(); |
| 490 | } |
| 491 | |
| 492 | rb_link_node(&new->ci_node, parent, p); |
| 493 | rb_insert_color(&new->ci_node, &ci->i_caps); |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * (re)set cap hold timeouts, which control the delayed release |
| 498 | * of unused caps back to the MDS. Should be called on cap use. |
| 499 | */ |
| 500 | static void __cap_set_timeouts(struct ceph_mds_client *mdsc, |
| 501 | struct ceph_inode_info *ci) |
| 502 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 503 | struct ceph_mount_options *ma = mdsc->fsc->mount_options; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 504 | |
| 505 | ci->i_hold_caps_min = round_jiffies(jiffies + |
| 506 | ma->caps_wanted_delay_min * HZ); |
| 507 | ci->i_hold_caps_max = round_jiffies(jiffies + |
| 508 | ma->caps_wanted_delay_max * HZ); |
| 509 | dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode, |
| 510 | ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies); |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * (Re)queue cap at the end of the delayed cap release list. |
| 515 | * |
| 516 | * If I_FLUSH is set, leave the inode at the front of the list. |
| 517 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 518 | * Caller holds i_ceph_lock |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 519 | * -> we take mdsc->cap_delay_lock |
| 520 | */ |
| 521 | static void __cap_delay_requeue(struct ceph_mds_client *mdsc, |
| 522 | struct ceph_inode_info *ci) |
| 523 | { |
| 524 | __cap_set_timeouts(mdsc, ci); |
| 525 | dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode, |
| 526 | ci->i_ceph_flags, ci->i_hold_caps_max); |
| 527 | if (!mdsc->stopping) { |
| 528 | spin_lock(&mdsc->cap_delay_lock); |
| 529 | if (!list_empty(&ci->i_cap_delay_list)) { |
| 530 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
| 531 | goto no_change; |
| 532 | list_del_init(&ci->i_cap_delay_list); |
| 533 | } |
| 534 | list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
| 535 | no_change: |
| 536 | spin_unlock(&mdsc->cap_delay_lock); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * Queue an inode for immediate writeback. Mark inode with I_FLUSH, |
| 542 | * indicating we should send a cap message to flush dirty metadata |
| 543 | * asap, and move to the front of the delayed cap list. |
| 544 | */ |
| 545 | static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, |
| 546 | struct ceph_inode_info *ci) |
| 547 | { |
| 548 | dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode); |
| 549 | spin_lock(&mdsc->cap_delay_lock); |
| 550 | ci->i_ceph_flags |= CEPH_I_FLUSH; |
| 551 | if (!list_empty(&ci->i_cap_delay_list)) |
| 552 | list_del_init(&ci->i_cap_delay_list); |
| 553 | list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
| 554 | spin_unlock(&mdsc->cap_delay_lock); |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Cancel delayed work on cap. |
| 559 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 560 | * Caller must hold i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 561 | */ |
| 562 | static void __cap_delay_cancel(struct ceph_mds_client *mdsc, |
| 563 | struct ceph_inode_info *ci) |
| 564 | { |
| 565 | dout("__cap_delay_cancel %p\n", &ci->vfs_inode); |
| 566 | if (list_empty(&ci->i_cap_delay_list)) |
| 567 | return; |
| 568 | spin_lock(&mdsc->cap_delay_lock); |
| 569 | list_del_init(&ci->i_cap_delay_list); |
| 570 | spin_unlock(&mdsc->cap_delay_lock); |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Common issue checks for add_cap, handle_cap_grant. |
| 575 | */ |
| 576 | static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, |
| 577 | unsigned issued) |
| 578 | { |
| 579 | unsigned had = __ceph_caps_issued(ci, NULL); |
| 580 | |
| 581 | /* |
| 582 | * Each time we receive FILE_CACHE anew, we increment |
| 583 | * i_rdcache_gen. |
| 584 | */ |
Sage Weil | 2962507 | 2010-05-27 10:40:43 -0700 | [diff] [blame] | 585 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 586 | (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 587 | ci->i_rdcache_gen++; |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 588 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 589 | |
| 590 | /* |
Yan, Zheng | 15b51bd | 2017-09-06 10:15:16 +0800 | [diff] [blame] | 591 | * If FILE_SHARED is newly issued, mark dir not complete. We don't |
| 592 | * know what happened to this directory while we didn't have the cap. |
| 593 | * If FILE_SHARED is being revoked, also mark dir not complete. It |
| 594 | * stops on-going cached readdir. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 595 | */ |
Yan, Zheng | 15b51bd | 2017-09-06 10:15:16 +0800 | [diff] [blame] | 596 | if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) { |
| 597 | if (issued & CEPH_CAP_FILE_SHARED) |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 598 | atomic_inc(&ci->i_shared_gen); |
Yan, Zheng | a8673d6 | 2013-02-18 16:38:14 +0800 | [diff] [blame] | 599 | if (S_ISDIR(ci->vfs_inode.i_mode)) { |
| 600 | dout(" marking %p NOT complete\n", &ci->vfs_inode); |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 601 | __ceph_dir_clear_complete(ci); |
Yan, Zheng | a8673d6 | 2013-02-18 16:38:14 +0800 | [diff] [blame] | 602 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Add a capability under the given MDS session. |
| 608 | * |
| 609 | * Caller should hold session snap_rwsem (read) and s_mutex. |
| 610 | * |
| 611 | * @fmode is the open file mode, if we are opening a file, otherwise |
| 612 | * it is < 0. (This is so we can atomically add the cap and add an |
| 613 | * open file reference to it.) |
| 614 | */ |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 615 | void ceph_add_cap(struct inode *inode, |
| 616 | struct ceph_mds_session *session, u64 cap_id, |
| 617 | int fmode, unsigned issued, unsigned wanted, |
| 618 | unsigned seq, unsigned mseq, u64 realmino, int flags, |
| 619 | struct ceph_cap **new_cap) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 620 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 621 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 622 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 623 | struct ceph_cap *cap; |
| 624 | int mds = session->s_mds; |
| 625 | int actual_wanted; |
| 626 | |
| 627 | dout("add_cap %p mds%d cap %llx %s seq %d\n", inode, |
| 628 | session->s_mds, cap_id, ceph_cap_string(issued), seq); |
| 629 | |
| 630 | /* |
| 631 | * If we are opening the file, include file mode wanted bits |
| 632 | * in wanted. |
| 633 | */ |
| 634 | if (fmode >= 0) |
| 635 | wanted |= ceph_caps_for_mode(fmode); |
| 636 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 637 | cap = __get_cap_for_mds(ci, mds); |
| 638 | if (!cap) { |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 639 | cap = *new_cap; |
| 640 | *new_cap = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 641 | |
| 642 | cap->issued = 0; |
| 643 | cap->implemented = 0; |
| 644 | cap->mds = mds; |
| 645 | cap->mds_wanted = 0; |
Yan, Zheng | 964266c | 2013-02-27 09:26:09 +0800 | [diff] [blame] | 646 | cap->mseq = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 647 | |
| 648 | cap->ci = ci; |
| 649 | __insert_cap_node(ci, cap); |
| 650 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 651 | /* add to session cap list */ |
| 652 | cap->session = session; |
| 653 | spin_lock(&session->s_cap_lock); |
| 654 | list_add_tail(&cap->session_caps, &session->s_caps); |
| 655 | session->s_nr_caps++; |
| 656 | spin_unlock(&session->s_cap_lock); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 657 | } else { |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 658 | /* |
| 659 | * auth mds of the inode changed. we received the cap export |
| 660 | * message, but still haven't received the cap import message. |
| 661 | * handle_cap_export() updated the new auth MDS' cap. |
| 662 | * |
| 663 | * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing |
| 664 | * a message that was send before the cap import message. So |
| 665 | * don't remove caps. |
| 666 | */ |
| 667 | if (ceph_seq_cmp(seq, cap->seq) <= 0) { |
| 668 | WARN_ON(cap != ci->i_auth_cap); |
| 669 | WARN_ON(cap->cap_id != cap_id); |
| 670 | seq = cap->seq; |
| 671 | mseq = cap->mseq; |
| 672 | issued |= cap->issued; |
| 673 | flags |= CEPH_CAP_FLAG_AUTH; |
| 674 | } |
| 675 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 676 | |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 677 | if (!ci->i_snap_realm || |
| 678 | ((flags & CEPH_CAP_FLAG_AUTH) && |
| 679 | realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 680 | /* |
| 681 | * add this inode to the appropriate snap realm |
| 682 | */ |
| 683 | struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, |
| 684 | realmino); |
| 685 | if (realm) { |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 686 | struct ceph_snap_realm *oldrealm = ci->i_snap_realm; |
| 687 | if (oldrealm) { |
| 688 | spin_lock(&oldrealm->inodes_with_caps_lock); |
| 689 | list_del_init(&ci->i_snap_realm_item); |
| 690 | spin_unlock(&oldrealm->inodes_with_caps_lock); |
| 691 | } |
| 692 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 693 | spin_lock(&realm->inodes_with_caps_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 694 | list_add(&ci->i_snap_realm_item, |
| 695 | &realm->inodes_with_caps); |
Luis Henriques | e3161f1 | 2018-01-12 17:19:28 +0000 | [diff] [blame] | 696 | ci->i_snap_realm = realm; |
| 697 | if (realm->ino == ci->i_vino.ino) |
| 698 | realm->inode = inode; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 699 | spin_unlock(&realm->inodes_with_caps_lock); |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 700 | |
| 701 | if (oldrealm) |
| 702 | ceph_put_snap_realm(mdsc, oldrealm); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 703 | } else { |
| 704 | pr_err("ceph_add_cap: couldn't find snap realm %llx\n", |
| 705 | realmino); |
Sage Weil | b8cd07e | 2010-07-16 12:00:02 -0700 | [diff] [blame] | 706 | WARN_ON(!realm); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | __check_cap_issue(ci, cap, issued); |
| 711 | |
| 712 | /* |
| 713 | * If we are issued caps we don't want, or the mds' wanted |
| 714 | * value appears to be off, queue a check so we'll release |
| 715 | * later and/or update the mds wanted value. |
| 716 | */ |
| 717 | actual_wanted = __ceph_caps_wanted(ci); |
| 718 | if ((wanted & ~actual_wanted) || |
| 719 | (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { |
| 720 | dout(" issued %s, mds wanted %s, actual %s, queueing\n", |
| 721 | ceph_cap_string(issued), ceph_cap_string(wanted), |
| 722 | ceph_cap_string(actual_wanted)); |
| 723 | __cap_delay_requeue(mdsc, ci); |
| 724 | } |
| 725 | |
Yan, Zheng | b8c2f3a | 2013-05-31 16:37:11 +0800 | [diff] [blame] | 726 | if (flags & CEPH_CAP_FLAG_AUTH) { |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 727 | if (!ci->i_auth_cap || |
Yan, Zheng | d9ffc4f | 2014-03-18 10:15:29 +0800 | [diff] [blame] | 728 | ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) { |
Yan, Zheng | b8c2f3a | 2013-05-31 16:37:11 +0800 | [diff] [blame] | 729 | ci->i_auth_cap = cap; |
Yan, Zheng | d9ffc4f | 2014-03-18 10:15:29 +0800 | [diff] [blame] | 730 | cap->mds_wanted = wanted; |
| 731 | } |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 732 | } else { |
| 733 | WARN_ON(ci->i_auth_cap == cap); |
Yan, Zheng | 8a92a11 | 2013-01-04 14:28:07 +0800 | [diff] [blame] | 734 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 735 | |
| 736 | dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n", |
| 737 | inode, ceph_vinop(inode), cap, ceph_cap_string(issued), |
| 738 | ceph_cap_string(issued|cap->issued), seq, mds); |
| 739 | cap->cap_id = cap_id; |
| 740 | cap->issued = issued; |
| 741 | cap->implemented |= issued; |
Yan, Zheng | d1b8780 | 2013-11-13 14:47:19 +0800 | [diff] [blame] | 742 | if (ceph_seq_cmp(mseq, cap->mseq) > 0) |
Yan, Zheng | 964266c | 2013-02-27 09:26:09 +0800 | [diff] [blame] | 743 | cap->mds_wanted = wanted; |
| 744 | else |
| 745 | cap->mds_wanted |= wanted; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 746 | cap->seq = seq; |
| 747 | cap->issue_seq = seq; |
| 748 | cap->mseq = mseq; |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 749 | cap->cap_gen = session->s_cap_gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 750 | |
| 751 | if (fmode >= 0) |
| 752 | __ceph_get_fmode(ci, fmode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Return true if cap has not timed out and belongs to the current |
| 757 | * generation of the MDS session (i.e. has not gone 'stale' due to |
| 758 | * us losing touch with the mds). |
| 759 | */ |
| 760 | static int __cap_is_valid(struct ceph_cap *cap) |
| 761 | { |
| 762 | unsigned long ttl; |
Sage Weil | cdac830 | 2009-11-10 16:02:23 -0800 | [diff] [blame] | 763 | u32 gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 764 | |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 765 | spin_lock(&cap->session->s_gen_ttl_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 766 | gen = cap->session->s_cap_gen; |
| 767 | ttl = cap->session->s_cap_ttl; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 768 | spin_unlock(&cap->session->s_gen_ttl_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 769 | |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 770 | if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 771 | dout("__cap_is_valid %p cap %p issued %s " |
| 772 | "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode, |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 773 | cap, ceph_cap_string(cap->issued), cap->cap_gen, gen); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | return 1; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Return set of valid cap bits issued to us. Note that caps time |
| 782 | * out, and may be invalidated in bulk if the client session times out |
| 783 | * and session->s_cap_gen is bumped. |
| 784 | */ |
| 785 | int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) |
| 786 | { |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 787 | int have = ci->i_snap_caps; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 788 | struct ceph_cap *cap; |
| 789 | struct rb_node *p; |
| 790 | |
| 791 | if (implemented) |
| 792 | *implemented = 0; |
| 793 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 794 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 795 | if (!__cap_is_valid(cap)) |
| 796 | continue; |
| 797 | dout("__ceph_caps_issued %p cap %p issued %s\n", |
| 798 | &ci->vfs_inode, cap, ceph_cap_string(cap->issued)); |
| 799 | have |= cap->issued; |
| 800 | if (implemented) |
| 801 | *implemented |= cap->implemented; |
| 802 | } |
Yan, Zheng | b1530f5 | 2013-07-02 12:40:20 +0800 | [diff] [blame] | 803 | /* |
| 804 | * exclude caps issued by non-auth MDS, but are been revoking |
| 805 | * by the auth MDS. The non-auth MDS should be revoking/exporting |
| 806 | * these caps, but the message is delayed. |
| 807 | */ |
| 808 | if (ci->i_auth_cap) { |
| 809 | cap = ci->i_auth_cap; |
| 810 | have &= ~cap->implemented | cap->issued; |
| 811 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 812 | return have; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Get cap bits issued by caps other than @ocap |
| 817 | */ |
| 818 | int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) |
| 819 | { |
| 820 | int have = ci->i_snap_caps; |
| 821 | struct ceph_cap *cap; |
| 822 | struct rb_node *p; |
| 823 | |
| 824 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 825 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 826 | if (cap == ocap) |
| 827 | continue; |
| 828 | if (!__cap_is_valid(cap)) |
| 829 | continue; |
| 830 | have |= cap->issued; |
| 831 | } |
| 832 | return have; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Move a cap to the end of the LRU (oldest caps at list head, newest |
| 837 | * at list tail). |
| 838 | */ |
| 839 | static void __touch_cap(struct ceph_cap *cap) |
| 840 | { |
| 841 | struct ceph_mds_session *s = cap->session; |
| 842 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 843 | spin_lock(&s->s_cap_lock); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 844 | if (!s->s_cap_iterator) { |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 845 | dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap, |
| 846 | s->s_mds); |
| 847 | list_move_tail(&cap->session_caps, &s->s_caps); |
| 848 | } else { |
| 849 | dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n", |
| 850 | &cap->ci->vfs_inode, cap, s->s_mds); |
| 851 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 852 | spin_unlock(&s->s_cap_lock); |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Check if we hold the given mask. If so, move the cap(s) to the |
| 857 | * front of their respective LRUs. (This is the preferred way for |
| 858 | * callers to check for caps they want.) |
| 859 | */ |
| 860 | int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) |
| 861 | { |
| 862 | struct ceph_cap *cap; |
| 863 | struct rb_node *p; |
| 864 | int have = ci->i_snap_caps; |
| 865 | |
| 866 | if ((have & mask) == mask) { |
| 867 | dout("__ceph_caps_issued_mask %p snap issued %s" |
| 868 | " (mask %s)\n", &ci->vfs_inode, |
| 869 | ceph_cap_string(have), |
| 870 | ceph_cap_string(mask)); |
| 871 | return 1; |
| 872 | } |
| 873 | |
| 874 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 875 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 876 | if (!__cap_is_valid(cap)) |
| 877 | continue; |
| 878 | if ((cap->issued & mask) == mask) { |
| 879 | dout("__ceph_caps_issued_mask %p cap %p issued %s" |
| 880 | " (mask %s)\n", &ci->vfs_inode, cap, |
| 881 | ceph_cap_string(cap->issued), |
| 882 | ceph_cap_string(mask)); |
| 883 | if (touch) |
| 884 | __touch_cap(cap); |
| 885 | return 1; |
| 886 | } |
| 887 | |
| 888 | /* does a combination of caps satisfy mask? */ |
| 889 | have |= cap->issued; |
| 890 | if ((have & mask) == mask) { |
| 891 | dout("__ceph_caps_issued_mask %p combo issued %s" |
| 892 | " (mask %s)\n", &ci->vfs_inode, |
| 893 | ceph_cap_string(cap->issued), |
| 894 | ceph_cap_string(mask)); |
| 895 | if (touch) { |
| 896 | struct rb_node *q; |
| 897 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 898 | /* touch this + preceding caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 899 | __touch_cap(cap); |
| 900 | for (q = rb_first(&ci->i_caps); q != p; |
| 901 | q = rb_next(q)) { |
| 902 | cap = rb_entry(q, struct ceph_cap, |
| 903 | ci_node); |
| 904 | if (!__cap_is_valid(cap)) |
| 905 | continue; |
| 906 | __touch_cap(cap); |
| 907 | } |
| 908 | } |
| 909 | return 1; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * Return true if mask caps are currently being revoked by an MDS. |
| 918 | */ |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 919 | int __ceph_caps_revoking_other(struct ceph_inode_info *ci, |
| 920 | struct ceph_cap *ocap, int mask) |
| 921 | { |
| 922 | struct ceph_cap *cap; |
| 923 | struct rb_node *p; |
| 924 | |
| 925 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 926 | cap = rb_entry(p, struct ceph_cap, ci_node); |
Yan, Zheng | 9563f88 | 2013-11-22 13:50:45 +0800 | [diff] [blame] | 927 | if (cap != ocap && |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 928 | (cap->implemented & ~cap->issued & mask)) |
| 929 | return 1; |
| 930 | } |
| 931 | return 0; |
| 932 | } |
| 933 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 934 | int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) |
| 935 | { |
| 936 | struct inode *inode = &ci->vfs_inode; |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 937 | int ret; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 938 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 939 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 940 | ret = __ceph_caps_revoking_other(ci, NULL, mask); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 941 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 942 | dout("ceph_caps_revoking %p %s = %d\n", inode, |
| 943 | ceph_cap_string(mask), ret); |
| 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | int __ceph_caps_used(struct ceph_inode_info *ci) |
| 948 | { |
| 949 | int used = 0; |
| 950 | if (ci->i_pin_ref) |
| 951 | used |= CEPH_CAP_PIN; |
| 952 | if (ci->i_rd_ref) |
| 953 | used |= CEPH_CAP_FILE_RD; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 954 | if (ci->i_rdcache_ref || |
| 955 | (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */ |
| 956 | ci->vfs_inode.i_data.nrpages)) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 957 | used |= CEPH_CAP_FILE_CACHE; |
| 958 | if (ci->i_wr_ref) |
| 959 | used |= CEPH_CAP_FILE_WR; |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 960 | if (ci->i_wb_ref || ci->i_wrbuffer_ref) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 961 | used |= CEPH_CAP_FILE_BUFFER; |
| 962 | return used; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * wanted, by virtue of open file modes |
| 967 | */ |
| 968 | int __ceph_caps_file_wanted(struct ceph_inode_info *ci) |
| 969 | { |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 970 | int i, bits = 0; |
| 971 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { |
| 972 | if (ci->i_nr_by_mode[i]) |
| 973 | bits |= 1 << i; |
| 974 | } |
| 975 | if (bits == 0) |
| 976 | return 0; |
| 977 | return ceph_caps_for_mode(bits >> 1); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Return caps we have registered with the MDS(s) as 'wanted'. |
| 982 | */ |
Yan, Zheng | c1944fe | 2017-01-29 22:15:47 +0800 | [diff] [blame] | 983 | int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 984 | { |
| 985 | struct ceph_cap *cap; |
| 986 | struct rb_node *p; |
| 987 | int mds_wanted = 0; |
| 988 | |
| 989 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 990 | cap = rb_entry(p, struct ceph_cap, ci_node); |
Yan, Zheng | c1944fe | 2017-01-29 22:15:47 +0800 | [diff] [blame] | 991 | if (check && !__cap_is_valid(cap)) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 992 | continue; |
Yan, Zheng | a255060 | 2014-03-08 09:51:45 +0800 | [diff] [blame] | 993 | if (cap == ci->i_auth_cap) |
| 994 | mds_wanted |= cap->mds_wanted; |
| 995 | else |
| 996 | mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 997 | } |
| 998 | return mds_wanted; |
| 999 | } |
| 1000 | |
| 1001 | /* |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1002 | * called under i_ceph_lock |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1003 | */ |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1004 | static int __ceph_is_single_caps(struct ceph_inode_info *ci) |
| 1005 | { |
| 1006 | return rb_first(&ci->i_caps) == rb_last(&ci->i_caps); |
| 1007 | } |
| 1008 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1009 | static int __ceph_is_any_caps(struct ceph_inode_info *ci) |
| 1010 | { |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 1011 | return !RB_EMPTY_ROOT(&ci->i_caps); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1014 | int ceph_is_any_caps(struct inode *inode) |
| 1015 | { |
| 1016 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1017 | int ret; |
| 1018 | |
| 1019 | spin_lock(&ci->i_ceph_lock); |
| 1020 | ret = __ceph_is_any_caps(ci); |
| 1021 | spin_unlock(&ci->i_ceph_lock); |
| 1022 | |
| 1023 | return ret; |
| 1024 | } |
| 1025 | |
Yan, Zheng | db40cc1 | 2015-03-23 20:12:20 +0800 | [diff] [blame] | 1026 | static void drop_inode_snap_realm(struct ceph_inode_info *ci) |
| 1027 | { |
| 1028 | struct ceph_snap_realm *realm = ci->i_snap_realm; |
| 1029 | spin_lock(&realm->inodes_with_caps_lock); |
| 1030 | list_del_init(&ci->i_snap_realm_item); |
| 1031 | ci->i_snap_realm_counter++; |
| 1032 | ci->i_snap_realm = NULL; |
| 1033 | spin_unlock(&realm->inodes_with_caps_lock); |
| 1034 | ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc, |
| 1035 | realm); |
| 1036 | } |
| 1037 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1038 | /* |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1039 | * Remove a cap. Take steps to deal with a racing iterate_session_caps. |
| 1040 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1041 | * caller should hold i_ceph_lock. |
Sage Weil | a636974 | 2010-02-22 13:59:00 -0800 | [diff] [blame] | 1042 | * caller will not hold session s_mutex if called from destroy_inode. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1043 | */ |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1044 | void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1045 | { |
| 1046 | struct ceph_mds_session *session = cap->session; |
| 1047 | struct ceph_inode_info *ci = cap->ci; |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1048 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1049 | ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1050 | int removed = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1051 | |
| 1052 | dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); |
| 1053 | |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1054 | /* remove from session list */ |
| 1055 | spin_lock(&session->s_cap_lock); |
| 1056 | if (session->s_cap_iterator == cap) { |
| 1057 | /* not yet, we are iterating over this very cap */ |
| 1058 | dout("__ceph_remove_cap delaying %p removal from session %p\n", |
| 1059 | cap, cap->session); |
| 1060 | } else { |
| 1061 | list_del_init(&cap->session_caps); |
| 1062 | session->s_nr_caps--; |
| 1063 | cap->session = NULL; |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1064 | removed = 1; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1065 | } |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1066 | /* protect backpointer with s_cap_lock: see iterate_session_caps */ |
| 1067 | cap->ci = NULL; |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1068 | |
| 1069 | /* |
| 1070 | * s_cap_reconnect is protected by s_cap_lock. no one changes |
| 1071 | * s_cap_gen while session is in the reconnect state. |
| 1072 | */ |
| 1073 | if (queue_release && |
| 1074 | (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) { |
| 1075 | cap->queue_release = 1; |
| 1076 | if (removed) { |
| 1077 | list_add_tail(&cap->session_caps, |
| 1078 | &session->s_cap_releases); |
| 1079 | session->s_num_cap_releases++; |
| 1080 | removed = 0; |
| 1081 | } |
| 1082 | } else { |
| 1083 | cap->queue_release = 0; |
| 1084 | } |
| 1085 | cap->cap_ino = ci->i_vino.ino; |
| 1086 | |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1087 | spin_unlock(&session->s_cap_lock); |
| 1088 | |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1089 | /* remove from inode list */ |
| 1090 | rb_erase(&cap->ci_node, &ci->i_caps); |
| 1091 | if (ci->i_auth_cap == cap) |
| 1092 | ci->i_auth_cap = NULL; |
| 1093 | |
| 1094 | if (removed) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1095 | ceph_put_cap(mdsc, cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1096 | |
Yan, Zheng | db40cc1 | 2015-03-23 20:12:20 +0800 | [diff] [blame] | 1097 | /* when reconnect denied, we remove session caps forcibly, |
| 1098 | * i_wr_ref can be non-zero. If there are ongoing write, |
| 1099 | * keep i_snap_realm. |
| 1100 | */ |
| 1101 | if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm) |
| 1102 | drop_inode_snap_realm(ci); |
| 1103 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1104 | if (!__ceph_is_any_real_caps(ci)) |
| 1105 | __cap_delay_cancel(mdsc, ci); |
| 1106 | } |
| 1107 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1108 | struct cap_msg_args { |
| 1109 | struct ceph_mds_session *session; |
| 1110 | u64 ino, cid, follows; |
| 1111 | u64 flush_tid, oldest_flush_tid, size, max_size; |
| 1112 | u64 xattr_version; |
| 1113 | struct ceph_buffer *xattr_buf; |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1114 | struct timespec64 atime, mtime, ctime; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1115 | int op, caps, wanted, dirty; |
| 1116 | u32 seq, issue_seq, mseq, time_warp_seq; |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1117 | u32 flags; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1118 | kuid_t uid; |
| 1119 | kgid_t gid; |
| 1120 | umode_t mode; |
| 1121 | bool inline_data; |
| 1122 | }; |
| 1123 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1124 | /* |
| 1125 | * Build and send a cap message to the given MDS. |
| 1126 | * |
| 1127 | * Caller should be holding s_mutex. |
| 1128 | */ |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1129 | static int send_cap_msg(struct cap_msg_args *arg) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1130 | { |
| 1131 | struct ceph_mds_caps *fc; |
| 1132 | struct ceph_msg *msg; |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1133 | void *p; |
| 1134 | size_t extra_len; |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1135 | struct timespec64 zerotime = {0}; |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1136 | struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1137 | |
| 1138 | dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s" |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1139 | " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu" |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1140 | " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op), |
| 1141 | arg->cid, arg->ino, ceph_cap_string(arg->caps), |
| 1142 | ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty), |
| 1143 | arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid, |
| 1144 | arg->mseq, arg->follows, arg->size, arg->max_size, |
| 1145 | arg->xattr_version, |
| 1146 | arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1147 | |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1148 | /* flock buffer size + inline version + inline data size + |
| 1149 | * osd_epoch_barrier + oldest_flush_tid */ |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1150 | extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4; |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1151 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len, |
| 1152 | GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1153 | if (!msg) |
| 1154 | return -ENOMEM; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1155 | |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1156 | msg->hdr.version = cpu_to_le16(10); |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1157 | msg->hdr.tid = cpu_to_le64(arg->flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1158 | |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 1159 | fc = msg->front.iov_base; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1160 | memset(fc, 0, sizeof(*fc)); |
| 1161 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1162 | fc->cap_id = cpu_to_le64(arg->cid); |
| 1163 | fc->op = cpu_to_le32(arg->op); |
| 1164 | fc->seq = cpu_to_le32(arg->seq); |
| 1165 | fc->issue_seq = cpu_to_le32(arg->issue_seq); |
| 1166 | fc->migrate_seq = cpu_to_le32(arg->mseq); |
| 1167 | fc->caps = cpu_to_le32(arg->caps); |
| 1168 | fc->wanted = cpu_to_le32(arg->wanted); |
| 1169 | fc->dirty = cpu_to_le32(arg->dirty); |
| 1170 | fc->ino = cpu_to_le64(arg->ino); |
| 1171 | fc->snap_follows = cpu_to_le64(arg->follows); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1172 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1173 | fc->size = cpu_to_le64(arg->size); |
| 1174 | fc->max_size = cpu_to_le64(arg->max_size); |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1175 | ceph_encode_timespec64(&fc->mtime, &arg->mtime); |
| 1176 | ceph_encode_timespec64(&fc->atime, &arg->atime); |
| 1177 | ceph_encode_timespec64(&fc->ctime, &arg->ctime); |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1178 | fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1179 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1180 | fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid)); |
| 1181 | fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid)); |
| 1182 | fc->mode = cpu_to_le32(arg->mode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1183 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1184 | fc->xattr_version = cpu_to_le64(arg->xattr_version); |
| 1185 | if (arg->xattr_buf) { |
| 1186 | msg->middle = ceph_buffer_get(arg->xattr_buf); |
| 1187 | fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len); |
| 1188 | msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len); |
Jeff Layton | 9670079 | 2016-11-10 07:42:02 -0500 | [diff] [blame] | 1189 | } |
| 1190 | |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1191 | p = fc + 1; |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1192 | /* flock buffer size (version 2) */ |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1193 | ceph_encode_32(&p, 0); |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1194 | /* inline version (version 4) */ |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1195 | ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE); |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1196 | /* inline data size */ |
| 1197 | ceph_encode_32(&p, 0); |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1198 | /* |
| 1199 | * osd_epoch_barrier (version 5) |
| 1200 | * The epoch_barrier is protected osdc->lock, so READ_ONCE here in |
| 1201 | * case it was recently changed |
| 1202 | */ |
| 1203 | ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier)); |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1204 | /* oldest_flush_tid (version 6) */ |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1205 | ceph_encode_64(&p, arg->oldest_flush_tid); |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1206 | |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1207 | /* |
| 1208 | * caller_uid/caller_gid (version 7) |
| 1209 | * |
| 1210 | * Currently, we don't properly track which caller dirtied the caps |
| 1211 | * last, and force a flush of them when there is a conflict. For now, |
| 1212 | * just set this to 0:0, to emulate how the MDS has worked up to now. |
| 1213 | */ |
| 1214 | ceph_encode_32(&p, 0); |
| 1215 | ceph_encode_32(&p, 0); |
| 1216 | |
| 1217 | /* pool namespace (version 8) (mds always ignores this) */ |
| 1218 | ceph_encode_32(&p, 0); |
| 1219 | |
| 1220 | /* |
| 1221 | * btime and change_attr (version 9) |
| 1222 | * |
| 1223 | * We just zero these out for now, as the MDS ignores them unless |
| 1224 | * the requisite feature flags are set (which we don't do yet). |
| 1225 | */ |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1226 | ceph_encode_timespec64(p, &zerotime); |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1227 | p += sizeof(struct ceph_timespec); |
| 1228 | ceph_encode_64(&p, 0); |
| 1229 | |
| 1230 | /* Advisory flags (version 10) */ |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1231 | ceph_encode_32(&p, arg->flags); |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1232 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1233 | ceph_con_send(&arg->session->s_con, msg); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
Sage Weil | a636974 | 2010-02-22 13:59:00 -0800 | [diff] [blame] | 1238 | * Queue cap releases when an inode is dropped from our cache. Since |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1239 | * inode is about to be destroyed, there is no need for i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1240 | */ |
| 1241 | void ceph_queue_caps_release(struct inode *inode) |
| 1242 | { |
| 1243 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1244 | struct rb_node *p; |
| 1245 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1246 | p = rb_first(&ci->i_caps); |
| 1247 | while (p) { |
| 1248 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1249 | p = rb_next(p); |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1250 | __ceph_remove_cap(cap, true); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1251 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /* |
| 1255 | * Send a cap msg on the given inode. Update our caps state, then |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1256 | * drop i_ceph_lock and send the message. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1257 | * |
| 1258 | * Make note of max_size reported/requested from mds, revoked caps |
| 1259 | * that have now been implemented. |
| 1260 | * |
| 1261 | * Make half-hearted attempt ot to invalidate page cache if we are |
| 1262 | * dropping RDCACHE. Note that this will leave behind locked pages |
| 1263 | * that we'll then need to deal with elsewhere. |
| 1264 | * |
| 1265 | * Return non-zero if delayed release, or we experienced an error |
| 1266 | * such that the caller should requeue + retry later. |
| 1267 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1268 | * called with i_ceph_lock, then drops it. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1269 | * caller should hold snap_rwsem (read), s_mutex. |
| 1270 | */ |
| 1271 | static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1272 | int op, bool sync, int used, int want, int retain, |
| 1273 | int flushing, u64 flush_tid, u64 oldest_flush_tid) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1274 | __releases(cap->ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1275 | { |
| 1276 | struct ceph_inode_info *ci = cap->ci; |
| 1277 | struct inode *inode = &ci->vfs_inode; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1278 | struct cap_msg_args arg; |
Colin Ian King | bb0581f | 2017-10-18 12:34:25 +0100 | [diff] [blame] | 1279 | int held, revoking; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1280 | int wake = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1281 | int delayed = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1282 | int ret; |
| 1283 | |
Sage Weil | 68c2832 | 2010-02-09 13:41:47 -0800 | [diff] [blame] | 1284 | held = cap->issued | cap->implemented; |
| 1285 | revoking = cap->implemented & ~cap->issued; |
| 1286 | retain &= ~revoking; |
Sage Weil | 68c2832 | 2010-02-09 13:41:47 -0800 | [diff] [blame] | 1287 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1288 | dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n", |
| 1289 | inode, cap, cap->session, |
| 1290 | ceph_cap_string(held), ceph_cap_string(held & retain), |
| 1291 | ceph_cap_string(revoking)); |
| 1292 | BUG_ON((retain & CEPH_CAP_PIN) == 0); |
| 1293 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1294 | arg.session = cap->session; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1295 | |
| 1296 | /* don't release wanted unless we've waited a bit. */ |
| 1297 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && |
| 1298 | time_before(jiffies, ci->i_hold_caps_min)) { |
| 1299 | dout(" delaying issued %s -> %s, wanted %s -> %s on send\n", |
| 1300 | ceph_cap_string(cap->issued), |
| 1301 | ceph_cap_string(cap->issued & retain), |
| 1302 | ceph_cap_string(cap->mds_wanted), |
| 1303 | ceph_cap_string(want)); |
| 1304 | want |= cap->mds_wanted; |
| 1305 | retain |= cap->issued; |
| 1306 | delayed = 1; |
| 1307 | } |
| 1308 | ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH); |
Yan, Zheng | eb65b91 | 2017-01-12 17:18:00 +0800 | [diff] [blame] | 1309 | if (want & ~cap->mds_wanted) { |
| 1310 | /* user space may open/close single file frequently. |
| 1311 | * This avoids droping mds_wanted immediately after |
| 1312 | * requesting new mds_wanted. |
| 1313 | */ |
| 1314 | __cap_set_timeouts(mdsc, ci); |
| 1315 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1316 | |
| 1317 | cap->issued &= retain; /* drop bits we don't want */ |
| 1318 | if (cap->implemented & ~cap->issued) { |
| 1319 | /* |
| 1320 | * Wake up any waiters on wanted -> needed transition. |
| 1321 | * This is due to the weird transition from buffered |
| 1322 | * to sync IO... we need to flush dirty pages _before_ |
| 1323 | * allowing sync writes to avoid reordering. |
| 1324 | */ |
| 1325 | wake = 1; |
| 1326 | } |
| 1327 | cap->implemented &= cap->issued | used; |
| 1328 | cap->mds_wanted = want; |
| 1329 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1330 | arg.ino = ceph_vino(inode).ino; |
| 1331 | arg.cid = cap->cap_id; |
| 1332 | arg.follows = flushing ? ci->i_head_snapc->seq : 0; |
| 1333 | arg.flush_tid = flush_tid; |
| 1334 | arg.oldest_flush_tid = oldest_flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1335 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1336 | arg.size = inode->i_size; |
| 1337 | ci->i_reported_size = arg.size; |
| 1338 | arg.max_size = ci->i_wanted_max_size; |
| 1339 | ci->i_requested_max_size = arg.max_size; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1340 | |
Sage Weil | 082afec | 2010-08-22 15:16:41 -0700 | [diff] [blame] | 1341 | if (flushing & CEPH_CAP_XATTR_EXCL) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1342 | __ceph_build_xattrs_blob(ci); |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1343 | arg.xattr_version = ci->i_xattrs.version; |
| 1344 | arg.xattr_buf = ci->i_xattrs.blob; |
| 1345 | } else { |
| 1346 | arg.xattr_buf = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1349 | arg.mtime = inode->i_mtime; |
| 1350 | arg.atime = inode->i_atime; |
| 1351 | arg.ctime = inode->i_ctime; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1352 | |
| 1353 | arg.op = op; |
| 1354 | arg.caps = cap->implemented; |
| 1355 | arg.wanted = want; |
| 1356 | arg.dirty = flushing; |
| 1357 | |
| 1358 | arg.seq = cap->seq; |
| 1359 | arg.issue_seq = cap->issue_seq; |
| 1360 | arg.mseq = cap->mseq; |
| 1361 | arg.time_warp_seq = ci->i_time_warp_seq; |
| 1362 | |
| 1363 | arg.uid = inode->i_uid; |
| 1364 | arg.gid = inode->i_gid; |
| 1365 | arg.mode = inode->i_mode; |
| 1366 | |
| 1367 | arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE; |
Yan, Zheng | 9556971 | 2017-07-24 17:59:39 +0800 | [diff] [blame] | 1368 | if (list_empty(&ci->i_cap_snaps)) |
| 1369 | arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP; |
| 1370 | else |
| 1371 | arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP; |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1372 | if (sync) |
| 1373 | arg.flags |= CEPH_CLIENT_CAPS_SYNC; |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1374 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1375 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1376 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1377 | ret = send_cap_msg(&arg); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1378 | if (ret < 0) { |
| 1379 | dout("error sending cap msg, must requeue %p\n", inode); |
| 1380 | delayed = 1; |
| 1381 | } |
| 1382 | |
| 1383 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 1384 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1385 | |
| 1386 | return delayed; |
| 1387 | } |
| 1388 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1389 | static inline int __send_flush_snap(struct inode *inode, |
| 1390 | struct ceph_mds_session *session, |
| 1391 | struct ceph_cap_snap *capsnap, |
| 1392 | u32 mseq, u64 oldest_flush_tid) |
| 1393 | { |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1394 | struct cap_msg_args arg; |
| 1395 | |
| 1396 | arg.session = session; |
| 1397 | arg.ino = ceph_vino(inode).ino; |
| 1398 | arg.cid = 0; |
| 1399 | arg.follows = capsnap->follows; |
| 1400 | arg.flush_tid = capsnap->cap_flush.tid; |
| 1401 | arg.oldest_flush_tid = oldest_flush_tid; |
| 1402 | |
| 1403 | arg.size = capsnap->size; |
| 1404 | arg.max_size = 0; |
| 1405 | arg.xattr_version = capsnap->xattr_version; |
| 1406 | arg.xattr_buf = capsnap->xattr_blob; |
| 1407 | |
| 1408 | arg.atime = capsnap->atime; |
| 1409 | arg.mtime = capsnap->mtime; |
| 1410 | arg.ctime = capsnap->ctime; |
| 1411 | |
| 1412 | arg.op = CEPH_CAP_OP_FLUSHSNAP; |
| 1413 | arg.caps = capsnap->issued; |
| 1414 | arg.wanted = 0; |
| 1415 | arg.dirty = capsnap->dirty; |
| 1416 | |
| 1417 | arg.seq = 0; |
| 1418 | arg.issue_seq = 0; |
| 1419 | arg.mseq = mseq; |
| 1420 | arg.time_warp_seq = capsnap->time_warp_seq; |
| 1421 | |
| 1422 | arg.uid = capsnap->uid; |
| 1423 | arg.gid = capsnap->gid; |
| 1424 | arg.mode = capsnap->mode; |
| 1425 | |
| 1426 | arg.inline_data = capsnap->inline_data; |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1427 | arg.flags = 0; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1428 | |
| 1429 | return send_cap_msg(&arg); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1430 | } |
| 1431 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1432 | /* |
| 1433 | * When a snapshot is taken, clients accumulate dirty metadata on |
| 1434 | * inodes with capabilities in ceph_cap_snaps to describe the file |
| 1435 | * state at the time the snapshot was taken. This must be flushed |
| 1436 | * asynchronously back to the MDS once sync writes complete and dirty |
| 1437 | * data is written out. |
| 1438 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1439 | * Called under i_ceph_lock. Takes s_mutex as needed. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1440 | */ |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1441 | static void __ceph_flush_snaps(struct ceph_inode_info *ci, |
| 1442 | struct ceph_mds_session *session) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1443 | __releases(ci->i_ceph_lock) |
| 1444 | __acquires(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1445 | { |
| 1446 | struct inode *inode = &ci->vfs_inode; |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1447 | struct ceph_mds_client *mdsc = session->s_mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1448 | struct ceph_cap_snap *capsnap; |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1449 | u64 oldest_flush_tid = 0; |
| 1450 | u64 first_tid = 1, last_tid = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1451 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1452 | dout("__flush_snaps %p session %p\n", inode, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1453 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1454 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1455 | /* |
| 1456 | * we need to wait for sync writes to complete and for dirty |
| 1457 | * pages to be written out. |
| 1458 | */ |
| 1459 | if (capsnap->dirty_pages || capsnap->writing) |
Sage Weil | cfc0bf6 | 2010-09-14 15:50:59 -0700 | [diff] [blame] | 1460 | break; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1461 | |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 1462 | /* should be removed by ceph_try_drop_cap_snap() */ |
| 1463 | BUG_ON(!capsnap->need_flush); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 1464 | |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1465 | /* only flush each capsnap once */ |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1466 | if (capsnap->cap_flush.tid > 0) { |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1467 | dout(" already flushed %p, skipping\n", capsnap); |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1468 | continue; |
| 1469 | } |
| 1470 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1471 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1472 | capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid; |
| 1473 | list_add_tail(&capsnap->cap_flush.g_list, |
| 1474 | &mdsc->cap_flush_list); |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1475 | if (oldest_flush_tid == 0) |
| 1476 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1477 | if (list_empty(&ci->i_flushing_item)) { |
| 1478 | list_add_tail(&ci->i_flushing_item, |
| 1479 | &session->s_cap_flushing); |
| 1480 | } |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1481 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1482 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1483 | list_add_tail(&capsnap->cap_flush.i_list, |
| 1484 | &ci->i_cap_flush_list); |
| 1485 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1486 | if (first_tid == 1) |
| 1487 | first_tid = capsnap->cap_flush.tid; |
| 1488 | last_tid = capsnap->cap_flush.tid; |
| 1489 | } |
| 1490 | |
| 1491 | ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS; |
| 1492 | |
| 1493 | while (first_tid <= last_tid) { |
| 1494 | struct ceph_cap *cap = ci->i_auth_cap; |
| 1495 | struct ceph_cap_flush *cf; |
| 1496 | int ret; |
| 1497 | |
| 1498 | if (!(cap && cap->session == session)) { |
| 1499 | dout("__flush_snaps %p auth cap %p not mds%d, " |
| 1500 | "stop\n", inode, cap, session->s_mds); |
| 1501 | break; |
| 1502 | } |
| 1503 | |
| 1504 | ret = -ENOENT; |
| 1505 | list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) { |
| 1506 | if (cf->tid >= first_tid) { |
| 1507 | ret = 0; |
| 1508 | break; |
| 1509 | } |
| 1510 | } |
| 1511 | if (ret < 0) |
| 1512 | break; |
| 1513 | |
| 1514 | first_tid = cf->tid + 1; |
| 1515 | |
| 1516 | capsnap = container_of(cf, struct ceph_cap_snap, cap_flush); |
Elena Reshetova | 805692d | 2017-03-03 11:15:07 +0200 | [diff] [blame] | 1517 | refcount_inc(&capsnap->nref); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1518 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1519 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1520 | dout("__flush_snaps %p capsnap %p tid %llu %s\n", |
| 1521 | inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1522 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1523 | ret = __send_flush_snap(inode, session, capsnap, cap->mseq, |
| 1524 | oldest_flush_tid); |
| 1525 | if (ret < 0) { |
| 1526 | pr_err("__flush_snaps: error sending cap flushsnap, " |
| 1527 | "ino (%llx.%llx) tid %llu follows %llu\n", |
| 1528 | ceph_vinop(inode), cf->tid, capsnap->follows); |
| 1529 | } |
| 1530 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1531 | ceph_put_cap_snap(capsnap); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1532 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | void ceph_flush_snaps(struct ceph_inode_info *ci, |
| 1537 | struct ceph_mds_session **psession) |
| 1538 | { |
| 1539 | struct inode *inode = &ci->vfs_inode; |
| 1540 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Yan, Zheng | e4d2b16 | 2016-08-04 08:43:33 +0800 | [diff] [blame] | 1541 | struct ceph_mds_session *session = NULL; |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1542 | int mds; |
Yan, Zheng | e4d2b16 | 2016-08-04 08:43:33 +0800 | [diff] [blame] | 1543 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1544 | dout("ceph_flush_snaps %p\n", inode); |
Yan, Zheng | e4d2b16 | 2016-08-04 08:43:33 +0800 | [diff] [blame] | 1545 | if (psession) |
| 1546 | session = *psession; |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1547 | retry: |
| 1548 | spin_lock(&ci->i_ceph_lock); |
| 1549 | if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) { |
| 1550 | dout(" no capsnap needs flush, doing nothing\n"); |
| 1551 | goto out; |
| 1552 | } |
| 1553 | if (!ci->i_auth_cap) { |
| 1554 | dout(" no auth cap (migrating?), doing nothing\n"); |
| 1555 | goto out; |
| 1556 | } |
| 1557 | |
| 1558 | mds = ci->i_auth_cap->session->s_mds; |
| 1559 | if (session && session->s_mds != mds) { |
| 1560 | dout(" oops, wrong session %p mutex\n", session); |
| 1561 | mutex_unlock(&session->s_mutex); |
| 1562 | ceph_put_mds_session(session); |
| 1563 | session = NULL; |
| 1564 | } |
| 1565 | if (!session) { |
| 1566 | spin_unlock(&ci->i_ceph_lock); |
| 1567 | mutex_lock(&mdsc->mutex); |
| 1568 | session = __ceph_lookup_mds_session(mdsc, mds); |
| 1569 | mutex_unlock(&mdsc->mutex); |
| 1570 | if (session) { |
| 1571 | dout(" inverting session/ino locks on %p\n", session); |
| 1572 | mutex_lock(&session->s_mutex); |
| 1573 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1574 | goto retry; |
| 1575 | } |
| 1576 | |
Yan, Zheng | 24d063a | 2017-08-15 11:37:32 +0800 | [diff] [blame] | 1577 | // make sure flushsnap messages are sent in proper order. |
| 1578 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) { |
| 1579 | __kick_flushing_caps(mdsc, session, ci, 0); |
| 1580 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
| 1581 | } |
| 1582 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1583 | __ceph_flush_snaps(ci, session); |
| 1584 | out: |
| 1585 | spin_unlock(&ci->i_ceph_lock); |
| 1586 | |
| 1587 | if (psession) { |
| 1588 | *psession = session; |
Yan, Zheng | c858a07 | 2017-08-28 15:02:42 +0800 | [diff] [blame] | 1589 | } else if (session) { |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1590 | mutex_unlock(&session->s_mutex); |
| 1591 | ceph_put_mds_session(session); |
| 1592 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1593 | /* we flushed them all; remove this inode from the queue */ |
| 1594 | spin_lock(&mdsc->snap_flush_lock); |
| 1595 | list_del_init(&ci->i_snap_flush_item); |
| 1596 | spin_unlock(&mdsc->snap_flush_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 1600 | * Mark caps dirty. If inode is newly dirty, return the dirty flags. |
| 1601 | * Caller is then responsible for calling __mark_inode_dirty with the |
| 1602 | * returned flags value. |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1603 | */ |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1604 | int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask, |
| 1605 | struct ceph_cap_flush **pcf) |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1606 | { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1607 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1608 | ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1609 | struct inode *inode = &ci->vfs_inode; |
| 1610 | int was = ci->i_dirty_caps; |
| 1611 | int dirty = 0; |
| 1612 | |
Yan, Zheng | 571ade3 | 2015-03-24 11:36:08 +0800 | [diff] [blame] | 1613 | if (!ci->i_auth_cap) { |
| 1614 | pr_warn("__mark_dirty_caps %p %llx mask %s, " |
| 1615 | "but no auth cap (session was closed?)\n", |
| 1616 | inode, ceph_ino(inode), ceph_cap_string(mask)); |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1620 | dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode, |
| 1621 | ceph_cap_string(mask), ceph_cap_string(was), |
| 1622 | ceph_cap_string(was | mask)); |
| 1623 | ci->i_dirty_caps |= mask; |
| 1624 | if (was == 0) { |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1625 | WARN_ON_ONCE(ci->i_prealloc_cap_flush); |
| 1626 | swap(ci->i_prealloc_cap_flush, *pcf); |
| 1627 | |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1628 | if (!ci->i_head_snapc) { |
| 1629 | WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem)); |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 1630 | ci->i_head_snapc = ceph_get_snap_context( |
| 1631 | ci->i_snap_realm->cached_context); |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1632 | } |
Yan, Zheng | 0685235 | 2012-11-19 10:49:07 +0800 | [diff] [blame] | 1633 | dout(" inode %p now dirty snapc %p auth cap %p\n", |
| 1634 | &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1635 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
| 1636 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 1637 | list_add(&ci->i_dirty_item, &mdsc->cap_dirty); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1638 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1639 | if (ci->i_flushing_caps == 0) { |
Sage Weil | 3772d26 | 2011-05-03 09:28:08 -0700 | [diff] [blame] | 1640 | ihold(inode); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1641 | dirty |= I_DIRTY_SYNC; |
| 1642 | } |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1643 | } else { |
| 1644 | WARN_ON_ONCE(!ci->i_prealloc_cap_flush); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1645 | } |
| 1646 | BUG_ON(list_empty(&ci->i_dirty_item)); |
| 1647 | if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && |
| 1648 | (mask & CEPH_CAP_FILE_BUFFER)) |
| 1649 | dirty |= I_DIRTY_DATASYNC; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1650 | __cap_delay_requeue(mdsc, ci); |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 1651 | return dirty; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1654 | struct ceph_cap_flush *ceph_alloc_cap_flush(void) |
| 1655 | { |
| 1656 | return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL); |
| 1657 | } |
| 1658 | |
| 1659 | void ceph_free_cap_flush(struct ceph_cap_flush *cf) |
| 1660 | { |
| 1661 | if (cf) |
| 1662 | kmem_cache_free(ceph_cap_flush_cachep, cf); |
| 1663 | } |
| 1664 | |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1665 | static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc) |
| 1666 | { |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1667 | if (!list_empty(&mdsc->cap_flush_list)) { |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1668 | struct ceph_cap_flush *cf = |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1669 | list_first_entry(&mdsc->cap_flush_list, |
| 1670 | struct ceph_cap_flush, g_list); |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1671 | return cf->tid; |
| 1672 | } |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1676 | /* |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 1677 | * Remove cap_flush from the mdsc's or inode's flushing cap list. |
| 1678 | * Return true if caller needs to wake up flush waiters. |
| 1679 | */ |
| 1680 | static bool __finish_cap_flush(struct ceph_mds_client *mdsc, |
| 1681 | struct ceph_inode_info *ci, |
| 1682 | struct ceph_cap_flush *cf) |
| 1683 | { |
| 1684 | struct ceph_cap_flush *prev; |
| 1685 | bool wake = cf->wake; |
| 1686 | if (mdsc) { |
| 1687 | /* are there older pending cap flushes? */ |
| 1688 | if (wake && cf->g_list.prev != &mdsc->cap_flush_list) { |
| 1689 | prev = list_prev_entry(cf, g_list); |
| 1690 | prev->wake = true; |
| 1691 | wake = false; |
| 1692 | } |
| 1693 | list_del(&cf->g_list); |
| 1694 | } else if (ci) { |
| 1695 | if (wake && cf->i_list.prev != &ci->i_cap_flush_list) { |
| 1696 | prev = list_prev_entry(cf, i_list); |
| 1697 | prev->wake = true; |
| 1698 | wake = false; |
| 1699 | } |
| 1700 | list_del(&cf->i_list); |
| 1701 | } else { |
| 1702 | BUG_ON(1); |
| 1703 | } |
| 1704 | return wake; |
| 1705 | } |
| 1706 | |
| 1707 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1708 | * Add dirty inode to the flushing list. Assigned a seq number so we |
| 1709 | * can wait for caps to flush without starving. |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1710 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1711 | * Called under i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1712 | */ |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1713 | static int __mark_caps_flushing(struct inode *inode, |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 1714 | struct ceph_mds_session *session, bool wake, |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1715 | u64 *flush_tid, u64 *oldest_flush_tid) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1716 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1717 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1718 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1719 | struct ceph_cap_flush *cf = NULL; |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1720 | int flushing; |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 1721 | |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1722 | BUG_ON(ci->i_dirty_caps == 0); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1723 | BUG_ON(list_empty(&ci->i_dirty_item)); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1724 | BUG_ON(!ci->i_prealloc_cap_flush); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1725 | |
| 1726 | flushing = ci->i_dirty_caps; |
| 1727 | dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n", |
| 1728 | ceph_cap_string(flushing), |
| 1729 | ceph_cap_string(ci->i_flushing_caps), |
| 1730 | ceph_cap_string(ci->i_flushing_caps | flushing)); |
| 1731 | ci->i_flushing_caps |= flushing; |
| 1732 | ci->i_dirty_caps = 0; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1733 | dout(" inode %p now !dirty\n", inode); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1734 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1735 | swap(cf, ci->i_prealloc_cap_flush); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1736 | cf->caps = flushing; |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 1737 | cf->wake = wake; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1738 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1739 | spin_lock(&mdsc->cap_dirty_lock); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1740 | list_del_init(&ci->i_dirty_item); |
| 1741 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1742 | cf->tid = ++mdsc->last_cap_flush_tid; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1743 | list_add_tail(&cf->g_list, &mdsc->cap_flush_list); |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1744 | *oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1745 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1746 | if (list_empty(&ci->i_flushing_item)) { |
| 1747 | list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); |
| 1748 | mdsc->num_cap_flushing++; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1749 | } |
| 1750 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1751 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1752 | list_add_tail(&cf->i_list, &ci->i_cap_flush_list); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1753 | |
| 1754 | *flush_tid = cf->tid; |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1755 | return flushing; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /* |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1759 | * try to invalidate mapping pages without blocking. |
| 1760 | */ |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1761 | static int try_nonblocking_invalidate(struct inode *inode) |
| 1762 | { |
| 1763 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1764 | u32 invalidating_gen = ci->i_rdcache_gen; |
| 1765 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1766 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1767 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1768 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1769 | |
Sage Weil | 18a3819 | 2010-09-17 10:46:44 -0700 | [diff] [blame] | 1770 | if (inode->i_data.nrpages == 0 && |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1771 | invalidating_gen == ci->i_rdcache_gen) { |
| 1772 | /* success. */ |
| 1773 | dout("try_nonblocking_invalidate %p success\n", inode); |
Sage Weil | cd045cb | 2010-11-04 11:05:05 -0700 | [diff] [blame] | 1774 | /* save any racing async invalidate some trouble */ |
| 1775 | ci->i_rdcache_revoking = ci->i_rdcache_gen - 1; |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1776 | return 0; |
| 1777 | } |
| 1778 | dout("try_nonblocking_invalidate %p failed\n", inode); |
| 1779 | return -1; |
| 1780 | } |
| 1781 | |
Yan, Zheng | efb0ca7 | 2017-05-22 12:03:32 +0800 | [diff] [blame] | 1782 | bool __ceph_should_report_size(struct ceph_inode_info *ci) |
| 1783 | { |
| 1784 | loff_t size = ci->vfs_inode.i_size; |
| 1785 | /* mds will adjust max size according to the reported size */ |
| 1786 | if (ci->i_flushing_caps & CEPH_CAP_FILE_WR) |
| 1787 | return false; |
| 1788 | if (size >= ci->i_max_size) |
| 1789 | return true; |
| 1790 | /* half of previous max_size increment has been used */ |
| 1791 | if (ci->i_max_size > ci->i_reported_size && |
| 1792 | (size << 1) >= ci->i_max_size + ci->i_reported_size) |
| 1793 | return true; |
| 1794 | return false; |
| 1795 | } |
| 1796 | |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1797 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1798 | * Swiss army knife function to examine currently used and wanted |
| 1799 | * versus held caps. Release, flush, ack revoked caps to mds as |
| 1800 | * appropriate. |
| 1801 | * |
| 1802 | * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay |
| 1803 | * cap release further. |
| 1804 | * CHECK_CAPS_AUTHONLY - we should only check the auth cap |
| 1805 | * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without |
| 1806 | * further delay. |
| 1807 | */ |
| 1808 | void ceph_check_caps(struct ceph_inode_info *ci, int flags, |
| 1809 | struct ceph_mds_session *session) |
| 1810 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1811 | struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); |
| 1812 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1813 | struct inode *inode = &ci->vfs_inode; |
| 1814 | struct ceph_cap *cap; |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1815 | u64 flush_tid, oldest_flush_tid; |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1816 | int file_wanted, used, cap_used; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1817 | int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */ |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1818 | int issued, implemented, want, retain, revoking, flushing = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1819 | int mds = -1; /* keep track of how far we've gone through i_caps list |
| 1820 | to avoid an infinite loop on retry */ |
| 1821 | struct rb_node *p; |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1822 | int delayed = 0, sent = 0; |
| 1823 | bool no_delay = flags & CHECK_CAPS_NODELAY; |
Yan, Zheng | 3609404 | 2016-07-06 15:37:42 +0800 | [diff] [blame] | 1824 | bool queue_invalidate = false; |
Yan, Zheng | 3609404 | 2016-07-06 15:37:42 +0800 | [diff] [blame] | 1825 | bool tried_invalidate = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1826 | |
| 1827 | /* if we are unmounting, flush any unused caps immediately. */ |
| 1828 | if (mdsc->stopping) |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1829 | no_delay = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1830 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1831 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1832 | |
| 1833 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
| 1834 | flags |= CHECK_CAPS_FLUSH; |
| 1835 | |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1836 | if (!(flags & CHECK_CAPS_AUTHONLY) || |
| 1837 | (ci->i_auth_cap && __ceph_is_single_caps(ci))) |
| 1838 | __cap_delay_cancel(mdsc, ci); |
| 1839 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1840 | goto retry_locked; |
| 1841 | retry: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1842 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1843 | retry_locked: |
| 1844 | file_wanted = __ceph_caps_file_wanted(ci); |
| 1845 | used = __ceph_caps_used(ci); |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1846 | issued = __ceph_caps_issued(ci, &implemented); |
| 1847 | revoking = implemented & ~issued; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1848 | |
Yan, Zheng | 4144599 | 2015-05-25 17:36:42 +0800 | [diff] [blame] | 1849 | want = file_wanted; |
| 1850 | retain = file_wanted | used | CEPH_CAP_PIN; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1851 | if (!mdsc->stopping && inode->i_nlink > 0) { |
Yan, Zheng | 4144599 | 2015-05-25 17:36:42 +0800 | [diff] [blame] | 1852 | if (file_wanted) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1853 | retain |= CEPH_CAP_ANY; /* be greedy */ |
Yan, Zheng | 32ec439 | 2015-03-26 19:06:00 +0800 | [diff] [blame] | 1854 | } else if (S_ISDIR(inode->i_mode) && |
| 1855 | (issued & CEPH_CAP_FILE_SHARED) && |
| 1856 | __ceph_dir_is_complete(ci)) { |
| 1857 | /* |
| 1858 | * If a directory is complete, we want to keep |
| 1859 | * the exclusive cap. So that MDS does not end up |
| 1860 | * revoking the shared cap on every create/unlink |
| 1861 | * operation. |
| 1862 | */ |
| 1863 | want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL; |
| 1864 | retain |= want; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1865 | } else { |
Yan, Zheng | 32ec439 | 2015-03-26 19:06:00 +0800 | [diff] [blame] | 1866 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1867 | retain |= CEPH_CAP_ANY_SHARED; |
| 1868 | /* |
| 1869 | * keep RD only if we didn't have the file open RW, |
| 1870 | * because then the mds would revoke it anyway to |
| 1871 | * journal max_size=0. |
| 1872 | */ |
| 1873 | if (ci->i_max_size == 0) |
| 1874 | retain |= CEPH_CAP_ANY_RD; |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | dout("check_caps %p file_want %s used %s dirty %s flushing %s" |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1879 | " issued %s revoking %s retain %s %s%s%s\n", inode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1880 | ceph_cap_string(file_wanted), |
| 1881 | ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), |
| 1882 | ceph_cap_string(ci->i_flushing_caps), |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1883 | ceph_cap_string(issued), ceph_cap_string(revoking), |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1884 | ceph_cap_string(retain), |
| 1885 | (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", |
| 1886 | (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "", |
| 1887 | (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : ""); |
| 1888 | |
| 1889 | /* |
| 1890 | * If we no longer need to hold onto old our caps, and we may |
| 1891 | * have cached pages, but don't want them, then try to invalidate. |
| 1892 | * If we fail, it's because pages are locked.... try again later. |
| 1893 | */ |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1894 | if ((!no_delay || mdsc->stopping) && |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1895 | !S_ISDIR(inode->i_mode) && /* ignore readdir cache */ |
Yan, Zheng | 9abd4db | 2016-05-18 20:58:26 +0800 | [diff] [blame] | 1896 | !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */ |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1897 | inode->i_data.nrpages && /* have cached pages */ |
Yan, Zheng | 5e804ac | 2015-10-26 16:08:43 +0800 | [diff] [blame] | 1898 | (revoking & (CEPH_CAP_FILE_CACHE| |
| 1899 | CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1900 | !tried_invalidate) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1901 | dout("check_caps trying to invalidate on %p\n", inode); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1902 | if (try_nonblocking_invalidate(inode) < 0) { |
Yan, Zheng | ee612d9 | 2018-01-08 14:36:25 +0800 | [diff] [blame] | 1903 | dout("check_caps queuing invalidate\n"); |
| 1904 | queue_invalidate = true; |
| 1905 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1906 | } |
Yan, Zheng | 3609404 | 2016-07-06 15:37:42 +0800 | [diff] [blame] | 1907 | tried_invalidate = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1908 | goto retry_locked; |
| 1909 | } |
| 1910 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1911 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 1912 | cap = rb_entry(p, struct ceph_cap, ci_node); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1913 | |
| 1914 | /* avoid looping forever */ |
| 1915 | if (mds >= cap->mds || |
| 1916 | ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) |
| 1917 | continue; |
| 1918 | |
| 1919 | /* NOTE: no side-effects allowed, until we take s_mutex */ |
| 1920 | |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1921 | cap_used = used; |
| 1922 | if (ci->i_auth_cap && cap != ci->i_auth_cap) |
| 1923 | cap_used &= ~ci->i_auth_cap->issued; |
| 1924 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1925 | revoking = cap->implemented & ~cap->issued; |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1926 | dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n", |
Yan, Zheng | 9abd4db | 2016-05-18 20:58:26 +0800 | [diff] [blame] | 1927 | cap->mds, cap, ceph_cap_string(cap_used), |
| 1928 | ceph_cap_string(cap->issued), |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1929 | ceph_cap_string(cap->implemented), |
| 1930 | ceph_cap_string(revoking)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1931 | |
| 1932 | if (cap == ci->i_auth_cap && |
| 1933 | (cap->issued & CEPH_CAP_FILE_WR)) { |
| 1934 | /* request larger max_size from MDS? */ |
| 1935 | if (ci->i_wanted_max_size > ci->i_max_size && |
| 1936 | ci->i_wanted_max_size > ci->i_requested_max_size) { |
| 1937 | dout("requesting new max_size\n"); |
| 1938 | goto ack; |
| 1939 | } |
| 1940 | |
| 1941 | /* approaching file_max? */ |
Yan, Zheng | efb0ca7 | 2017-05-22 12:03:32 +0800 | [diff] [blame] | 1942 | if (__ceph_should_report_size(ci)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1943 | dout("i_size approaching max_size\n"); |
| 1944 | goto ack; |
| 1945 | } |
| 1946 | } |
| 1947 | /* flush anything dirty? */ |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 1948 | if (cap == ci->i_auth_cap) { |
| 1949 | if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) { |
| 1950 | dout("flushing dirty caps\n"); |
| 1951 | goto ack; |
| 1952 | } |
| 1953 | if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) { |
| 1954 | dout("flushing snap caps\n"); |
| 1955 | goto ack; |
| 1956 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | /* completed revocation? going down and there are no caps? */ |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1960 | if (revoking && (revoking & cap_used) == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1961 | dout("completed revocation of %s\n", |
| 1962 | ceph_cap_string(cap->implemented & ~cap->issued)); |
| 1963 | goto ack; |
| 1964 | } |
| 1965 | |
| 1966 | /* want more caps from mds? */ |
| 1967 | if (want & ~(cap->mds_wanted | cap->issued)) |
| 1968 | goto ack; |
| 1969 | |
| 1970 | /* things we might delay */ |
| 1971 | if ((cap->issued & ~retain) == 0 && |
| 1972 | cap->mds_wanted == want) |
| 1973 | continue; /* nope, all good */ |
| 1974 | |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1975 | if (no_delay) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1976 | goto ack; |
| 1977 | |
| 1978 | /* delay? */ |
| 1979 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && |
| 1980 | time_before(jiffies, ci->i_hold_caps_max)) { |
| 1981 | dout(" delaying issued %s -> %s, wanted %s -> %s\n", |
| 1982 | ceph_cap_string(cap->issued), |
| 1983 | ceph_cap_string(cap->issued & retain), |
| 1984 | ceph_cap_string(cap->mds_wanted), |
| 1985 | ceph_cap_string(want)); |
| 1986 | delayed++; |
| 1987 | continue; |
| 1988 | } |
| 1989 | |
| 1990 | ack: |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 1991 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
| 1992 | dout(" skipping %p I_NOFLUSH set\n", inode); |
| 1993 | continue; |
| 1994 | } |
| 1995 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1996 | if (session && session != cap->session) { |
| 1997 | dout("oops, wrong session %p mutex\n", session); |
| 1998 | mutex_unlock(&session->s_mutex); |
| 1999 | session = NULL; |
| 2000 | } |
| 2001 | if (!session) { |
| 2002 | session = cap->session; |
| 2003 | if (mutex_trylock(&session->s_mutex) == 0) { |
| 2004 | dout("inverting session/ino locks on %p\n", |
| 2005 | session); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2006 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2007 | if (took_snap_rwsem) { |
| 2008 | up_read(&mdsc->snap_rwsem); |
| 2009 | took_snap_rwsem = 0; |
| 2010 | } |
| 2011 | mutex_lock(&session->s_mutex); |
| 2012 | goto retry; |
| 2013 | } |
| 2014 | } |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 2015 | |
| 2016 | /* kick flushing and flush snaps before sending normal |
| 2017 | * cap message */ |
| 2018 | if (cap == ci->i_auth_cap && |
| 2019 | (ci->i_ceph_flags & |
| 2020 | (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) { |
| 2021 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) { |
Yan, Zheng | 24d063a | 2017-08-15 11:37:32 +0800 | [diff] [blame] | 2022 | __kick_flushing_caps(mdsc, session, ci, 0); |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 2023 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
| 2024 | } |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 2025 | if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) |
| 2026 | __ceph_flush_snaps(ci, session); |
| 2027 | |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 2028 | goto retry_locked; |
| 2029 | } |
| 2030 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2031 | /* take snap_rwsem after session mutex */ |
| 2032 | if (!took_snap_rwsem) { |
| 2033 | if (down_read_trylock(&mdsc->snap_rwsem) == 0) { |
| 2034 | dout("inverting snap/in locks on %p\n", |
| 2035 | inode); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2036 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2037 | down_read(&mdsc->snap_rwsem); |
| 2038 | took_snap_rwsem = 1; |
| 2039 | goto retry; |
| 2040 | } |
| 2041 | took_snap_rwsem = 1; |
| 2042 | } |
| 2043 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2044 | if (cap == ci->i_auth_cap && ci->i_dirty_caps) { |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 2045 | flushing = __mark_caps_flushing(inode, session, false, |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 2046 | &flush_tid, |
| 2047 | &oldest_flush_tid); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2048 | } else { |
Sage Weil | 24be0c4 | 2011-01-18 08:48:06 -0800 | [diff] [blame] | 2049 | flushing = 0; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2050 | flush_tid = 0; |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 2051 | spin_lock(&mdsc->cap_dirty_lock); |
| 2052 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
| 2053 | spin_unlock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2054 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2055 | |
| 2056 | mds = cap->mds; /* remember mds, so we don't repeat */ |
| 2057 | sent++; |
| 2058 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2059 | /* __send_cap drops i_ceph_lock */ |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 2060 | delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false, |
| 2061 | cap_used, want, retain, flushing, |
| 2062 | flush_tid, oldest_flush_tid); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2063 | goto retry; /* retake i_ceph_lock and restart our cap scan. */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 2066 | /* Reschedule delayed caps release if we delayed anything */ |
| 2067 | if (delayed) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2068 | __cap_delay_requeue(mdsc, ci); |
| 2069 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2070 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2071 | |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 2072 | if (queue_invalidate) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2073 | ceph_queue_invalidate(inode); |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 2074 | |
Sage Weil | cdc2ce0 | 2010-03-16 13:39:28 -0700 | [diff] [blame] | 2075 | if (session) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2076 | mutex_unlock(&session->s_mutex); |
| 2077 | if (took_snap_rwsem) |
| 2078 | up_read(&mdsc->snap_rwsem); |
| 2079 | } |
| 2080 | |
| 2081 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2082 | * Try to flush dirty caps back to the auth mds. |
| 2083 | */ |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2084 | static int try_flush_caps(struct inode *inode, u64 *ptid) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2085 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2086 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2087 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 4fe5978 | 2013-10-31 16:44:14 +0800 | [diff] [blame] | 2088 | struct ceph_mds_session *session = NULL; |
Yan, Zheng | 89b52fe | 2015-05-27 09:59:48 +0800 | [diff] [blame] | 2089 | int flushing = 0; |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 2090 | u64 flush_tid = 0, oldest_flush_tid = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2091 | |
| 2092 | retry: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2093 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 2094 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
Jeff Layton | 6c2838f | 2017-10-19 08:52:58 -0400 | [diff] [blame] | 2095 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 2096 | dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode); |
| 2097 | goto out; |
| 2098 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2099 | if (ci->i_dirty_caps && ci->i_auth_cap) { |
| 2100 | struct ceph_cap *cap = ci->i_auth_cap; |
| 2101 | int used = __ceph_caps_used(ci); |
| 2102 | int want = __ceph_caps_wanted(ci); |
| 2103 | int delayed; |
| 2104 | |
Yan, Zheng | 4fe5978 | 2013-10-31 16:44:14 +0800 | [diff] [blame] | 2105 | if (!session || session != cap->session) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2106 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 4fe5978 | 2013-10-31 16:44:14 +0800 | [diff] [blame] | 2107 | if (session) |
| 2108 | mutex_unlock(&session->s_mutex); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2109 | session = cap->session; |
| 2110 | mutex_lock(&session->s_mutex); |
| 2111 | goto retry; |
| 2112 | } |
Jeff Layton | 6c2838f | 2017-10-19 08:52:58 -0400 | [diff] [blame] | 2113 | if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) { |
| 2114 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2115 | goto out; |
Jeff Layton | 6c2838f | 2017-10-19 08:52:58 -0400 | [diff] [blame] | 2116 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2117 | |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 2118 | flushing = __mark_caps_flushing(inode, session, true, |
| 2119 | &flush_tid, &oldest_flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2120 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2121 | /* __send_cap drops i_ceph_lock */ |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 2122 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true, |
| 2123 | used, want, (cap->issued | cap->implemented), |
| 2124 | flushing, flush_tid, oldest_flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2125 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2126 | if (delayed) { |
| 2127 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 89b52fe | 2015-05-27 09:59:48 +0800 | [diff] [blame] | 2128 | __cap_delay_requeue(mdsc, ci); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2129 | spin_unlock(&ci->i_ceph_lock); |
| 2130 | } |
| 2131 | } else { |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2132 | if (!list_empty(&ci->i_cap_flush_list)) { |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2133 | struct ceph_cap_flush *cf = |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2134 | list_last_entry(&ci->i_cap_flush_list, |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 2135 | struct ceph_cap_flush, i_list); |
| 2136 | cf->wake = true; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2137 | flush_tid = cf->tid; |
| 2138 | } |
| 2139 | flushing = ci->i_flushing_caps; |
| 2140 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2141 | } |
| 2142 | out: |
Yan, Zheng | 4fe5978 | 2013-10-31 16:44:14 +0800 | [diff] [blame] | 2143 | if (session) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2144 | mutex_unlock(&session->s_mutex); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2145 | |
| 2146 | *ptid = flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2147 | return flushing; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * Return true if we've flushed caps through the given flush_tid. |
| 2152 | */ |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2153 | static int caps_are_flushed(struct inode *inode, u64 flush_tid) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2154 | { |
| 2155 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2156 | int ret = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2157 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2158 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2159 | if (!list_empty(&ci->i_cap_flush_list)) { |
| 2160 | struct ceph_cap_flush * cf = |
| 2161 | list_first_entry(&ci->i_cap_flush_list, |
| 2162 | struct ceph_cap_flush, i_list); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2163 | if (cf->tid <= flush_tid) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2164 | ret = 0; |
Yan, Zheng | 89b52fe | 2015-05-27 09:59:48 +0800 | [diff] [blame] | 2165 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2166 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2167 | return ret; |
| 2168 | } |
| 2169 | |
| 2170 | /* |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2171 | * wait for any unsafe requests to complete. |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2172 | */ |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2173 | static int unsafe_request_wait(struct inode *inode) |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2174 | { |
| 2175 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2176 | struct ceph_mds_request *req1 = NULL, *req2 = NULL; |
| 2177 | int ret, err = 0; |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2178 | |
| 2179 | spin_lock(&ci->i_unsafe_lock); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2180 | if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) { |
| 2181 | req1 = list_last_entry(&ci->i_unsafe_dirops, |
| 2182 | struct ceph_mds_request, |
| 2183 | r_unsafe_dir_item); |
| 2184 | ceph_mdsc_get_request(req1); |
| 2185 | } |
| 2186 | if (!list_empty(&ci->i_unsafe_iops)) { |
| 2187 | req2 = list_last_entry(&ci->i_unsafe_iops, |
| 2188 | struct ceph_mds_request, |
| 2189 | r_unsafe_target_item); |
| 2190 | ceph_mdsc_get_request(req2); |
| 2191 | } |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2192 | spin_unlock(&ci->i_unsafe_lock); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2193 | |
Jeff Layton | 4945a08 | 2016-11-16 09:45:22 -0500 | [diff] [blame] | 2194 | dout("unsafe_request_wait %p wait on tid %llu %llu\n", |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2195 | inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL); |
| 2196 | if (req1) { |
| 2197 | ret = !wait_for_completion_timeout(&req1->r_safe_completion, |
| 2198 | ceph_timeout_jiffies(req1->r_timeout)); |
| 2199 | if (ret) |
| 2200 | err = -EIO; |
| 2201 | ceph_mdsc_put_request(req1); |
| 2202 | } |
| 2203 | if (req2) { |
| 2204 | ret = !wait_for_completion_timeout(&req2->r_safe_completion, |
| 2205 | ceph_timeout_jiffies(req2->r_timeout)); |
| 2206 | if (ret) |
| 2207 | err = -EIO; |
| 2208 | ceph_mdsc_put_request(req2); |
| 2209 | } |
| 2210 | return err; |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2211 | } |
| 2212 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2213 | int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2214 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 2215 | struct inode *inode = file->f_mapping->host; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2216 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2217 | u64 flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2218 | int ret; |
| 2219 | int dirty; |
| 2220 | |
| 2221 | dout("fsync %p%s\n", inode, datasync ? " datasync" : ""); |
Yan, Zheng | 9a5530c | 2016-06-15 16:29:18 +0800 | [diff] [blame] | 2222 | |
Jeff Layton | b74fcea | 2017-07-25 10:50:41 -0400 | [diff] [blame] | 2223 | ret = file_write_and_wait_range(file, start, end); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2224 | if (ret < 0) |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2225 | goto out; |
| 2226 | |
| 2227 | if (datasync) |
| 2228 | goto out; |
| 2229 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2230 | inode_lock(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2231 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2232 | dirty = try_flush_caps(inode, &flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2233 | dout("fsync dirty caps are %s\n", ceph_cap_string(dirty)); |
| 2234 | |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2235 | ret = unsafe_request_wait(inode); |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2236 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2237 | /* |
| 2238 | * only wait on non-file metadata writeback (the mds |
| 2239 | * can recover size and mtime, so we don't need to |
| 2240 | * wait for that) |
| 2241 | */ |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2242 | if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2243 | ret = wait_event_interruptible(ci->i_cap_wq, |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2244 | caps_are_flushed(inode, flush_tid)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2245 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2246 | inode_unlock(inode); |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2247 | out: |
| 2248 | dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2249 | return ret; |
| 2250 | } |
| 2251 | |
| 2252 | /* |
| 2253 | * Flush any dirty caps back to the mds. If we aren't asked to wait, |
| 2254 | * queue inode for flush but don't do so immediately, because we can |
| 2255 | * get by with fewer MDS messages if we wait for data writeback to |
| 2256 | * complete first. |
| 2257 | */ |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 2258 | int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2259 | { |
| 2260 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2261 | u64 flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2262 | int err = 0; |
| 2263 | int dirty; |
Chengguang Xu | 16515a6 | 2018-01-30 10:02:30 +0800 | [diff] [blame] | 2264 | int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2265 | |
| 2266 | dout("write_inode %p wait=%d\n", inode, wait); |
| 2267 | if (wait) { |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2268 | dirty = try_flush_caps(inode, &flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2269 | if (dirty) |
| 2270 | err = wait_event_interruptible(ci->i_cap_wq, |
| 2271 | caps_are_flushed(inode, flush_tid)); |
| 2272 | } else { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 2273 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2274 | ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2275 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2276 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2277 | if (__ceph_caps_dirty(ci)) |
| 2278 | __cap_delay_requeue_front(mdsc, ci); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2279 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2280 | } |
| 2281 | return err; |
| 2282 | } |
| 2283 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2284 | static void __kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 2285 | struct ceph_mds_session *session, |
| 2286 | struct ceph_inode_info *ci, |
| 2287 | u64 oldest_flush_tid) |
| 2288 | __releases(ci->i_ceph_lock) |
| 2289 | __acquires(ci->i_ceph_lock) |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2290 | { |
| 2291 | struct inode *inode = &ci->vfs_inode; |
| 2292 | struct ceph_cap *cap; |
| 2293 | struct ceph_cap_flush *cf; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2294 | int ret; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2295 | u64 first_tid = 0; |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 2296 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2297 | list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) { |
| 2298 | if (cf->tid < first_tid) |
| 2299 | continue; |
| 2300 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2301 | cap = ci->i_auth_cap; |
| 2302 | if (!(cap && cap->session == session)) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2303 | pr_err("%p auth cap %p not mds%d ???\n", |
| 2304 | inode, cap, session->s_mds); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2305 | break; |
| 2306 | } |
| 2307 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2308 | first_tid = cf->tid + 1; |
| 2309 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2310 | if (cf->caps) { |
| 2311 | dout("kick_flushing_caps %p cap %p tid %llu %s\n", |
| 2312 | inode, cap, cf->tid, ceph_cap_string(cf->caps)); |
| 2313 | ci->i_ceph_flags |= CEPH_I_NODELAY; |
| 2314 | ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 2315 | false, __ceph_caps_used(ci), |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2316 | __ceph_caps_wanted(ci), |
| 2317 | cap->issued | cap->implemented, |
| 2318 | cf->caps, cf->tid, oldest_flush_tid); |
| 2319 | if (ret) { |
| 2320 | pr_err("kick_flushing_caps: error sending " |
| 2321 | "cap flush, ino (%llx.%llx) " |
| 2322 | "tid %llu flushing %s\n", |
| 2323 | ceph_vinop(inode), cf->tid, |
| 2324 | ceph_cap_string(cf->caps)); |
| 2325 | } |
| 2326 | } else { |
| 2327 | struct ceph_cap_snap *capsnap = |
| 2328 | container_of(cf, struct ceph_cap_snap, |
| 2329 | cap_flush); |
| 2330 | dout("kick_flushing_caps %p capsnap %p tid %llu %s\n", |
| 2331 | inode, capsnap, cf->tid, |
| 2332 | ceph_cap_string(capsnap->dirty)); |
| 2333 | |
Elena Reshetova | 805692d | 2017-03-03 11:15:07 +0200 | [diff] [blame] | 2334 | refcount_inc(&capsnap->nref); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2335 | spin_unlock(&ci->i_ceph_lock); |
| 2336 | |
| 2337 | ret = __send_flush_snap(inode, session, capsnap, cap->mseq, |
| 2338 | oldest_flush_tid); |
| 2339 | if (ret < 0) { |
| 2340 | pr_err("kick_flushing_caps: error sending " |
| 2341 | "cap flushsnap, ino (%llx.%llx) " |
| 2342 | "tid %llu follows %llu\n", |
| 2343 | ceph_vinop(inode), cf->tid, |
| 2344 | capsnap->follows); |
| 2345 | } |
| 2346 | |
| 2347 | ceph_put_cap_snap(capsnap); |
| 2348 | } |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2349 | |
| 2350 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2351 | } |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2352 | } |
| 2353 | |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2354 | void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 2355 | struct ceph_mds_session *session) |
| 2356 | { |
| 2357 | struct ceph_inode_info *ci; |
| 2358 | struct ceph_cap *cap; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2359 | u64 oldest_flush_tid; |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2360 | |
| 2361 | dout("early_kick_flushing_caps mds%d\n", session->s_mds); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2362 | |
| 2363 | spin_lock(&mdsc->cap_dirty_lock); |
| 2364 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
| 2365 | spin_unlock(&mdsc->cap_dirty_lock); |
| 2366 | |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2367 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { |
| 2368 | spin_lock(&ci->i_ceph_lock); |
| 2369 | cap = ci->i_auth_cap; |
| 2370 | if (!(cap && cap->session == session)) { |
| 2371 | pr_err("%p auth cap %p not mds%d ???\n", |
| 2372 | &ci->vfs_inode, cap, session->s_mds); |
| 2373 | spin_unlock(&ci->i_ceph_lock); |
| 2374 | continue; |
| 2375 | } |
| 2376 | |
| 2377 | |
| 2378 | /* |
| 2379 | * if flushing caps were revoked, we re-send the cap flush |
| 2380 | * in client reconnect stage. This guarantees MDS * processes |
| 2381 | * the cap flush message before issuing the flushing caps to |
| 2382 | * other client. |
| 2383 | */ |
| 2384 | if ((cap->issued & ci->i_flushing_caps) != |
| 2385 | ci->i_flushing_caps) { |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2386 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2387 | __kick_flushing_caps(mdsc, session, ci, |
| 2388 | oldest_flush_tid); |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2389 | } else { |
| 2390 | ci->i_ceph_flags |= CEPH_I_KICK_FLUSH; |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2391 | } |
| 2392 | |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2393 | spin_unlock(&ci->i_ceph_lock); |
| 2394 | } |
| 2395 | } |
| 2396 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2397 | void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 2398 | struct ceph_mds_session *session) |
| 2399 | { |
| 2400 | struct ceph_inode_info *ci; |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2401 | struct ceph_cap *cap; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2402 | u64 oldest_flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2403 | |
| 2404 | dout("kick_flushing_caps mds%d\n", session->s_mds); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2405 | |
| 2406 | spin_lock(&mdsc->cap_dirty_lock); |
| 2407 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
| 2408 | spin_unlock(&mdsc->cap_dirty_lock); |
| 2409 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2410 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2411 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2412 | cap = ci->i_auth_cap; |
| 2413 | if (!(cap && cap->session == session)) { |
| 2414 | pr_err("%p auth cap %p not mds%d ???\n", |
| 2415 | &ci->vfs_inode, cap, session->s_mds); |
| 2416 | spin_unlock(&ci->i_ceph_lock); |
| 2417 | continue; |
| 2418 | } |
| 2419 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) { |
| 2420 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
| 2421 | __kick_flushing_caps(mdsc, session, ci, |
| 2422 | oldest_flush_tid); |
| 2423 | } |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2424 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2425 | } |
| 2426 | } |
| 2427 | |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2428 | static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc, |
| 2429 | struct ceph_mds_session *session, |
| 2430 | struct inode *inode) |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2431 | __releases(ci->i_ceph_lock) |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2432 | { |
| 2433 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2434 | struct ceph_cap *cap; |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2435 | |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2436 | cap = ci->i_auth_cap; |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 2437 | dout("kick_flushing_inode_caps %p flushing %s\n", inode, |
| 2438 | ceph_cap_string(ci->i_flushing_caps)); |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 2439 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2440 | if (!list_empty(&ci->i_cap_flush_list)) { |
| 2441 | u64 oldest_flush_tid; |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 2442 | spin_lock(&mdsc->cap_dirty_lock); |
| 2443 | list_move_tail(&ci->i_flushing_item, |
| 2444 | &cap->session->s_cap_flushing); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2445 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 2446 | spin_unlock(&mdsc->cap_dirty_lock); |
| 2447 | |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2448 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2449 | __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2450 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2451 | } else { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2452 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2456 | |
| 2457 | /* |
| 2458 | * Take references to capabilities we hold, so that we don't release |
| 2459 | * them to the MDS prematurely. |
| 2460 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2461 | * Protected by i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2462 | */ |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2463 | static void __take_cap_refs(struct ceph_inode_info *ci, int got, |
| 2464 | bool snap_rwsem_locked) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2465 | { |
| 2466 | if (got & CEPH_CAP_PIN) |
| 2467 | ci->i_pin_ref++; |
| 2468 | if (got & CEPH_CAP_FILE_RD) |
| 2469 | ci->i_rd_ref++; |
| 2470 | if (got & CEPH_CAP_FILE_CACHE) |
| 2471 | ci->i_rdcache_ref++; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2472 | if (got & CEPH_CAP_FILE_WR) { |
| 2473 | if (ci->i_wr_ref == 0 && !ci->i_head_snapc) { |
| 2474 | BUG_ON(!snap_rwsem_locked); |
| 2475 | ci->i_head_snapc = ceph_get_snap_context( |
| 2476 | ci->i_snap_realm->cached_context); |
| 2477 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2478 | ci->i_wr_ref++; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2479 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2480 | if (got & CEPH_CAP_FILE_BUFFER) { |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2481 | if (ci->i_wb_ref == 0) |
Sage Weil | 3772d26 | 2011-05-03 09:28:08 -0700 | [diff] [blame] | 2482 | ihold(&ci->vfs_inode); |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2483 | ci->i_wb_ref++; |
| 2484 | dout("__take_cap_refs %p wb %d -> %d (?)\n", |
| 2485 | &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | /* |
| 2490 | * Try to grab cap references. Specify those refs we @want, and the |
| 2491 | * minimal set we @need. Also include the larger offset we are writing |
| 2492 | * to (when applicable), and check against max_size here as well. |
| 2493 | * Note that caller is responsible for ensuring max_size increases are |
| 2494 | * requested from the MDS. |
| 2495 | */ |
| 2496 | static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2497 | loff_t endoff, bool nonblock, int *got, int *err) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2498 | { |
| 2499 | struct inode *inode = &ci->vfs_inode; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2500 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2501 | int ret = 0; |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2502 | int have, implemented; |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 2503 | int file_wanted; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2504 | bool snap_rwsem_locked = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2505 | |
| 2506 | dout("get_cap_refs %p need %s want %s\n", inode, |
| 2507 | ceph_cap_string(need), ceph_cap_string(want)); |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2508 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2509 | again: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2510 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2511 | |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 2512 | /* make sure file is actually open */ |
| 2513 | file_wanted = __ceph_caps_file_wanted(ci); |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2514 | if ((file_wanted & need) != need) { |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 2515 | dout("try_get_cap_refs need %s file_wanted %s, EBADF\n", |
| 2516 | ceph_cap_string(need), ceph_cap_string(file_wanted)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2517 | *err = -EBADF; |
| 2518 | ret = 1; |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2519 | goto out_unlock; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2520 | } |
| 2521 | |
Yan, Zheng | 37505d5 | 2013-04-12 16:11:10 +0800 | [diff] [blame] | 2522 | /* finish pending truncate */ |
| 2523 | while (ci->i_truncate_pending) { |
| 2524 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2525 | if (snap_rwsem_locked) { |
| 2526 | up_read(&mdsc->snap_rwsem); |
| 2527 | snap_rwsem_locked = false; |
| 2528 | } |
Yan, Zheng | b415bf4 | 2013-07-02 12:40:19 +0800 | [diff] [blame] | 2529 | __ceph_do_pending_vmtruncate(inode); |
Yan, Zheng | 37505d5 | 2013-04-12 16:11:10 +0800 | [diff] [blame] | 2530 | spin_lock(&ci->i_ceph_lock); |
| 2531 | } |
| 2532 | |
Yan, Zheng | 3871cbb | 2013-08-05 14:10:29 +0800 | [diff] [blame] | 2533 | have = __ceph_caps_issued(ci, &implemented); |
| 2534 | |
| 2535 | if (have & need & CEPH_CAP_FILE_WR) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2536 | if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { |
| 2537 | dout("get_cap_refs %p endoff %llu > maxsize %llu\n", |
| 2538 | inode, endoff, ci->i_max_size); |
Yan, Zheng | 3871cbb | 2013-08-05 14:10:29 +0800 | [diff] [blame] | 2539 | if (endoff > ci->i_requested_max_size) { |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2540 | *err = -EAGAIN; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2541 | ret = 1; |
| 2542 | } |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2543 | goto out_unlock; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2544 | } |
| 2545 | /* |
| 2546 | * If a sync write is in progress, we must wait, so that we |
| 2547 | * can get a final snapshot value for size+mtime. |
| 2548 | */ |
| 2549 | if (__ceph_have_pending_cap_snap(ci)) { |
| 2550 | dout("get_cap_refs %p cap_snap_pending\n", inode); |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2551 | goto out_unlock; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2552 | } |
| 2553 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2554 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2555 | if ((have & need) == need) { |
| 2556 | /* |
| 2557 | * Look at (implemented & ~have & not) so that we keep waiting |
| 2558 | * on transition from wanted -> needed caps. This is needed |
| 2559 | * for WRBUFFER|WR -> WR to avoid a new WR sync write from |
| 2560 | * going before a prior buffered writeback happens. |
| 2561 | */ |
| 2562 | int not = want & ~(have & need); |
| 2563 | int revoking = implemented & ~have; |
| 2564 | dout("get_cap_refs %p have %s but not %s (revoking %s)\n", |
| 2565 | inode, ceph_cap_string(have), ceph_cap_string(not), |
| 2566 | ceph_cap_string(revoking)); |
| 2567 | if ((revoking & not) == 0) { |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2568 | if (!snap_rwsem_locked && |
| 2569 | !ci->i_head_snapc && |
| 2570 | (need & CEPH_CAP_FILE_WR)) { |
| 2571 | if (!down_read_trylock(&mdsc->snap_rwsem)) { |
| 2572 | /* |
| 2573 | * we can not call down_read() when |
| 2574 | * task isn't in TASK_RUNNING state |
| 2575 | */ |
| 2576 | if (nonblock) { |
| 2577 | *err = -EAGAIN; |
| 2578 | ret = 1; |
| 2579 | goto out_unlock; |
| 2580 | } |
| 2581 | |
| 2582 | spin_unlock(&ci->i_ceph_lock); |
| 2583 | down_read(&mdsc->snap_rwsem); |
| 2584 | snap_rwsem_locked = true; |
| 2585 | goto again; |
| 2586 | } |
| 2587 | snap_rwsem_locked = true; |
| 2588 | } |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2589 | *got = need | (have & want); |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 2590 | if ((need & CEPH_CAP_FILE_RD) && |
| 2591 | !(*got & CEPH_CAP_FILE_CACHE)) |
| 2592 | ceph_disable_fscache_readpage(ci); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2593 | __take_cap_refs(ci, *got, true); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2594 | ret = 1; |
| 2595 | } |
| 2596 | } else { |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 2597 | int session_readonly = false; |
| 2598 | if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) { |
| 2599 | struct ceph_mds_session *s = ci->i_auth_cap->session; |
| 2600 | spin_lock(&s->s_cap_lock); |
| 2601 | session_readonly = s->s_readonly; |
| 2602 | spin_unlock(&s->s_cap_lock); |
| 2603 | } |
| 2604 | if (session_readonly) { |
| 2605 | dout("get_cap_refs %p needed %s but mds%d readonly\n", |
| 2606 | inode, ceph_cap_string(need), ci->i_auth_cap->mds); |
| 2607 | *err = -EROFS; |
| 2608 | ret = 1; |
| 2609 | goto out_unlock; |
| 2610 | } |
| 2611 | |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2612 | if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) { |
| 2613 | int mds_wanted; |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 2614 | if (READ_ONCE(mdsc->fsc->mount_state) == |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2615 | CEPH_MOUNT_SHUTDOWN) { |
| 2616 | dout("get_cap_refs %p forced umount\n", inode); |
| 2617 | *err = -EIO; |
| 2618 | ret = 1; |
| 2619 | goto out_unlock; |
| 2620 | } |
Yan, Zheng | c1944fe | 2017-01-29 22:15:47 +0800 | [diff] [blame] | 2621 | mds_wanted = __ceph_caps_mds_wanted(ci, false); |
Yan, Zheng | eb65b91 | 2017-01-12 17:18:00 +0800 | [diff] [blame] | 2622 | if (need & ~(mds_wanted & need)) { |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2623 | dout("get_cap_refs %p caps were dropped" |
| 2624 | " (session killed?)\n", inode); |
| 2625 | *err = -ESTALE; |
| 2626 | ret = 1; |
| 2627 | goto out_unlock; |
| 2628 | } |
Yan, Zheng | eb65b91 | 2017-01-12 17:18:00 +0800 | [diff] [blame] | 2629 | if (!(file_wanted & ~mds_wanted)) |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2630 | ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED; |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 2631 | } |
| 2632 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2633 | dout("get_cap_refs %p have %s needed %s\n", inode, |
| 2634 | ceph_cap_string(have), ceph_cap_string(need)); |
| 2635 | } |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2636 | out_unlock: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2637 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2638 | if (snap_rwsem_locked) |
| 2639 | up_read(&mdsc->snap_rwsem); |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2640 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2641 | dout("get_cap_refs %p ret %d got %s\n", inode, |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2642 | ret, ceph_cap_string(*got)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2643 | return ret; |
| 2644 | } |
| 2645 | |
| 2646 | /* |
| 2647 | * Check the offset we are writing up to against our current |
| 2648 | * max_size. If necessary, tell the MDS we want to write to |
| 2649 | * a larger offset. |
| 2650 | */ |
| 2651 | static void check_max_size(struct inode *inode, loff_t endoff) |
| 2652 | { |
| 2653 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2654 | int check = 0; |
| 2655 | |
| 2656 | /* do we need to explicitly request a larger max_size? */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2657 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 3871cbb | 2013-08-05 14:10:29 +0800 | [diff] [blame] | 2658 | if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2659 | dout("write %p at large endoff %llu, req max_size\n", |
| 2660 | inode, endoff); |
| 2661 | ci->i_wanted_max_size = endoff; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2662 | } |
Yan, Zheng | 3871cbb | 2013-08-05 14:10:29 +0800 | [diff] [blame] | 2663 | /* duplicate ceph_check_caps()'s logic */ |
| 2664 | if (ci->i_auth_cap && |
| 2665 | (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) && |
| 2666 | ci->i_wanted_max_size > ci->i_max_size && |
| 2667 | ci->i_wanted_max_size > ci->i_requested_max_size) |
| 2668 | check = 1; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2669 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2670 | if (check) |
| 2671 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); |
| 2672 | } |
| 2673 | |
Yan, Zheng | 2b1ac85 | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 2674 | int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got) |
| 2675 | { |
| 2676 | int ret, err = 0; |
| 2677 | |
| 2678 | BUG_ON(need & ~CEPH_CAP_FILE_RD); |
| 2679 | BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)); |
| 2680 | ret = ceph_pool_perm_check(ci, need); |
| 2681 | if (ret < 0) |
| 2682 | return ret; |
| 2683 | |
| 2684 | ret = try_get_cap_refs(ci, need, want, 0, true, got, &err); |
| 2685 | if (ret) { |
| 2686 | if (err == -EAGAIN) { |
| 2687 | ret = 0; |
| 2688 | } else if (err < 0) { |
| 2689 | ret = err; |
| 2690 | } |
| 2691 | } |
| 2692 | return ret; |
| 2693 | } |
| 2694 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2695 | /* |
| 2696 | * Wait for caps, and take cap references. If we can't get a WR cap |
| 2697 | * due to a small max_size, make sure we check_max_size (and possibly |
| 2698 | * ask the mds) so we don't get hung up indefinitely. |
| 2699 | */ |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2700 | int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, |
| 2701 | loff_t endoff, int *got, struct page **pinned_page) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2702 | { |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2703 | int _got, ret, err = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2704 | |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 2705 | ret = ceph_pool_perm_check(ci, need); |
| 2706 | if (ret < 0) |
| 2707 | return ret; |
| 2708 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2709 | while (true) { |
| 2710 | if (endoff > 0) |
| 2711 | check_max_size(&ci->vfs_inode, endoff); |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2712 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2713 | err = 0; |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2714 | _got = 0; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2715 | ret = try_get_cap_refs(ci, need, want, endoff, |
| 2716 | false, &_got, &err); |
| 2717 | if (ret) { |
| 2718 | if (err == -EAGAIN) |
| 2719 | continue; |
| 2720 | if (err < 0) |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2721 | ret = err; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2722 | } else { |
Nikolay Borisov | 5c341ee3 | 2016-10-11 12:04:09 +0300 | [diff] [blame] | 2723 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 2724 | add_wait_queue(&ci->i_cap_wq, &wait); |
| 2725 | |
| 2726 | while (!try_get_cap_refs(ci, need, want, endoff, |
Yan, Zheng | 6e09d0f | 2016-12-22 16:05:43 +0800 | [diff] [blame] | 2727 | true, &_got, &err)) { |
| 2728 | if (signal_pending(current)) { |
| 2729 | ret = -ERESTARTSYS; |
| 2730 | break; |
| 2731 | } |
Nikolay Borisov | 5c341ee3 | 2016-10-11 12:04:09 +0300 | [diff] [blame] | 2732 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Yan, Zheng | 6e09d0f | 2016-12-22 16:05:43 +0800 | [diff] [blame] | 2733 | } |
Nikolay Borisov | 5c341ee3 | 2016-10-11 12:04:09 +0300 | [diff] [blame] | 2734 | |
| 2735 | remove_wait_queue(&ci->i_cap_wq, &wait); |
| 2736 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2737 | if (err == -EAGAIN) |
| 2738 | continue; |
| 2739 | if (err < 0) |
| 2740 | ret = err; |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2741 | } |
| 2742 | if (ret < 0) { |
| 2743 | if (err == -ESTALE) { |
| 2744 | /* session was killed, try renew caps */ |
| 2745 | ret = ceph_renew_caps(&ci->vfs_inode); |
| 2746 | if (ret == 0) |
| 2747 | continue; |
| 2748 | } |
| 2749 | return ret; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2750 | } |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2751 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2752 | if (ci->i_inline_version != CEPH_INLINE_NONE && |
| 2753 | (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
| 2754 | i_size_read(&ci->vfs_inode) > 0) { |
| 2755 | struct page *page = |
| 2756 | find_get_page(ci->vfs_inode.i_mapping, 0); |
| 2757 | if (page) { |
| 2758 | if (PageUptodate(page)) { |
| 2759 | *pinned_page = page; |
| 2760 | break; |
| 2761 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2762 | put_page(page); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2763 | } |
| 2764 | /* |
| 2765 | * drop cap refs first because getattr while |
| 2766 | * holding * caps refs can cause deadlock. |
| 2767 | */ |
| 2768 | ceph_put_cap_refs(ci, _got); |
| 2769 | _got = 0; |
| 2770 | |
| 2771 | /* |
| 2772 | * getattr request will bring inline data into |
| 2773 | * page cache |
| 2774 | */ |
| 2775 | ret = __ceph_do_getattr(&ci->vfs_inode, NULL, |
| 2776 | CEPH_STAT_CAP_INLINE_DATA, |
| 2777 | true); |
| 2778 | if (ret < 0) |
| 2779 | return ret; |
| 2780 | continue; |
| 2781 | } |
| 2782 | break; |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2783 | } |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2784 | |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 2785 | if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE)) |
| 2786 | ceph_fscache_revalidate_cookie(ci); |
| 2787 | |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2788 | *got = _got; |
| 2789 | return 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | /* |
| 2793 | * Take cap refs. Caller must already know we hold at least one ref |
| 2794 | * on the caps in question or we don't know this is safe. |
| 2795 | */ |
| 2796 | void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) |
| 2797 | { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2798 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2799 | __take_cap_refs(ci, caps, false); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2800 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2803 | |
| 2804 | /* |
| 2805 | * drop cap_snap that is not associated with any snapshot. |
| 2806 | * we don't need to send FLUSHSNAP message for it. |
| 2807 | */ |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2808 | static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci, |
| 2809 | struct ceph_cap_snap *capsnap) |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2810 | { |
| 2811 | if (!capsnap->need_flush && |
| 2812 | !capsnap->writing && !capsnap->dirty_pages) { |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2813 | dout("dropping cap_snap %p follows %llu\n", |
| 2814 | capsnap, capsnap->follows); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2815 | BUG_ON(capsnap->cap_flush.tid > 0); |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2816 | ceph_put_snap_context(capsnap->context); |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2817 | if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps)) |
| 2818 | ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS; |
| 2819 | |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2820 | list_del(&capsnap->ci_item); |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2821 | ceph_put_cap_snap(capsnap); |
| 2822 | return 1; |
| 2823 | } |
| 2824 | return 0; |
| 2825 | } |
| 2826 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2827 | /* |
| 2828 | * Release cap refs. |
| 2829 | * |
| 2830 | * If we released the last ref on any given cap, call ceph_check_caps |
| 2831 | * to release (or schedule a release). |
| 2832 | * |
| 2833 | * If we are releasing a WR cap (from a sync write), finalize any affected |
| 2834 | * cap_snap, and wake up any waiters. |
| 2835 | */ |
| 2836 | void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) |
| 2837 | { |
| 2838 | struct inode *inode = &ci->vfs_inode; |
| 2839 | int last = 0, put = 0, flushsnaps = 0, wake = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2840 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2841 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2842 | if (had & CEPH_CAP_PIN) |
| 2843 | --ci->i_pin_ref; |
| 2844 | if (had & CEPH_CAP_FILE_RD) |
| 2845 | if (--ci->i_rd_ref == 0) |
| 2846 | last++; |
| 2847 | if (had & CEPH_CAP_FILE_CACHE) |
| 2848 | if (--ci->i_rdcache_ref == 0) |
| 2849 | last++; |
| 2850 | if (had & CEPH_CAP_FILE_BUFFER) { |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2851 | if (--ci->i_wb_ref == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2852 | last++; |
| 2853 | put++; |
| 2854 | } |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2855 | dout("put_cap_refs %p wb %d -> %d (?)\n", |
| 2856 | inode, ci->i_wb_ref+1, ci->i_wb_ref); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2857 | } |
| 2858 | if (had & CEPH_CAP_FILE_WR) |
| 2859 | if (--ci->i_wr_ref == 0) { |
| 2860 | last++; |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2861 | if (__ceph_have_pending_cap_snap(ci)) { |
| 2862 | struct ceph_cap_snap *capsnap = |
| 2863 | list_last_entry(&ci->i_cap_snaps, |
| 2864 | struct ceph_cap_snap, |
| 2865 | ci_item); |
| 2866 | capsnap->writing = 0; |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2867 | if (ceph_try_drop_cap_snap(ci, capsnap)) |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2868 | put++; |
| 2869 | else if (__ceph_finish_cap_snap(ci, capsnap)) |
| 2870 | flushsnaps = 1; |
| 2871 | wake = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2872 | } |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2873 | if (ci->i_wrbuffer_ref_head == 0 && |
| 2874 | ci->i_dirty_caps == 0 && |
| 2875 | ci->i_flushing_caps == 0) { |
| 2876 | BUG_ON(!ci->i_head_snapc); |
| 2877 | ceph_put_snap_context(ci->i_head_snapc); |
| 2878 | ci->i_head_snapc = NULL; |
| 2879 | } |
Yan, Zheng | db40cc1 | 2015-03-23 20:12:20 +0800 | [diff] [blame] | 2880 | /* see comment in __ceph_remove_cap() */ |
| 2881 | if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) |
| 2882 | drop_inode_snap_realm(ci); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2883 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2884 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2885 | |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2886 | dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had), |
| 2887 | last ? " last" : "", put ? " put" : ""); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2888 | |
| 2889 | if (last && !flushsnaps) |
| 2890 | ceph_check_caps(ci, 0, NULL); |
| 2891 | else if (flushsnaps) |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 2892 | ceph_flush_snaps(ci, NULL); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2893 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2894 | wake_up_all(&ci->i_cap_wq); |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2895 | while (put-- > 0) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2896 | iput(inode); |
| 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap |
| 2901 | * context. Adjust per-snap dirty page accounting as appropriate. |
| 2902 | * Once all dirty data for a cap_snap is flushed, flush snapped file |
| 2903 | * metadata back to the MDS. If we dropped the last ref, call |
| 2904 | * ceph_check_caps. |
| 2905 | */ |
| 2906 | void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, |
| 2907 | struct ceph_snap_context *snapc) |
| 2908 | { |
| 2909 | struct inode *inode = &ci->vfs_inode; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2910 | struct ceph_cap_snap *capsnap = NULL; |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2911 | int put = 0; |
| 2912 | bool last = false; |
| 2913 | bool found = false; |
| 2914 | bool flush_snaps = false; |
| 2915 | bool complete_capsnap = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2916 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2917 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2918 | ci->i_wrbuffer_ref -= nr; |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2919 | if (ci->i_wrbuffer_ref == 0) { |
| 2920 | last = true; |
| 2921 | put++; |
| 2922 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2923 | |
| 2924 | if (ci->i_head_snapc == snapc) { |
| 2925 | ci->i_wrbuffer_ref_head -= nr; |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 2926 | if (ci->i_wrbuffer_ref_head == 0 && |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2927 | ci->i_wr_ref == 0 && |
| 2928 | ci->i_dirty_caps == 0 && |
| 2929 | ci->i_flushing_caps == 0) { |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 2930 | BUG_ON(!ci->i_head_snapc); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2931 | ceph_put_snap_context(ci->i_head_snapc); |
| 2932 | ci->i_head_snapc = NULL; |
| 2933 | } |
| 2934 | dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n", |
| 2935 | inode, |
| 2936 | ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr, |
| 2937 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, |
| 2938 | last ? " LAST" : ""); |
| 2939 | } else { |
| 2940 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 2941 | if (capsnap->context == snapc) { |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2942 | found = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2943 | break; |
| 2944 | } |
| 2945 | } |
| 2946 | BUG_ON(!found); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2947 | capsnap->dirty_pages -= nr; |
| 2948 | if (capsnap->dirty_pages == 0) { |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2949 | complete_capsnap = true; |
| 2950 | if (!capsnap->writing) { |
| 2951 | if (ceph_try_drop_cap_snap(ci, capsnap)) { |
| 2952 | put++; |
| 2953 | } else { |
| 2954 | ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS; |
| 2955 | flush_snaps = true; |
| 2956 | } |
| 2957 | } |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2958 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2959 | dout("put_wrbuffer_cap_refs on %p cap_snap %p " |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2960 | " snap %lld %d/%d -> %d/%d %s%s\n", |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2961 | inode, capsnap, capsnap->context->seq, |
| 2962 | ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, |
| 2963 | ci->i_wrbuffer_ref, capsnap->dirty_pages, |
| 2964 | last ? " (wrbuffer last)" : "", |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2965 | complete_capsnap ? " (complete capsnap)" : ""); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2966 | } |
| 2967 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2968 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2969 | |
| 2970 | if (last) { |
| 2971 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2972 | } else if (flush_snaps) { |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 2973 | ceph_flush_snaps(ci, NULL); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2974 | } |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2975 | if (complete_capsnap) |
| 2976 | wake_up_all(&ci->i_cap_wq); |
| 2977 | while (put-- > 0) |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2978 | iput(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | /* |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 2982 | * Invalidate unlinked inode's aliases, so we can drop the inode ASAP. |
| 2983 | */ |
| 2984 | static void invalidate_aliases(struct inode *inode) |
| 2985 | { |
| 2986 | struct dentry *dn, *prev = NULL; |
| 2987 | |
| 2988 | dout("invalidate_aliases inode %p\n", inode); |
| 2989 | d_prune_aliases(inode); |
| 2990 | /* |
| 2991 | * For non-directory inode, d_find_alias() only returns |
J. Bruce Fields | fc12c80 | 2014-01-16 17:42:53 -0500 | [diff] [blame] | 2992 | * hashed dentry. After calling d_invalidate(), the |
| 2993 | * dentry becomes unhashed. |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 2994 | * |
Yan, Zheng | a8d436f | 2013-09-02 15:19:54 +0800 | [diff] [blame] | 2995 | * For directory inode, d_find_alias() can return |
J. Bruce Fields | fc12c80 | 2014-01-16 17:42:53 -0500 | [diff] [blame] | 2996 | * unhashed dentry. But directory inode should have |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 2997 | * one alias at most. |
| 2998 | */ |
| 2999 | while ((dn = d_find_alias(inode))) { |
| 3000 | if (dn == prev) { |
| 3001 | dput(dn); |
| 3002 | break; |
| 3003 | } |
Yan, Zheng | a8d436f | 2013-09-02 15:19:54 +0800 | [diff] [blame] | 3004 | d_invalidate(dn); |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3005 | if (prev) |
| 3006 | dput(prev); |
| 3007 | prev = dn; |
| 3008 | } |
| 3009 | if (prev) |
| 3010 | dput(prev); |
| 3011 | } |
| 3012 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3013 | struct cap_extra_info { |
| 3014 | struct ceph_string *pool_ns; |
| 3015 | /* inline data */ |
| 3016 | u64 inline_version; |
| 3017 | void *inline_data; |
| 3018 | u32 inline_len; |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3019 | /* dirstat */ |
| 3020 | bool dirstat_valid; |
| 3021 | u64 nfiles; |
| 3022 | u64 nsubdirs; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3023 | /* currently issued */ |
| 3024 | int issued; |
| 3025 | }; |
| 3026 | |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3027 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3028 | * Handle a cap GRANT message from the MDS. (Note that a GRANT may |
| 3029 | * actually be a revocation if it specifies a smaller cap set.) |
| 3030 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3031 | * caller holds s_mutex and i_ceph_lock, we drop both. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3032 | */ |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3033 | static void handle_cap_grant(struct inode *inode, |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 3034 | struct ceph_mds_session *session, |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3035 | struct ceph_cap *cap, |
| 3036 | struct ceph_mds_caps *grant, |
| 3037 | struct ceph_buffer *xattr_buf, |
| 3038 | struct cap_extra_info *extra_info) |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3039 | __releases(ci->i_ceph_lock) |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3040 | __releases(session->s_mdsc->snap_rwsem) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3041 | { |
| 3042 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | 2f56f56 | 2010-10-27 20:59:49 -0700 | [diff] [blame] | 3043 | int seq = le32_to_cpu(grant->seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3044 | int newcaps = le32_to_cpu(grant->caps); |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3045 | int used, wanted, dirty; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3046 | u64 size = le64_to_cpu(grant->size); |
| 3047 | u64 max_size = le64_to_cpu(grant->max_size); |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 3048 | int check_caps = 0; |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3049 | bool wake = false; |
| 3050 | bool writeback = false; |
| 3051 | bool queue_trunc = false; |
| 3052 | bool queue_invalidate = false; |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3053 | bool deleted_inode = false; |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 3054 | bool fill_inline = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3055 | |
Sage Weil | 2f56f56 | 2010-10-27 20:59:49 -0700 | [diff] [blame] | 3056 | dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n", |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3057 | inode, cap, session->s_mds, seq, ceph_cap_string(newcaps)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3058 | dout(" size %llu max_size %llu, i_size %llu\n", size, max_size, |
| 3059 | inode->i_size); |
| 3060 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3061 | |
| 3062 | /* |
| 3063 | * auth mds of the inode changed. we received the cap export message, |
| 3064 | * but still haven't received the cap import message. handle_cap_export |
| 3065 | * updated the new auth MDS' cap. |
| 3066 | * |
| 3067 | * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message |
| 3068 | * that was sent before the cap import message. So don't remove caps. |
| 3069 | */ |
| 3070 | if (ceph_seq_cmp(seq, cap->seq) <= 0) { |
| 3071 | WARN_ON(cap != ci->i_auth_cap); |
| 3072 | WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id)); |
| 3073 | seq = cap->seq; |
| 3074 | newcaps |= cap->issued; |
| 3075 | } |
| 3076 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3077 | /* |
| 3078 | * If CACHE is being revoked, and we have no dirty buffers, |
| 3079 | * try to invalidate (once). (If there are dirty buffers, we |
| 3080 | * will invalidate _after_ writeback.) |
| 3081 | */ |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 3082 | if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */ |
| 3083 | ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 3084 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
Yan, Zheng | 9abd4db | 2016-05-18 20:58:26 +0800 | [diff] [blame] | 3085 | !(ci->i_wrbuffer_ref || ci->i_wb_ref)) { |
Li Wang | e907574 | 2013-08-15 22:00:25 -0700 | [diff] [blame] | 3086 | if (try_nonblocking_invalidate(inode)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3087 | /* there were locked pages.. invalidate later |
| 3088 | in a separate thread. */ |
| 3089 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3090 | queue_invalidate = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3091 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
| 3092 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3093 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | /* side effects now are allowed */ |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 3097 | cap->cap_gen = session->s_cap_gen; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3098 | cap->seq = seq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3099 | |
| 3100 | __check_cap_issue(ci, cap, newcaps); |
| 3101 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3102 | if ((newcaps & CEPH_CAP_AUTH_SHARED) && |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3103 | (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3104 | inode->i_mode = le32_to_cpu(grant->mode); |
Eric W. Biederman | 05cb11c | 2013-01-31 02:56:19 -0800 | [diff] [blame] | 3105 | inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid)); |
| 3106 | inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3107 | dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, |
Eric W. Biederman | bd2bae6 | 2013-01-31 04:05:39 -0800 | [diff] [blame] | 3108 | from_kuid(&init_user_ns, inode->i_uid), |
| 3109 | from_kgid(&init_user_ns, inode->i_gid)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3110 | } |
| 3111 | |
Yan, Zheng | fa46674 | 2018-05-25 11:22:56 +0800 | [diff] [blame] | 3112 | if ((newcaps & CEPH_CAP_LINK_SHARED) && |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3113 | (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) { |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3114 | set_nlink(inode, le32_to_cpu(grant->nlink)); |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3115 | if (inode->i_nlink == 0 && |
| 3116 | (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL))) |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3117 | deleted_inode = true; |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3118 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3119 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3120 | if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 && |
| 3121 | grant->xattr_len) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3122 | int len = le32_to_cpu(grant->xattr_len); |
| 3123 | u64 version = le64_to_cpu(grant->xattr_version); |
| 3124 | |
| 3125 | if (version > ci->i_xattrs.version) { |
| 3126 | dout(" got new xattrs v%llu on %p len %d\n", |
| 3127 | version, inode, len); |
| 3128 | if (ci->i_xattrs.blob) |
| 3129 | ceph_buffer_put(ci->i_xattrs.blob); |
| 3130 | ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); |
| 3131 | ci->i_xattrs.version = version; |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 3132 | ceph_forget_all_cached_acls(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3133 | } |
| 3134 | } |
| 3135 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3136 | if (newcaps & CEPH_CAP_ANY_RD) { |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 3137 | struct timespec64 mtime, atime, ctime; |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3138 | /* ctime/mtime/atime? */ |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 3139 | ceph_decode_timespec64(&mtime, &grant->mtime); |
| 3140 | ceph_decode_timespec64(&atime, &grant->atime); |
| 3141 | ceph_decode_timespec64(&ctime, &grant->ctime); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3142 | ceph_fill_file_time(inode, extra_info->issued, |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3143 | le32_to_cpu(grant->time_warp_seq), |
| 3144 | &ctime, &mtime, &atime); |
| 3145 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3146 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3147 | if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) { |
| 3148 | ci->i_files = extra_info->nfiles; |
| 3149 | ci->i_subdirs = extra_info->nsubdirs; |
| 3150 | } |
| 3151 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3152 | if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) { |
| 3153 | /* file layout may have changed */ |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 3154 | s64 old_pool = ci->i_layout.pool_id; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3155 | struct ceph_string *old_ns; |
| 3156 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 3157 | ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3158 | old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, |
| 3159 | lockdep_is_held(&ci->i_ceph_lock)); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3160 | rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3161 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3162 | if (ci->i_layout.pool_id != old_pool || |
| 3163 | extra_info->pool_ns != old_ns) |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 3164 | ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 3165 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3166 | extra_info->pool_ns = old_ns; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3167 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3168 | /* size/truncate_seq? */ |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3169 | queue_trunc = ceph_fill_file_size(inode, extra_info->issued, |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3170 | le32_to_cpu(grant->truncate_seq), |
| 3171 | le64_to_cpu(grant->truncate_size), |
| 3172 | size); |
Yan, Zheng | 84eea8c | 2017-05-16 08:55:34 +0800 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) { |
| 3176 | if (max_size != ci->i_max_size) { |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3177 | dout("max_size %lld -> %llu\n", |
| 3178 | ci->i_max_size, max_size); |
| 3179 | ci->i_max_size = max_size; |
| 3180 | if (max_size >= ci->i_wanted_max_size) { |
| 3181 | ci->i_wanted_max_size = 0; /* reset */ |
| 3182 | ci->i_requested_max_size = 0; |
| 3183 | } |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3184 | wake = true; |
Yan, Zheng | 84eea8c | 2017-05-16 08:55:34 +0800 | [diff] [blame] | 3185 | } else if (ci->i_wanted_max_size > ci->i_max_size && |
| 3186 | ci->i_wanted_max_size > ci->i_requested_max_size) { |
| 3187 | /* CEPH_CAP_OP_IMPORT */ |
| 3188 | wake = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3189 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | /* check cap bits */ |
| 3193 | wanted = __ceph_caps_wanted(ci); |
| 3194 | used = __ceph_caps_used(ci); |
| 3195 | dirty = __ceph_caps_dirty(ci); |
| 3196 | dout(" my wanted = %s, used = %s, dirty %s\n", |
| 3197 | ceph_cap_string(wanted), |
| 3198 | ceph_cap_string(used), |
| 3199 | ceph_cap_string(dirty)); |
| 3200 | if (wanted != le32_to_cpu(grant->wanted)) { |
| 3201 | dout("mds wanted %s -> %s\n", |
| 3202 | ceph_cap_string(le32_to_cpu(grant->wanted)), |
| 3203 | ceph_cap_string(wanted)); |
Yan, Zheng | 390306c | 2013-01-04 15:30:10 +0800 | [diff] [blame] | 3204 | /* imported cap may not have correct mds_wanted */ |
| 3205 | if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) |
| 3206 | check_caps = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3207 | } |
| 3208 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3209 | /* revocation, grant, or no-op? */ |
| 3210 | if (cap->issued & ~newcaps) { |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 3211 | int revoking = cap->issued & ~newcaps; |
| 3212 | |
| 3213 | dout("revocation: %s -> %s (revoking %s)\n", |
| 3214 | ceph_cap_string(cap->issued), |
| 3215 | ceph_cap_string(newcaps), |
| 3216 | ceph_cap_string(revoking)); |
Sage Weil | 0eb6cd4 | 2010-08-05 13:53:18 -0700 | [diff] [blame] | 3217 | if (revoking & used & CEPH_CAP_FILE_BUFFER) |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3218 | writeback = true; /* initiate writeback; will delay ack */ |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 3219 | else if (revoking == CEPH_CAP_FILE_CACHE && |
| 3220 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
| 3221 | queue_invalidate) |
| 3222 | ; /* do nothing yet, invalidation will be queued */ |
| 3223 | else if (cap == ci->i_auth_cap) |
| 3224 | check_caps = 1; /* check auth cap only */ |
| 3225 | else |
| 3226 | check_caps = 2; /* check all caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3227 | cap->issued = newcaps; |
Sage Weil | 978097c | 2010-03-08 15:27:53 -0800 | [diff] [blame] | 3228 | cap->implemented |= newcaps; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3229 | } else if (cap->issued == newcaps) { |
| 3230 | dout("caps unchanged: %s -> %s\n", |
| 3231 | ceph_cap_string(cap->issued), ceph_cap_string(newcaps)); |
| 3232 | } else { |
| 3233 | dout("grant: %s -> %s\n", ceph_cap_string(cap->issued), |
| 3234 | ceph_cap_string(newcaps)); |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 3235 | /* non-auth MDS is revoking the newly grant caps ? */ |
| 3236 | if (cap == ci->i_auth_cap && |
| 3237 | __ceph_caps_revoking_other(ci, cap, newcaps)) |
| 3238 | check_caps = 2; |
| 3239 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3240 | cap->issued = newcaps; |
| 3241 | cap->implemented |= newcaps; /* add bits only, to |
| 3242 | * avoid stepping on a |
| 3243 | * pending revocation */ |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3244 | wake = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3245 | } |
Sage Weil | 978097c | 2010-03-08 15:27:53 -0800 | [diff] [blame] | 3246 | BUG_ON(cap->issued & ~cap->implemented); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3247 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3248 | if (extra_info->inline_version > 0 && |
| 3249 | extra_info->inline_version >= ci->i_inline_version) { |
| 3250 | ci->i_inline_version = extra_info->inline_version; |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 3251 | if (ci->i_inline_version != CEPH_INLINE_NONE && |
| 3252 | (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO))) |
| 3253 | fill_inline = true; |
| 3254 | } |
| 3255 | |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3256 | if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) { |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3257 | if (newcaps & ~extra_info->issued) |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3258 | wake = true; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3259 | kick_flushing_inode_caps(session->s_mdsc, session, inode); |
| 3260 | up_read(&session->s_mdsc->snap_rwsem); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3261 | } else { |
| 3262 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3263 | } |
| 3264 | |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 3265 | if (fill_inline) |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3266 | ceph_fill_inline_data(inode, NULL, extra_info->inline_data, |
| 3267 | extra_info->inline_len); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 3268 | |
Yan, Zheng | 1464975 | 2016-05-20 15:41:20 +0800 | [diff] [blame] | 3269 | if (queue_trunc) |
Yan, Zheng | c6bcda6 | 2014-04-11 10:18:07 +0800 | [diff] [blame] | 3270 | ceph_queue_vmtruncate(inode); |
Yan, Zheng | c6bcda6 | 2014-04-11 10:18:07 +0800 | [diff] [blame] | 3271 | |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 3272 | if (writeback) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3273 | /* |
| 3274 | * queue inode for writeback: we can't actually call |
| 3275 | * filemap_write_and_wait, etc. from message handler |
| 3276 | * context. |
| 3277 | */ |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 3278 | ceph_queue_writeback(inode); |
| 3279 | if (queue_invalidate) |
| 3280 | ceph_queue_invalidate(inode); |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3281 | if (deleted_inode) |
| 3282 | invalidate_aliases(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3283 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 3284 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 3285 | |
| 3286 | if (check_caps == 1) |
| 3287 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY, |
| 3288 | session); |
| 3289 | else if (check_caps == 2) |
| 3290 | ceph_check_caps(ci, CHECK_CAPS_NODELAY, session); |
| 3291 | else |
| 3292 | mutex_unlock(&session->s_mutex); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | /* |
| 3296 | * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the |
| 3297 | * MDS has been safely committed. |
| 3298 | */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 3299 | static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3300 | struct ceph_mds_caps *m, |
| 3301 | struct ceph_mds_session *session, |
| 3302 | struct ceph_cap *cap) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3303 | __releases(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3304 | { |
| 3305 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3306 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3307 | struct ceph_cap_flush *cf, *tmp_cf; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3308 | LIST_HEAD(to_remove); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3309 | unsigned seq = le32_to_cpu(m->seq); |
| 3310 | int dirty = le32_to_cpu(m->dirty); |
| 3311 | int cleaned = 0; |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3312 | bool drop = false; |
Thomas Meyer | 7271efa | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 3313 | bool wake_ci = false; |
| 3314 | bool wake_mdsc = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3315 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3316 | list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) { |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3317 | if (cf->tid == flush_tid) |
| 3318 | cleaned = cf->caps; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3319 | if (cf->caps == 0) /* capsnap */ |
| 3320 | continue; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3321 | if (cf->tid <= flush_tid) { |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3322 | if (__finish_cap_flush(NULL, ci, cf)) |
| 3323 | wake_ci = true; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3324 | list_add_tail(&cf->i_list, &to_remove); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3325 | } else { |
| 3326 | cleaned &= ~cf->caps; |
| 3327 | if (!cleaned) |
| 3328 | break; |
| 3329 | } |
| 3330 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3331 | |
| 3332 | dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s," |
| 3333 | " flushing %s -> %s\n", |
| 3334 | inode, session->s_mds, seq, ceph_cap_string(dirty), |
| 3335 | ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps), |
| 3336 | ceph_cap_string(ci->i_flushing_caps & ~cleaned)); |
| 3337 | |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 3338 | if (list_empty(&to_remove) && !cleaned) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3339 | goto out; |
| 3340 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3341 | ci->i_flushing_caps &= ~cleaned; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3342 | |
| 3343 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 3344 | |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3345 | list_for_each_entry(cf, &to_remove, i_list) { |
| 3346 | if (__finish_cap_flush(mdsc, NULL, cf)) |
| 3347 | wake_mdsc = true; |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 3348 | } |
| 3349 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3350 | if (ci->i_flushing_caps == 0) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3351 | if (list_empty(&ci->i_cap_flush_list)) { |
| 3352 | list_del_init(&ci->i_flushing_item); |
| 3353 | if (!list_empty(&session->s_cap_flushing)) { |
| 3354 | dout(" mds%d still flushing cap on %p\n", |
| 3355 | session->s_mds, |
| 3356 | &list_first_entry(&session->s_cap_flushing, |
| 3357 | struct ceph_inode_info, |
| 3358 | i_flushing_item)->vfs_inode); |
| 3359 | } |
| 3360 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3361 | mdsc->num_cap_flushing--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3362 | dout(" inode %p now !flushing\n", inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3363 | |
| 3364 | if (ci->i_dirty_caps == 0) { |
| 3365 | dout(" inode %p now clean\n", inode); |
| 3366 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3367 | drop = true; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 3368 | if (ci->i_wr_ref == 0 && |
| 3369 | ci->i_wrbuffer_ref_head == 0) { |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 3370 | BUG_ON(!ci->i_head_snapc); |
| 3371 | ceph_put_snap_context(ci->i_head_snapc); |
| 3372 | ci->i_head_snapc = NULL; |
| 3373 | } |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 3374 | } else { |
| 3375 | BUG_ON(list_empty(&ci->i_dirty_item)); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3376 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3377 | } |
| 3378 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3379 | |
| 3380 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3381 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3382 | |
| 3383 | while (!list_empty(&to_remove)) { |
| 3384 | cf = list_first_entry(&to_remove, |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3385 | struct ceph_cap_flush, i_list); |
| 3386 | list_del(&cf->i_list); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 3387 | ceph_free_cap_flush(cf); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3388 | } |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3389 | |
| 3390 | if (wake_ci) |
| 3391 | wake_up_all(&ci->i_cap_wq); |
| 3392 | if (wake_mdsc) |
| 3393 | wake_up_all(&mdsc->cap_flushing_wq); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3394 | if (drop) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3395 | iput(inode); |
| 3396 | } |
| 3397 | |
| 3398 | /* |
| 3399 | * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can |
| 3400 | * throw away our cap_snap. |
| 3401 | * |
| 3402 | * Caller hold s_mutex. |
| 3403 | */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 3404 | static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3405 | struct ceph_mds_caps *m, |
| 3406 | struct ceph_mds_session *session) |
| 3407 | { |
| 3408 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | affbc19 | 2015-05-05 21:22:13 +0800 | [diff] [blame] | 3409 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3410 | u64 follows = le64_to_cpu(m->snap_follows); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3411 | struct ceph_cap_snap *capsnap; |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3412 | bool flushed = false; |
| 3413 | bool wake_ci = false; |
| 3414 | bool wake_mdsc = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3415 | |
| 3416 | dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n", |
| 3417 | inode, ci, session->s_mds, follows); |
| 3418 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3419 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3420 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 3421 | if (capsnap->follows == follows) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3422 | if (capsnap->cap_flush.tid != flush_tid) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3423 | dout(" cap_snap %p follows %lld tid %lld !=" |
| 3424 | " %lld\n", capsnap, follows, |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3425 | flush_tid, capsnap->cap_flush.tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3426 | break; |
| 3427 | } |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3428 | flushed = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3429 | break; |
| 3430 | } else { |
| 3431 | dout(" skipping cap_snap %p follows %lld\n", |
| 3432 | capsnap, capsnap->follows); |
| 3433 | } |
| 3434 | } |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3435 | if (flushed) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3436 | WARN_ON(capsnap->dirty_pages || capsnap->writing); |
| 3437 | dout(" removing %p cap_snap %p follows %lld\n", |
| 3438 | inode, capsnap, follows); |
| 3439 | list_del(&capsnap->ci_item); |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3440 | if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush)) |
| 3441 | wake_ci = true; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3442 | |
| 3443 | spin_lock(&mdsc->cap_dirty_lock); |
| 3444 | |
| 3445 | if (list_empty(&ci->i_cap_flush_list)) |
| 3446 | list_del_init(&ci->i_flushing_item); |
| 3447 | |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3448 | if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush)) |
| 3449 | wake_mdsc = true; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3450 | |
| 3451 | spin_unlock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3452 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3453 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3454 | if (flushed) { |
| 3455 | ceph_put_snap_context(capsnap->context); |
| 3456 | ceph_put_cap_snap(capsnap); |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3457 | if (wake_ci) |
| 3458 | wake_up_all(&ci->i_cap_wq); |
| 3459 | if (wake_mdsc) |
| 3460 | wake_up_all(&mdsc->cap_flushing_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3461 | iput(inode); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3462 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3463 | } |
| 3464 | |
| 3465 | /* |
| 3466 | * Handle TRUNC from MDS, indicating file truncation. |
| 3467 | * |
| 3468 | * caller hold s_mutex. |
| 3469 | */ |
| 3470 | static void handle_cap_trunc(struct inode *inode, |
| 3471 | struct ceph_mds_caps *trunc, |
| 3472 | struct ceph_mds_session *session) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3473 | __releases(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3474 | { |
| 3475 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 3476 | int mds = session->s_mds; |
| 3477 | int seq = le32_to_cpu(trunc->seq); |
| 3478 | u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); |
| 3479 | u64 truncate_size = le64_to_cpu(trunc->truncate_size); |
| 3480 | u64 size = le64_to_cpu(trunc->size); |
| 3481 | int implemented = 0; |
| 3482 | int dirty = __ceph_caps_dirty(ci); |
| 3483 | int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); |
| 3484 | int queue_trunc = 0; |
| 3485 | |
| 3486 | issued |= implemented | dirty; |
| 3487 | |
| 3488 | dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n", |
| 3489 | inode, mds, seq, truncate_size, truncate_seq); |
| 3490 | queue_trunc = ceph_fill_file_size(inode, issued, |
| 3491 | truncate_seq, truncate_size, size); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3492 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3493 | |
Yan, Zheng | 1464975 | 2016-05-20 15:41:20 +0800 | [diff] [blame] | 3494 | if (queue_trunc) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 3495 | ceph_queue_vmtruncate(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | /* |
| 3499 | * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a |
| 3500 | * different one. If we are the most recent migration we've seen (as |
| 3501 | * indicated by mseq), make note of the migrating cap bits for the |
| 3502 | * duration (until we see the corresponding IMPORT). |
| 3503 | * |
| 3504 | * caller holds s_mutex |
| 3505 | */ |
| 3506 | static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3507 | struct ceph_mds_cap_peer *ph, |
| 3508 | struct ceph_mds_session *session) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3509 | { |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 3510 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3511 | struct ceph_mds_session *tsession = NULL; |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3512 | struct ceph_cap *cap, *tcap, *new_cap = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3513 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3514 | u64 t_cap_id; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3515 | unsigned mseq = le32_to_cpu(ex->migrate_seq); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3516 | unsigned t_seq, t_mseq; |
| 3517 | int target, issued; |
| 3518 | int mds = session->s_mds; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3519 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3520 | if (ph) { |
| 3521 | t_cap_id = le64_to_cpu(ph->cap_id); |
| 3522 | t_seq = le32_to_cpu(ph->seq); |
| 3523 | t_mseq = le32_to_cpu(ph->mseq); |
| 3524 | target = le32_to_cpu(ph->mds); |
| 3525 | } else { |
| 3526 | t_cap_id = t_seq = t_mseq = 0; |
| 3527 | target = -1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3528 | } |
| 3529 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3530 | dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n", |
| 3531 | inode, ci, mds, mseq, target); |
| 3532 | retry: |
| 3533 | spin_lock(&ci->i_ceph_lock); |
| 3534 | cap = __get_cap_for_mds(ci, mds); |
Yan, Zheng | ca665e0 | 2014-04-21 15:46:37 +0800 | [diff] [blame] | 3535 | if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id)) |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3536 | goto out_unlock; |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 3537 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3538 | if (target < 0) { |
| 3539 | __ceph_remove_cap(cap, false); |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 3540 | if (!ci->i_auth_cap) |
| 3541 | ci->i_ceph_flags |= CEPH_I_CAP_DROPPED; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3542 | goto out_unlock; |
| 3543 | } |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 3544 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3545 | /* |
| 3546 | * now we know we haven't received the cap import message yet |
| 3547 | * because the exported cap still exist. |
| 3548 | */ |
| 3549 | |
| 3550 | issued = cap->issued; |
Yan, Zheng | d84b37f | 2018-01-03 11:16:27 +0800 | [diff] [blame] | 3551 | if (issued != cap->implemented) |
| 3552 | pr_err_ratelimited("handle_cap_export: issued != implemented: " |
| 3553 | "ino (%llx.%llx) mds%d seq %d mseq %d " |
| 3554 | "issued %s implemented %s\n", |
| 3555 | ceph_vinop(inode), mds, cap->seq, cap->mseq, |
| 3556 | ceph_cap_string(issued), |
| 3557 | ceph_cap_string(cap->implemented)); |
| 3558 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3559 | |
| 3560 | tcap = __get_cap_for_mds(ci, target); |
| 3561 | if (tcap) { |
| 3562 | /* already have caps from the target */ |
Yan, Zheng | fa0aa3b | 2017-08-28 15:07:42 +0800 | [diff] [blame] | 3563 | if (tcap->cap_id == t_cap_id && |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3564 | ceph_seq_cmp(tcap->seq, t_seq) < 0) { |
| 3565 | dout(" updating import cap %p mds%d\n", tcap, target); |
| 3566 | tcap->cap_id = t_cap_id; |
| 3567 | tcap->seq = t_seq - 1; |
| 3568 | tcap->issue_seq = t_seq - 1; |
| 3569 | tcap->mseq = t_mseq; |
| 3570 | tcap->issued |= issued; |
| 3571 | tcap->implemented |= issued; |
| 3572 | if (cap == ci->i_auth_cap) |
| 3573 | ci->i_auth_cap = tcap; |
Yan, Zheng | 00f06cb | 2017-01-24 10:02:32 +0800 | [diff] [blame] | 3574 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3575 | if (!list_empty(&ci->i_cap_flush_list) && |
| 3576 | ci->i_auth_cap == tcap) { |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3577 | spin_lock(&mdsc->cap_dirty_lock); |
| 3578 | list_move_tail(&ci->i_flushing_item, |
| 3579 | &tcap->session->s_cap_flushing); |
| 3580 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 3581 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3582 | } |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 3583 | __ceph_remove_cap(cap, false); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3584 | goto out_unlock; |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3585 | } else if (tsession) { |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3586 | /* add placeholder for the export tagert */ |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3587 | int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0; |
Yan, Zheng | 00f06cb | 2017-01-24 10:02:32 +0800 | [diff] [blame] | 3588 | tcap = new_cap; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3589 | ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0, |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3590 | t_seq - 1, t_mseq, (u64)-1, flag, &new_cap); |
| 3591 | |
Yan, Zheng | 00f06cb | 2017-01-24 10:02:32 +0800 | [diff] [blame] | 3592 | if (!list_empty(&ci->i_cap_flush_list) && |
| 3593 | ci->i_auth_cap == tcap) { |
| 3594 | spin_lock(&mdsc->cap_dirty_lock); |
| 3595 | list_move_tail(&ci->i_flushing_item, |
| 3596 | &tcap->session->s_cap_flushing); |
| 3597 | spin_unlock(&mdsc->cap_dirty_lock); |
| 3598 | } |
| 3599 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3600 | __ceph_remove_cap(cap, false); |
| 3601 | goto out_unlock; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3602 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3603 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3604 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3605 | mutex_unlock(&session->s_mutex); |
| 3606 | |
| 3607 | /* open target session */ |
| 3608 | tsession = ceph_mdsc_open_export_target_session(mdsc, target); |
| 3609 | if (!IS_ERR(tsession)) { |
| 3610 | if (mds > target) { |
| 3611 | mutex_lock(&session->s_mutex); |
| 3612 | mutex_lock_nested(&tsession->s_mutex, |
| 3613 | SINGLE_DEPTH_NESTING); |
| 3614 | } else { |
| 3615 | mutex_lock(&tsession->s_mutex); |
| 3616 | mutex_lock_nested(&session->s_mutex, |
| 3617 | SINGLE_DEPTH_NESTING); |
| 3618 | } |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3619 | new_cap = ceph_get_cap(mdsc, NULL); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3620 | } else { |
| 3621 | WARN_ON(1); |
| 3622 | tsession = NULL; |
| 3623 | target = -1; |
| 3624 | } |
| 3625 | goto retry; |
| 3626 | |
| 3627 | out_unlock: |
| 3628 | spin_unlock(&ci->i_ceph_lock); |
| 3629 | mutex_unlock(&session->s_mutex); |
| 3630 | if (tsession) { |
| 3631 | mutex_unlock(&tsession->s_mutex); |
| 3632 | ceph_put_mds_session(tsession); |
| 3633 | } |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3634 | if (new_cap) |
| 3635 | ceph_put_cap(mdsc, new_cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | /* |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3639 | * Handle cap IMPORT. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3640 | * |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3641 | * caller holds s_mutex. acquires i_ceph_lock |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3642 | */ |
| 3643 | static void handle_cap_import(struct ceph_mds_client *mdsc, |
| 3644 | struct inode *inode, struct ceph_mds_caps *im, |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3645 | struct ceph_mds_cap_peer *ph, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3646 | struct ceph_mds_session *session, |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3647 | struct ceph_cap **target_cap, int *old_issued) |
| 3648 | __acquires(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3649 | { |
| 3650 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3651 | struct ceph_cap *cap, *ocap, *new_cap = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3652 | int mds = session->s_mds; |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3653 | int issued; |
| 3654 | unsigned caps = le32_to_cpu(im->caps); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3655 | unsigned wanted = le32_to_cpu(im->wanted); |
| 3656 | unsigned seq = le32_to_cpu(im->seq); |
| 3657 | unsigned mseq = le32_to_cpu(im->migrate_seq); |
| 3658 | u64 realmino = le64_to_cpu(im->realm); |
| 3659 | u64 cap_id = le64_to_cpu(im->cap_id); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3660 | u64 p_cap_id; |
| 3661 | int peer; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3662 | |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3663 | if (ph) { |
| 3664 | p_cap_id = le64_to_cpu(ph->cap_id); |
| 3665 | peer = le32_to_cpu(ph->mds); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3666 | } else { |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3667 | p_cap_id = 0; |
| 3668 | peer = -1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3669 | } |
| 3670 | |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3671 | dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n", |
| 3672 | inode, ci, mds, mseq, peer); |
| 3673 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3674 | retry: |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3675 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3676 | cap = __get_cap_for_mds(ci, mds); |
| 3677 | if (!cap) { |
| 3678 | if (!new_cap) { |
| 3679 | spin_unlock(&ci->i_ceph_lock); |
| 3680 | new_cap = ceph_get_cap(mdsc, NULL); |
| 3681 | goto retry; |
| 3682 | } |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3683 | cap = new_cap; |
| 3684 | } else { |
| 3685 | if (new_cap) { |
| 3686 | ceph_put_cap(mdsc, new_cap); |
| 3687 | new_cap = NULL; |
| 3688 | } |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3689 | } |
| 3690 | |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3691 | __ceph_caps_issued(ci, &issued); |
| 3692 | issued |= __ceph_caps_dirty(ci); |
| 3693 | |
| 3694 | ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq, |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3695 | realmino, CEPH_CAP_FLAG_AUTH, &new_cap); |
| 3696 | |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3697 | ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL; |
| 3698 | if (ocap && ocap->cap_id == p_cap_id) { |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3699 | dout(" remove export cap %p mds%d flags %d\n", |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3700 | ocap, peer, ph->flags); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3701 | if ((ph->flags & CEPH_CAP_FLAG_AUTH) && |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3702 | (ocap->seq != le32_to_cpu(ph->seq) || |
| 3703 | ocap->mseq != le32_to_cpu(ph->mseq))) { |
Yan, Zheng | d84b37f | 2018-01-03 11:16:27 +0800 | [diff] [blame] | 3704 | pr_err_ratelimited("handle_cap_import: " |
| 3705 | "mismatched seq/mseq: ino (%llx.%llx) " |
| 3706 | "mds%d seq %d mseq %d importer mds%d " |
| 3707 | "has peer seq %d mseq %d\n", |
| 3708 | ceph_vinop(inode), peer, ocap->seq, |
| 3709 | ocap->mseq, mds, le32_to_cpu(ph->seq), |
| 3710 | le32_to_cpu(ph->mseq)); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3711 | } |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3712 | __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE)); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | /* make sure we re-request max_size, if necessary */ |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3716 | ci->i_requested_max_size = 0; |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3717 | |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3718 | *old_issued = issued; |
| 3719 | *target_cap = cap; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | /* |
| 3723 | * Handle a caps message from the MDS. |
| 3724 | * |
| 3725 | * Identify the appropriate session, inode, and call the right handler |
| 3726 | * based on the cap op. |
| 3727 | */ |
| 3728 | void ceph_handle_caps(struct ceph_mds_session *session, |
| 3729 | struct ceph_msg *msg) |
| 3730 | { |
| 3731 | struct ceph_mds_client *mdsc = session->s_mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3732 | struct inode *inode; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3733 | struct ceph_inode_info *ci; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3734 | struct ceph_cap *cap; |
| 3735 | struct ceph_mds_caps *h; |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3736 | struct ceph_mds_cap_peer *peer = NULL; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3737 | struct ceph_snap_realm *realm = NULL; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3738 | int op; |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3739 | int msg_version = le16_to_cpu(msg->hdr.version); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 3740 | u32 seq, mseq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3741 | struct ceph_vino vino; |
Sage Weil | 70edb55 | 2010-03-01 13:20:50 -0800 | [diff] [blame] | 3742 | void *snaptrace; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3743 | size_t snaptrace_len; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3744 | void *p, *end; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3745 | struct cap_extra_info extra_info = {}; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3746 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3747 | dout("handle_caps from mds%d\n", session->s_mds); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3748 | |
| 3749 | /* decode */ |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3750 | end = msg->front.iov_base + msg->front.iov_len; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3751 | if (msg->front.iov_len < sizeof(*h)) |
| 3752 | goto bad; |
| 3753 | h = msg->front.iov_base; |
| 3754 | op = le32_to_cpu(h->op); |
| 3755 | vino.ino = le64_to_cpu(h->ino); |
| 3756 | vino.snap = CEPH_NOSNAP; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3757 | seq = le32_to_cpu(h->seq); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 3758 | mseq = le32_to_cpu(h->migrate_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3759 | |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3760 | snaptrace = h + 1; |
| 3761 | snaptrace_len = le32_to_cpu(h->snap_trace_len); |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3762 | p = snaptrace + snaptrace_len; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3763 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3764 | if (msg_version >= 2) { |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3765 | u32 flock_len; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3766 | ceph_decode_32_safe(&p, end, flock_len, bad); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3767 | if (p + flock_len > end) |
| 3768 | goto bad; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3769 | p += flock_len; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3772 | if (msg_version >= 3) { |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3773 | if (op == CEPH_CAP_OP_IMPORT) { |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3774 | if (p + sizeof(*peer) > end) |
| 3775 | goto bad; |
| 3776 | peer = p; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3777 | p += sizeof(*peer); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3778 | } else if (op == CEPH_CAP_OP_EXPORT) { |
| 3779 | /* recorded in unused fields */ |
| 3780 | peer = (void *)&h->size; |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3784 | if (msg_version >= 4) { |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3785 | ceph_decode_64_safe(&p, end, extra_info.inline_version, bad); |
| 3786 | ceph_decode_32_safe(&p, end, extra_info.inline_len, bad); |
| 3787 | if (p + extra_info.inline_len > end) |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3788 | goto bad; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3789 | extra_info.inline_data = p; |
| 3790 | p += extra_info.inline_len; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3791 | } |
| 3792 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3793 | if (msg_version >= 5) { |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 3794 | struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc; |
| 3795 | u32 epoch_barrier; |
| 3796 | |
| 3797 | ceph_decode_32_safe(&p, end, epoch_barrier, bad); |
| 3798 | ceph_osdc_update_epoch_barrier(osdc, epoch_barrier); |
| 3799 | } |
| 3800 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3801 | if (msg_version >= 8) { |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 3802 | u64 flush_tid; |
| 3803 | u32 caller_uid, caller_gid; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3804 | u32 pool_ns_len; |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 3805 | |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 3806 | /* version >= 6 */ |
| 3807 | ceph_decode_64_safe(&p, end, flush_tid, bad); |
| 3808 | /* version >= 7 */ |
| 3809 | ceph_decode_32_safe(&p, end, caller_uid, bad); |
| 3810 | ceph_decode_32_safe(&p, end, caller_gid, bad); |
| 3811 | /* version >= 8 */ |
| 3812 | ceph_decode_32_safe(&p, end, pool_ns_len, bad); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3813 | if (pool_ns_len > 0) { |
| 3814 | ceph_decode_need(&p, end, pool_ns_len, bad); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3815 | extra_info.pool_ns = |
| 3816 | ceph_find_or_create_string(p, pool_ns_len); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3817 | p += pool_ns_len; |
| 3818 | } |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 3819 | } |
| 3820 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3821 | if (msg_version >= 11) { |
| 3822 | struct ceph_timespec *btime; |
| 3823 | u64 change_attr; |
| 3824 | u32 flags; |
| 3825 | |
| 3826 | /* version >= 9 */ |
| 3827 | if (p + sizeof(*btime) > end) |
| 3828 | goto bad; |
| 3829 | btime = p; |
| 3830 | p += sizeof(*btime); |
| 3831 | ceph_decode_64_safe(&p, end, change_attr, bad); |
| 3832 | /* version >= 10 */ |
| 3833 | ceph_decode_32_safe(&p, end, flags, bad); |
| 3834 | /* version >= 11 */ |
| 3835 | extra_info.dirstat_valid = true; |
| 3836 | ceph_decode_64_safe(&p, end, extra_info.nfiles, bad); |
| 3837 | ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad); |
| 3838 | } |
| 3839 | |
Yan, Zheng | 6cd3bca | 2014-09-17 07:45:12 +0800 | [diff] [blame] | 3840 | /* lookup ino */ |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3841 | inode = ceph_find_inode(mdsc->fsc->sb, vino); |
Yan, Zheng | 6cd3bca | 2014-09-17 07:45:12 +0800 | [diff] [blame] | 3842 | ci = ceph_inode(inode); |
| 3843 | dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino, |
| 3844 | vino.snap, inode); |
| 3845 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3846 | mutex_lock(&session->s_mutex); |
| 3847 | session->s_seq++; |
| 3848 | dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq, |
| 3849 | (unsigned)seq); |
| 3850 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3851 | if (!inode) { |
| 3852 | dout(" i don't have ino %llx\n", vino.ino); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 3853 | |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 3854 | if (op == CEPH_CAP_OP_IMPORT) { |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 3855 | cap = ceph_get_cap(mdsc, NULL); |
| 3856 | cap->cap_ino = vino.ino; |
| 3857 | cap->queue_release = 1; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3858 | cap->cap_id = le64_to_cpu(h->cap_id); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 3859 | cap->mseq = mseq; |
| 3860 | cap->seq = seq; |
Yan, Zheng | dc24de8 | 2016-11-17 19:55:30 +0800 | [diff] [blame] | 3861 | cap->issue_seq = seq; |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 3862 | spin_lock(&session->s_cap_lock); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 3863 | list_add_tail(&cap->session_caps, |
| 3864 | &session->s_cap_releases); |
| 3865 | session->s_num_cap_releases++; |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 3866 | spin_unlock(&session->s_cap_lock); |
| 3867 | } |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 3868 | goto flush_cap_releases; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | /* these will work even if we don't have a cap yet */ |
| 3872 | switch (op) { |
| 3873 | case CEPH_CAP_OP_FLUSHSNAP_ACK: |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3874 | handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid), |
| 3875 | h, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3876 | goto done; |
| 3877 | |
| 3878 | case CEPH_CAP_OP_EXPORT: |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3879 | handle_cap_export(inode, h, peer, session); |
| 3880 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3881 | |
| 3882 | case CEPH_CAP_OP_IMPORT: |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 3883 | realm = NULL; |
| 3884 | if (snaptrace_len) { |
| 3885 | down_write(&mdsc->snap_rwsem); |
| 3886 | ceph_update_snap_trace(mdsc, snaptrace, |
| 3887 | snaptrace + snaptrace_len, |
| 3888 | false, &realm); |
| 3889 | downgrade_write(&mdsc->snap_rwsem); |
| 3890 | } else { |
| 3891 | down_read(&mdsc->snap_rwsem); |
| 3892 | } |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3893 | handle_cap_import(mdsc, inode, h, peer, session, |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3894 | &cap, &extra_info.issued); |
| 3895 | handle_cap_grant(inode, session, cap, |
| 3896 | h, msg->middle, &extra_info); |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 3897 | if (realm) |
| 3898 | ceph_put_snap_realm(mdsc, realm); |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3899 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | /* the rest require a cap */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3903 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3904 | cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3905 | if (!cap) { |
Sage Weil | 9dbd412 | 2010-06-10 13:21:20 -0700 | [diff] [blame] | 3906 | dout(" no cap on %p ino %llx.%llx from mds%d\n", |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3907 | inode, ceph_ino(inode), ceph_snap(inode), |
| 3908 | session->s_mds); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3909 | spin_unlock(&ci->i_ceph_lock); |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 3910 | goto flush_cap_releases; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3913 | /* note that each of these drops i_ceph_lock for us */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3914 | switch (op) { |
| 3915 | case CEPH_CAP_OP_REVOKE: |
| 3916 | case CEPH_CAP_OP_GRANT: |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3917 | __ceph_caps_issued(ci, &extra_info.issued); |
| 3918 | extra_info.issued |= __ceph_caps_dirty(ci); |
| 3919 | handle_cap_grant(inode, session, cap, |
| 3920 | h, msg->middle, &extra_info); |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 3921 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3922 | |
| 3923 | case CEPH_CAP_OP_FLUSH_ACK: |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3924 | handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid), |
| 3925 | h, session, cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3926 | break; |
| 3927 | |
| 3928 | case CEPH_CAP_OP_TRUNC: |
| 3929 | handle_cap_trunc(inode, h, session); |
| 3930 | break; |
| 3931 | |
| 3932 | default: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3933 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3934 | pr_err("ceph_handle_caps: unknown cap op %d %s\n", op, |
| 3935 | ceph_cap_op_name(op)); |
| 3936 | } |
| 3937 | |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 3938 | goto done; |
| 3939 | |
| 3940 | flush_cap_releases: |
| 3941 | /* |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 3942 | * send any cap release message to try to move things |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 3943 | * along for the mds (who clearly thinks we still have this |
| 3944 | * cap). |
| 3945 | */ |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 3946 | ceph_send_cap_releases(mdsc, session); |
| 3947 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3948 | done: |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 3949 | mutex_unlock(&session->s_mutex); |
| 3950 | done_unlocked: |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 3951 | iput(inode); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3952 | ceph_put_string(extra_info.pool_ns); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3953 | return; |
| 3954 | |
| 3955 | bad: |
| 3956 | pr_err("ceph_handle_caps: corrupt message\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 3957 | ceph_msg_dump(msg); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3958 | return; |
| 3959 | } |
| 3960 | |
| 3961 | /* |
| 3962 | * Delayed work handler to process end of delayed cap release LRU list. |
| 3963 | */ |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3964 | void ceph_check_delayed_caps(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3965 | { |
Yan, Zheng | 4b9f204 | 2017-06-27 17:17:24 +0800 | [diff] [blame] | 3966 | struct inode *inode; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3967 | struct ceph_inode_info *ci; |
| 3968 | int flags = CHECK_CAPS_NODELAY; |
| 3969 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3970 | dout("check_delayed_caps\n"); |
| 3971 | while (1) { |
| 3972 | spin_lock(&mdsc->cap_delay_lock); |
| 3973 | if (list_empty(&mdsc->cap_delay_list)) |
| 3974 | break; |
| 3975 | ci = list_first_entry(&mdsc->cap_delay_list, |
| 3976 | struct ceph_inode_info, |
| 3977 | i_cap_delay_list); |
| 3978 | if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && |
| 3979 | time_before(jiffies, ci->i_hold_caps_max)) |
| 3980 | break; |
| 3981 | list_del_init(&ci->i_cap_delay_list); |
Yan, Zheng | 4b9f204 | 2017-06-27 17:17:24 +0800 | [diff] [blame] | 3982 | |
| 3983 | inode = igrab(&ci->vfs_inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3984 | spin_unlock(&mdsc->cap_delay_lock); |
Yan, Zheng | 4b9f204 | 2017-06-27 17:17:24 +0800 | [diff] [blame] | 3985 | |
| 3986 | if (inode) { |
| 3987 | dout("check_delayed_caps on %p\n", inode); |
| 3988 | ceph_check_caps(ci, flags, NULL); |
| 3989 | iput(inode); |
| 3990 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3991 | } |
| 3992 | spin_unlock(&mdsc->cap_delay_lock); |
| 3993 | } |
| 3994 | |
| 3995 | /* |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3996 | * Flush all dirty caps to the mds |
| 3997 | */ |
| 3998 | void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) |
| 3999 | { |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4000 | struct ceph_inode_info *ci; |
| 4001 | struct inode *inode; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4002 | |
| 4003 | dout("flush_dirty_caps\n"); |
| 4004 | spin_lock(&mdsc->cap_dirty_lock); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4005 | while (!list_empty(&mdsc->cap_dirty)) { |
| 4006 | ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info, |
| 4007 | i_dirty_item); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 4008 | inode = &ci->vfs_inode; |
| 4009 | ihold(inode); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4010 | dout("flush_dirty_caps %p\n", inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4011 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 4012 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL); |
| 4013 | iput(inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4014 | spin_lock(&mdsc->cap_dirty_lock); |
| 4015 | } |
| 4016 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4017 | dout("flush_dirty_caps done\n"); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4018 | } |
| 4019 | |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 4020 | void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode) |
| 4021 | { |
| 4022 | int i; |
| 4023 | int bits = (fmode << 1) | 1; |
| 4024 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { |
| 4025 | if (bits & (1 << i)) |
| 4026 | ci->i_nr_by_mode[i]++; |
| 4027 | } |
| 4028 | } |
| 4029 | |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4030 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4031 | * Drop open file reference. If we were the last open file, |
| 4032 | * we may need to release capabilities to the MDS (or schedule |
| 4033 | * their delayed release). |
| 4034 | */ |
| 4035 | void ceph_put_fmode(struct ceph_inode_info *ci, int fmode) |
| 4036 | { |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 4037 | int i, last = 0; |
| 4038 | int bits = (fmode << 1) | 1; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4039 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 4040 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { |
| 4041 | if (bits & (1 << i)) { |
| 4042 | BUG_ON(ci->i_nr_by_mode[i] == 0); |
| 4043 | if (--ci->i_nr_by_mode[i] == 0) |
| 4044 | last++; |
| 4045 | } |
| 4046 | } |
| 4047 | dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n", |
| 4048 | &ci->vfs_inode, fmode, |
| 4049 | ci->i_nr_by_mode[0], ci->i_nr_by_mode[1], |
| 4050 | ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4051 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4052 | |
| 4053 | if (last && ci->i_vino.snap == CEPH_NOSNAP) |
| 4054 | ceph_check_caps(ci, 0, NULL); |
| 4055 | } |
| 4056 | |
| 4057 | /* |
Zhi Zhang | 6ef0bc6 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 4058 | * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it |
| 4059 | * looks like the link count will hit 0, drop any other caps (other |
| 4060 | * than PIN) we don't specifically want (due to the file still being |
| 4061 | * open). |
| 4062 | */ |
| 4063 | int ceph_drop_caps_for_unlink(struct inode *inode) |
| 4064 | { |
| 4065 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 4066 | int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; |
| 4067 | |
| 4068 | spin_lock(&ci->i_ceph_lock); |
| 4069 | if (inode->i_nlink == 1) { |
| 4070 | drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN); |
| 4071 | |
| 4072 | ci->i_ceph_flags |= CEPH_I_NODELAY; |
| 4073 | if (__ceph_caps_dirty(ci)) { |
| 4074 | struct ceph_mds_client *mdsc = |
| 4075 | ceph_inode_to_client(inode)->mdsc; |
| 4076 | __cap_delay_requeue_front(mdsc, ci); |
| 4077 | } |
| 4078 | } |
| 4079 | spin_unlock(&ci->i_ceph_lock); |
| 4080 | return drop; |
| 4081 | } |
| 4082 | |
| 4083 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4084 | * Helpers for embedding cap and dentry lease releases into mds |
| 4085 | * requests. |
| 4086 | * |
| 4087 | * @force is used by dentry_release (below) to force inclusion of a |
| 4088 | * record for the directory inode, even when there aren't any caps to |
| 4089 | * drop. |
| 4090 | */ |
| 4091 | int ceph_encode_inode_release(void **p, struct inode *inode, |
| 4092 | int mds, int drop, int unless, int force) |
| 4093 | { |
| 4094 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 4095 | struct ceph_cap *cap; |
| 4096 | struct ceph_mds_request_release *rel = *p; |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 4097 | int used, dirty; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4098 | int ret = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4099 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4100 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 4101 | used = __ceph_caps_used(ci); |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 4102 | dirty = __ceph_caps_dirty(ci); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 4103 | |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 4104 | dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n", |
| 4105 | inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop), |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 4106 | ceph_cap_string(unless)); |
| 4107 | |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 4108 | /* only drop unused, clean caps */ |
| 4109 | drop &= ~(used | dirty); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 4110 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4111 | cap = __get_cap_for_mds(ci, mds); |
| 4112 | if (cap && __cap_is_valid(cap)) { |
Yan, Zheng | 222b7f9 | 2017-11-23 17:47:15 +0800 | [diff] [blame] | 4113 | unless &= cap->issued; |
| 4114 | if (unless) { |
| 4115 | if (unless & CEPH_CAP_AUTH_EXCL) |
| 4116 | drop &= ~CEPH_CAP_AUTH_SHARED; |
| 4117 | if (unless & CEPH_CAP_LINK_EXCL) |
| 4118 | drop &= ~CEPH_CAP_LINK_SHARED; |
| 4119 | if (unless & CEPH_CAP_XATTR_EXCL) |
| 4120 | drop &= ~CEPH_CAP_XATTR_SHARED; |
| 4121 | if (unless & CEPH_CAP_FILE_EXCL) |
| 4122 | drop &= ~CEPH_CAP_FILE_SHARED; |
| 4123 | } |
| 4124 | |
| 4125 | if (force || (cap->issued & drop)) { |
| 4126 | if (cap->issued & drop) { |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 4127 | int wanted = __ceph_caps_wanted(ci); |
| 4128 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0) |
| 4129 | wanted |= cap->mds_wanted; |
| 4130 | dout("encode_inode_release %p cap %p " |
| 4131 | "%s -> %s, wanted %s -> %s\n", inode, cap, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4132 | ceph_cap_string(cap->issued), |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 4133 | ceph_cap_string(cap->issued & ~drop), |
| 4134 | ceph_cap_string(cap->mds_wanted), |
| 4135 | ceph_cap_string(wanted)); |
| 4136 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4137 | cap->issued &= ~drop; |
| 4138 | cap->implemented &= ~drop; |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 4139 | cap->mds_wanted = wanted; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4140 | } else { |
| 4141 | dout("encode_inode_release %p cap %p %s" |
| 4142 | " (force)\n", inode, cap, |
| 4143 | ceph_cap_string(cap->issued)); |
| 4144 | } |
| 4145 | |
| 4146 | rel->ino = cpu_to_le64(ceph_ino(inode)); |
| 4147 | rel->cap_id = cpu_to_le64(cap->cap_id); |
| 4148 | rel->seq = cpu_to_le32(cap->seq); |
Himangi Saraogi | 08a0f24 | 2014-07-23 20:11:11 +0530 | [diff] [blame] | 4149 | rel->issue_seq = cpu_to_le32(cap->issue_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4150 | rel->mseq = cpu_to_le32(cap->mseq); |
Yan, Zheng | fd7b95c | 2014-04-17 08:02:02 +0800 | [diff] [blame] | 4151 | rel->caps = cpu_to_le32(cap->implemented); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4152 | rel->wanted = cpu_to_le32(cap->mds_wanted); |
| 4153 | rel->dname_len = 0; |
| 4154 | rel->dname_seq = 0; |
| 4155 | *p += sizeof(*rel); |
| 4156 | ret = 1; |
| 4157 | } else { |
Yan, Zheng | 222b7f9 | 2017-11-23 17:47:15 +0800 | [diff] [blame] | 4158 | dout("encode_inode_release %p cap %p %s (noop)\n", |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4159 | inode, cap, ceph_cap_string(cap->issued)); |
| 4160 | } |
| 4161 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4162 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4163 | return ret; |
| 4164 | } |
| 4165 | |
| 4166 | int ceph_encode_dentry_release(void **p, struct dentry *dentry, |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 4167 | struct inode *dir, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4168 | int mds, int drop, int unless) |
| 4169 | { |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 4170 | struct dentry *parent = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4171 | struct ceph_mds_request_release *rel = *p; |
| 4172 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 4173 | int force = 0; |
| 4174 | int ret; |
| 4175 | |
| 4176 | /* |
| 4177 | * force an record for the directory caps if we have a dentry lease. |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4178 | * this is racy (can't take i_ceph_lock and d_lock together), but it |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4179 | * doesn't have to be perfect; the mds will revoke anything we don't |
| 4180 | * release. |
| 4181 | */ |
| 4182 | spin_lock(&dentry->d_lock); |
| 4183 | if (di->lease_session && di->lease_session->s_mds == mds) |
| 4184 | force = 1; |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 4185 | if (!dir) { |
| 4186 | parent = dget(dentry->d_parent); |
| 4187 | dir = d_inode(parent); |
| 4188 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4189 | spin_unlock(&dentry->d_lock); |
| 4190 | |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 4191 | ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); |
Jeff Layton | adf0d68 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 4192 | dput(parent); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4193 | |
| 4194 | spin_lock(&dentry->d_lock); |
| 4195 | if (ret && di->lease_session && di->lease_session->s_mds == mds) { |
| 4196 | dout("encode_dentry_release %p mds%d seq %d\n", |
| 4197 | dentry, mds, (int)di->lease_seq); |
| 4198 | rel->dname_len = cpu_to_le32(dentry->d_name.len); |
| 4199 | memcpy(*p, dentry->d_name.name, dentry->d_name.len); |
| 4200 | *p += dentry->d_name.len; |
| 4201 | rel->dname_seq = cpu_to_le32(di->lease_seq); |
Sage Weil | 1dadcce | 2010-07-23 13:54:21 -0700 | [diff] [blame] | 4202 | __ceph_mdsc_drop_dentry_lease(dentry); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4203 | } |
| 4204 | spin_unlock(&dentry->d_lock); |
| 4205 | return ret; |
| 4206 | } |