Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2 | |
| 3 | #include <linux/fs.h> |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 7 | #include <linux/vmalloc.h> |
| 8 | #include <linux/wait.h> |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 9 | #include <linux/writeback.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 10 | |
| 11 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 12 | #include "mds_client.h" |
| 13 | #include <linux/ceph/decode.h> |
| 14 | #include <linux/ceph/messenger.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * Capability management |
| 18 | * |
| 19 | * The Ceph metadata servers control client access to inode metadata |
| 20 | * and file data by issuing capabilities, granting clients permission |
| 21 | * to read and/or write both inode field and file data to OSDs |
| 22 | * (storage nodes). Each capability consists of a set of bits |
| 23 | * indicating which operations are allowed. |
| 24 | * |
| 25 | * If the client holds a *_SHARED cap, the client has a coherent value |
| 26 | * that can be safely read from the cached inode. |
| 27 | * |
| 28 | * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the |
| 29 | * client is allowed to change inode attributes (e.g., file size, |
| 30 | * mtime), note its dirty state in the ceph_cap, and asynchronously |
| 31 | * flush that metadata change to the MDS. |
| 32 | * |
| 33 | * In the event of a conflicting operation (perhaps by another |
| 34 | * client), the MDS will revoke the conflicting client capabilities. |
| 35 | * |
| 36 | * In order for a client to cache an inode, it must hold a capability |
| 37 | * with at least one MDS server. When inodes are released, release |
| 38 | * notifications are batched and periodically sent en masse to the MDS |
| 39 | * cluster to release server state. |
| 40 | */ |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | * Generate readable cap strings for debugging output. |
| 45 | */ |
| 46 | #define MAX_CAP_STR 20 |
| 47 | static char cap_str[MAX_CAP_STR][40]; |
| 48 | static DEFINE_SPINLOCK(cap_str_lock); |
| 49 | static int last_cap_str; |
| 50 | |
| 51 | static char *gcap_string(char *s, int c) |
| 52 | { |
| 53 | if (c & CEPH_CAP_GSHARED) |
| 54 | *s++ = 's'; |
| 55 | if (c & CEPH_CAP_GEXCL) |
| 56 | *s++ = 'x'; |
| 57 | if (c & CEPH_CAP_GCACHE) |
| 58 | *s++ = 'c'; |
| 59 | if (c & CEPH_CAP_GRD) |
| 60 | *s++ = 'r'; |
| 61 | if (c & CEPH_CAP_GWR) |
| 62 | *s++ = 'w'; |
| 63 | if (c & CEPH_CAP_GBUFFER) |
| 64 | *s++ = 'b'; |
| 65 | if (c & CEPH_CAP_GLAZYIO) |
| 66 | *s++ = 'l'; |
| 67 | return s; |
| 68 | } |
| 69 | |
| 70 | const char *ceph_cap_string(int caps) |
| 71 | { |
| 72 | int i; |
| 73 | char *s; |
| 74 | int c; |
| 75 | |
| 76 | spin_lock(&cap_str_lock); |
| 77 | i = last_cap_str++; |
| 78 | if (last_cap_str == MAX_CAP_STR) |
| 79 | last_cap_str = 0; |
| 80 | spin_unlock(&cap_str_lock); |
| 81 | |
| 82 | s = cap_str[i]; |
| 83 | |
| 84 | if (caps & CEPH_CAP_PIN) |
| 85 | *s++ = 'p'; |
| 86 | |
| 87 | c = (caps >> CEPH_CAP_SAUTH) & 3; |
| 88 | if (c) { |
| 89 | *s++ = 'A'; |
| 90 | s = gcap_string(s, c); |
| 91 | } |
| 92 | |
| 93 | c = (caps >> CEPH_CAP_SLINK) & 3; |
| 94 | if (c) { |
| 95 | *s++ = 'L'; |
| 96 | s = gcap_string(s, c); |
| 97 | } |
| 98 | |
| 99 | c = (caps >> CEPH_CAP_SXATTR) & 3; |
| 100 | if (c) { |
| 101 | *s++ = 'X'; |
| 102 | s = gcap_string(s, c); |
| 103 | } |
| 104 | |
| 105 | c = caps >> CEPH_CAP_SFILE; |
| 106 | if (c) { |
| 107 | *s++ = 'F'; |
| 108 | s = gcap_string(s, c); |
| 109 | } |
| 110 | |
| 111 | if (s == cap_str[i]) |
| 112 | *s++ = '-'; |
| 113 | *s = 0; |
| 114 | return cap_str[i]; |
| 115 | } |
| 116 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 117 | void ceph_caps_init(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 118 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 119 | INIT_LIST_HEAD(&mdsc->caps_list); |
| 120 | spin_lock_init(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 123 | void ceph_caps_finalize(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 124 | { |
| 125 | struct ceph_cap *cap; |
| 126 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 127 | spin_lock(&mdsc->caps_list_lock); |
| 128 | while (!list_empty(&mdsc->caps_list)) { |
| 129 | cap = list_first_entry(&mdsc->caps_list, |
| 130 | struct ceph_cap, caps_item); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 131 | list_del(&cap->caps_item); |
| 132 | kmem_cache_free(ceph_cap_cachep, cap); |
| 133 | } |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 134 | mdsc->caps_total_count = 0; |
| 135 | mdsc->caps_avail_count = 0; |
| 136 | mdsc->caps_use_count = 0; |
| 137 | mdsc->caps_reserve_count = 0; |
| 138 | mdsc->caps_min_count = 0; |
| 139 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 142 | void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta) |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 143 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 144 | spin_lock(&mdsc->caps_list_lock); |
| 145 | mdsc->caps_min_count += delta; |
| 146 | BUG_ON(mdsc->caps_min_count < 0); |
| 147 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 148 | } |
| 149 | |
majianpeng | 93faca6 | 2013-06-26 11:15:27 +0800 | [diff] [blame^] | 150 | void ceph_reserve_caps(struct ceph_mds_client *mdsc, |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 151 | struct ceph_cap_reservation *ctx, int need) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 152 | { |
| 153 | int i; |
| 154 | struct ceph_cap *cap; |
| 155 | int have; |
| 156 | int alloc = 0; |
| 157 | LIST_HEAD(newcaps); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 158 | |
| 159 | dout("reserve caps ctx=%p need=%d\n", ctx, need); |
| 160 | |
| 161 | /* first reserve any caps that are already allocated */ |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 162 | spin_lock(&mdsc->caps_list_lock); |
| 163 | if (mdsc->caps_avail_count >= need) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 164 | have = need; |
| 165 | else |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 166 | have = mdsc->caps_avail_count; |
| 167 | mdsc->caps_avail_count -= have; |
| 168 | mdsc->caps_reserve_count += have; |
| 169 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 170 | mdsc->caps_reserve_count + |
| 171 | mdsc->caps_avail_count); |
| 172 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 173 | |
| 174 | for (i = have; i < need; i++) { |
| 175 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
majianpeng | 93faca6 | 2013-06-26 11:15:27 +0800 | [diff] [blame^] | 176 | if (!cap) |
| 177 | break; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 178 | list_add(&cap->caps_item, &newcaps); |
| 179 | alloc++; |
| 180 | } |
majianpeng | 93faca6 | 2013-06-26 11:15:27 +0800 | [diff] [blame^] | 181 | /* we didn't manage to reserve as much as we needed */ |
| 182 | if (have + alloc != need) |
| 183 | pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n", |
| 184 | ctx, need, have + alloc); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 185 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 186 | spin_lock(&mdsc->caps_list_lock); |
| 187 | mdsc->caps_total_count += alloc; |
| 188 | mdsc->caps_reserve_count += alloc; |
| 189 | list_splice(&newcaps, &mdsc->caps_list); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 190 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 191 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 192 | mdsc->caps_reserve_count + |
| 193 | mdsc->caps_avail_count); |
| 194 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 195 | |
| 196 | ctx->count = need; |
| 197 | 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] | 198 | ctx, mdsc->caps_total_count, mdsc->caps_use_count, |
| 199 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 202 | int ceph_unreserve_caps(struct ceph_mds_client *mdsc, |
| 203 | struct ceph_cap_reservation *ctx) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 204 | { |
| 205 | dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count); |
| 206 | if (ctx->count) { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 207 | spin_lock(&mdsc->caps_list_lock); |
| 208 | BUG_ON(mdsc->caps_reserve_count < ctx->count); |
| 209 | mdsc->caps_reserve_count -= ctx->count; |
| 210 | mdsc->caps_avail_count += ctx->count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 211 | ctx->count = 0; |
| 212 | dout("unreserve caps %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 213 | mdsc->caps_total_count, mdsc->caps_use_count, |
| 214 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
| 215 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 216 | mdsc->caps_reserve_count + |
| 217 | mdsc->caps_avail_count); |
| 218 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 223 | static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc, |
| 224 | struct ceph_cap_reservation *ctx) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 225 | { |
| 226 | struct ceph_cap *cap = NULL; |
| 227 | |
| 228 | /* temporary, until we do something about cap import/export */ |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 229 | if (!ctx) { |
| 230 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
| 231 | if (cap) { |
Yan, Zheng | 4d1d053 | 2012-11-03 10:32:37 +0800 | [diff] [blame] | 232 | spin_lock(&mdsc->caps_list_lock); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 233 | mdsc->caps_use_count++; |
| 234 | mdsc->caps_total_count++; |
Yan, Zheng | 4d1d053 | 2012-11-03 10:32:37 +0800 | [diff] [blame] | 235 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 236 | } |
| 237 | return cap; |
| 238 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 239 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 240 | spin_lock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 241 | 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] | 242 | ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count, |
| 243 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 244 | BUG_ON(!ctx->count); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 245 | BUG_ON(ctx->count > mdsc->caps_reserve_count); |
| 246 | BUG_ON(list_empty(&mdsc->caps_list)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 247 | |
| 248 | ctx->count--; |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 249 | mdsc->caps_reserve_count--; |
| 250 | mdsc->caps_use_count++; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 251 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 252 | cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 253 | list_del(&cap->caps_item); |
| 254 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 255 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 256 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 257 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 258 | return cap; |
| 259 | } |
| 260 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 261 | 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] | 262 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 263 | spin_lock(&mdsc->caps_list_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 264 | dout("put_cap %p %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 265 | cap, mdsc->caps_total_count, mdsc->caps_use_count, |
| 266 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
| 267 | mdsc->caps_use_count--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 268 | /* |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 269 | * Keep some preallocated caps around (ceph_min_count), to |
| 270 | * avoid lots of free/alloc churn. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 271 | */ |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 272 | if (mdsc->caps_avail_count >= mdsc->caps_reserve_count + |
| 273 | mdsc->caps_min_count) { |
| 274 | mdsc->caps_total_count--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 275 | kmem_cache_free(ceph_cap_cachep, cap); |
| 276 | } else { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 277 | mdsc->caps_avail_count++; |
| 278 | list_add(&cap->caps_item, &mdsc->caps_list); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 281 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 282 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 283 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 286 | void ceph_reservation_status(struct ceph_fs_client *fsc, |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 287 | int *total, int *avail, int *used, int *reserved, |
| 288 | int *min) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 289 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 290 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 291 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 292 | if (total) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 293 | *total = mdsc->caps_total_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 294 | if (avail) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 295 | *avail = mdsc->caps_avail_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 296 | if (used) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 297 | *used = mdsc->caps_use_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 298 | if (reserved) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 299 | *reserved = mdsc->caps_reserve_count; |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 300 | if (min) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 301 | *min = mdsc->caps_min_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Find ceph_cap for given mds, if any. |
| 306 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 307 | * Called with i_ceph_lock held. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 308 | */ |
| 309 | static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
| 310 | { |
| 311 | struct ceph_cap *cap; |
| 312 | struct rb_node *n = ci->i_caps.rb_node; |
| 313 | |
| 314 | while (n) { |
| 315 | cap = rb_entry(n, struct ceph_cap, ci_node); |
| 316 | if (mds < cap->mds) |
| 317 | n = n->rb_left; |
| 318 | else if (mds > cap->mds) |
| 319 | n = n->rb_right; |
| 320 | else |
| 321 | return cap; |
| 322 | } |
| 323 | return NULL; |
| 324 | } |
| 325 | |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 326 | struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
| 327 | { |
| 328 | struct ceph_cap *cap; |
| 329 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 330 | spin_lock(&ci->i_ceph_lock); |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 331 | cap = __get_cap_for_mds(ci, mds); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 332 | spin_unlock(&ci->i_ceph_lock); |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 333 | return cap; |
| 334 | } |
| 335 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 336 | /* |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 337 | * 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] | 338 | */ |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 339 | static int __ceph_get_cap_mds(struct ceph_inode_info *ci) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 340 | { |
| 341 | struct ceph_cap *cap; |
| 342 | int mds = -1; |
| 343 | struct rb_node *p; |
| 344 | |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 345 | /* prefer mds with WR|BUFFER|EXCL caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 346 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 347 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 348 | mds = cap->mds; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 349 | if (cap->issued & (CEPH_CAP_FILE_WR | |
| 350 | CEPH_CAP_FILE_BUFFER | |
| 351 | CEPH_CAP_FILE_EXCL)) |
| 352 | break; |
| 353 | } |
| 354 | return mds; |
| 355 | } |
| 356 | |
| 357 | int ceph_get_cap_mds(struct inode *inode) |
| 358 | { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 359 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 360 | int mds; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 361 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 362 | mds = __ceph_get_cap_mds(ceph_inode(inode)); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 363 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 364 | return mds; |
| 365 | } |
| 366 | |
| 367 | /* |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 368 | * Called under i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 369 | */ |
| 370 | static void __insert_cap_node(struct ceph_inode_info *ci, |
| 371 | struct ceph_cap *new) |
| 372 | { |
| 373 | struct rb_node **p = &ci->i_caps.rb_node; |
| 374 | struct rb_node *parent = NULL; |
| 375 | struct ceph_cap *cap = NULL; |
| 376 | |
| 377 | while (*p) { |
| 378 | parent = *p; |
| 379 | cap = rb_entry(parent, struct ceph_cap, ci_node); |
| 380 | if (new->mds < cap->mds) |
| 381 | p = &(*p)->rb_left; |
| 382 | else if (new->mds > cap->mds) |
| 383 | p = &(*p)->rb_right; |
| 384 | else |
| 385 | BUG(); |
| 386 | } |
| 387 | |
| 388 | rb_link_node(&new->ci_node, parent, p); |
| 389 | rb_insert_color(&new->ci_node, &ci->i_caps); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * (re)set cap hold timeouts, which control the delayed release |
| 394 | * of unused caps back to the MDS. Should be called on cap use. |
| 395 | */ |
| 396 | static void __cap_set_timeouts(struct ceph_mds_client *mdsc, |
| 397 | struct ceph_inode_info *ci) |
| 398 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 399 | struct ceph_mount_options *ma = mdsc->fsc->mount_options; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 400 | |
| 401 | ci->i_hold_caps_min = round_jiffies(jiffies + |
| 402 | ma->caps_wanted_delay_min * HZ); |
| 403 | ci->i_hold_caps_max = round_jiffies(jiffies + |
| 404 | ma->caps_wanted_delay_max * HZ); |
| 405 | dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode, |
| 406 | ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies); |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * (Re)queue cap at the end of the delayed cap release list. |
| 411 | * |
| 412 | * If I_FLUSH is set, leave the inode at the front of the list. |
| 413 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 414 | * Caller holds i_ceph_lock |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 415 | * -> we take mdsc->cap_delay_lock |
| 416 | */ |
| 417 | static void __cap_delay_requeue(struct ceph_mds_client *mdsc, |
| 418 | struct ceph_inode_info *ci) |
| 419 | { |
| 420 | __cap_set_timeouts(mdsc, ci); |
| 421 | dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode, |
| 422 | ci->i_ceph_flags, ci->i_hold_caps_max); |
| 423 | if (!mdsc->stopping) { |
| 424 | spin_lock(&mdsc->cap_delay_lock); |
| 425 | if (!list_empty(&ci->i_cap_delay_list)) { |
| 426 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
| 427 | goto no_change; |
| 428 | list_del_init(&ci->i_cap_delay_list); |
| 429 | } |
| 430 | list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
| 431 | no_change: |
| 432 | spin_unlock(&mdsc->cap_delay_lock); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Queue an inode for immediate writeback. Mark inode with I_FLUSH, |
| 438 | * indicating we should send a cap message to flush dirty metadata |
| 439 | * asap, and move to the front of the delayed cap list. |
| 440 | */ |
| 441 | static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, |
| 442 | struct ceph_inode_info *ci) |
| 443 | { |
| 444 | dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode); |
| 445 | spin_lock(&mdsc->cap_delay_lock); |
| 446 | ci->i_ceph_flags |= CEPH_I_FLUSH; |
| 447 | if (!list_empty(&ci->i_cap_delay_list)) |
| 448 | list_del_init(&ci->i_cap_delay_list); |
| 449 | list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
| 450 | spin_unlock(&mdsc->cap_delay_lock); |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Cancel delayed work on cap. |
| 455 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 456 | * Caller must hold i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 457 | */ |
| 458 | static void __cap_delay_cancel(struct ceph_mds_client *mdsc, |
| 459 | struct ceph_inode_info *ci) |
| 460 | { |
| 461 | dout("__cap_delay_cancel %p\n", &ci->vfs_inode); |
| 462 | if (list_empty(&ci->i_cap_delay_list)) |
| 463 | return; |
| 464 | spin_lock(&mdsc->cap_delay_lock); |
| 465 | list_del_init(&ci->i_cap_delay_list); |
| 466 | spin_unlock(&mdsc->cap_delay_lock); |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Common issue checks for add_cap, handle_cap_grant. |
| 471 | */ |
| 472 | static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, |
| 473 | unsigned issued) |
| 474 | { |
| 475 | unsigned had = __ceph_caps_issued(ci, NULL); |
| 476 | |
| 477 | /* |
| 478 | * Each time we receive FILE_CACHE anew, we increment |
| 479 | * i_rdcache_gen. |
| 480 | */ |
Sage Weil | 2962507 | 2010-05-27 10:40:43 -0700 | [diff] [blame] | 481 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
| 482 | (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 483 | ci->i_rdcache_gen++; |
| 484 | |
| 485 | /* |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 486 | * if we are newly issued FILE_SHARED, mark dir not complete; we |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 487 | * don't know what happened to this directory while we didn't |
| 488 | * have the cap. |
| 489 | */ |
| 490 | if ((issued & CEPH_CAP_FILE_SHARED) && |
| 491 | (had & CEPH_CAP_FILE_SHARED) == 0) { |
| 492 | ci->i_shared_gen++; |
Yan, Zheng | a8673d6 | 2013-02-18 16:38:14 +0800 | [diff] [blame] | 493 | if (S_ISDIR(ci->vfs_inode.i_mode)) { |
| 494 | dout(" marking %p NOT complete\n", &ci->vfs_inode); |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 495 | __ceph_dir_clear_complete(ci); |
Yan, Zheng | a8673d6 | 2013-02-18 16:38:14 +0800 | [diff] [blame] | 496 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Add a capability under the given MDS session. |
| 502 | * |
| 503 | * Caller should hold session snap_rwsem (read) and s_mutex. |
| 504 | * |
| 505 | * @fmode is the open file mode, if we are opening a file, otherwise |
| 506 | * it is < 0. (This is so we can atomically add the cap and add an |
| 507 | * open file reference to it.) |
| 508 | */ |
| 509 | int ceph_add_cap(struct inode *inode, |
| 510 | struct ceph_mds_session *session, u64 cap_id, |
| 511 | int fmode, unsigned issued, unsigned wanted, |
| 512 | unsigned seq, unsigned mseq, u64 realmino, int flags, |
| 513 | struct ceph_cap_reservation *caps_reservation) |
| 514 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 515 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 516 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 517 | struct ceph_cap *new_cap = NULL; |
| 518 | struct ceph_cap *cap; |
| 519 | int mds = session->s_mds; |
| 520 | int actual_wanted; |
| 521 | |
| 522 | dout("add_cap %p mds%d cap %llx %s seq %d\n", inode, |
| 523 | session->s_mds, cap_id, ceph_cap_string(issued), seq); |
| 524 | |
| 525 | /* |
| 526 | * If we are opening the file, include file mode wanted bits |
| 527 | * in wanted. |
| 528 | */ |
| 529 | if (fmode >= 0) |
| 530 | wanted |= ceph_caps_for_mode(fmode); |
| 531 | |
| 532 | retry: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 533 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 534 | cap = __get_cap_for_mds(ci, mds); |
| 535 | if (!cap) { |
| 536 | if (new_cap) { |
| 537 | cap = new_cap; |
| 538 | new_cap = NULL; |
| 539 | } else { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 540 | spin_unlock(&ci->i_ceph_lock); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 541 | new_cap = get_cap(mdsc, caps_reservation); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 542 | if (new_cap == NULL) |
| 543 | return -ENOMEM; |
| 544 | goto retry; |
| 545 | } |
| 546 | |
| 547 | cap->issued = 0; |
| 548 | cap->implemented = 0; |
| 549 | cap->mds = mds; |
| 550 | cap->mds_wanted = 0; |
Yan, Zheng | 964266c | 2013-02-27 09:26:09 +0800 | [diff] [blame] | 551 | cap->mseq = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 552 | |
| 553 | cap->ci = ci; |
| 554 | __insert_cap_node(ci, cap); |
| 555 | |
| 556 | /* clear out old exporting info? (i.e. on cap import) */ |
| 557 | if (ci->i_cap_exporting_mds == mds) { |
| 558 | ci->i_cap_exporting_issued = 0; |
| 559 | ci->i_cap_exporting_mseq = 0; |
| 560 | ci->i_cap_exporting_mds = -1; |
| 561 | } |
| 562 | |
| 563 | /* add to session cap list */ |
| 564 | cap->session = session; |
| 565 | spin_lock(&session->s_cap_lock); |
| 566 | list_add_tail(&cap->session_caps, &session->s_caps); |
| 567 | session->s_nr_caps++; |
| 568 | spin_unlock(&session->s_cap_lock); |
Sage Weil | 3540303 | 2011-05-12 15:13:23 -0700 | [diff] [blame] | 569 | } else if (new_cap) |
| 570 | ceph_put_cap(mdsc, new_cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 571 | |
| 572 | if (!ci->i_snap_realm) { |
| 573 | /* |
| 574 | * add this inode to the appropriate snap realm |
| 575 | */ |
| 576 | struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, |
| 577 | realmino); |
| 578 | if (realm) { |
| 579 | ceph_get_snap_realm(mdsc, realm); |
| 580 | spin_lock(&realm->inodes_with_caps_lock); |
| 581 | ci->i_snap_realm = realm; |
| 582 | list_add(&ci->i_snap_realm_item, |
| 583 | &realm->inodes_with_caps); |
| 584 | spin_unlock(&realm->inodes_with_caps_lock); |
| 585 | } else { |
| 586 | pr_err("ceph_add_cap: couldn't find snap realm %llx\n", |
| 587 | realmino); |
Sage Weil | b8cd07e | 2010-07-16 12:00:02 -0700 | [diff] [blame] | 588 | WARN_ON(!realm); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | __check_cap_issue(ci, cap, issued); |
| 593 | |
| 594 | /* |
| 595 | * If we are issued caps we don't want, or the mds' wanted |
| 596 | * value appears to be off, queue a check so we'll release |
| 597 | * later and/or update the mds wanted value. |
| 598 | */ |
| 599 | actual_wanted = __ceph_caps_wanted(ci); |
| 600 | if ((wanted & ~actual_wanted) || |
| 601 | (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { |
| 602 | dout(" issued %s, mds wanted %s, actual %s, queueing\n", |
| 603 | ceph_cap_string(issued), ceph_cap_string(wanted), |
| 604 | ceph_cap_string(actual_wanted)); |
| 605 | __cap_delay_requeue(mdsc, ci); |
| 606 | } |
| 607 | |
Yan, Zheng | b8c2f3a | 2013-05-31 16:37:11 +0800 | [diff] [blame] | 608 | if (flags & CEPH_CAP_FLAG_AUTH) { |
| 609 | if (ci->i_auth_cap == NULL || |
| 610 | ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) |
| 611 | ci->i_auth_cap = cap; |
| 612 | } else if (ci->i_auth_cap == cap) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 613 | ci->i_auth_cap = NULL; |
Yan, Zheng | 8a92a11 | 2013-01-04 14:28:07 +0800 | [diff] [blame] | 614 | spin_lock(&mdsc->cap_dirty_lock); |
| 615 | if (!list_empty(&ci->i_dirty_item)) { |
| 616 | dout(" moving %p to cap_dirty_migrating\n", inode); |
| 617 | list_move(&ci->i_dirty_item, |
| 618 | &mdsc->cap_dirty_migrating); |
| 619 | } |
| 620 | spin_unlock(&mdsc->cap_dirty_lock); |
| 621 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 622 | |
| 623 | dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n", |
| 624 | inode, ceph_vinop(inode), cap, ceph_cap_string(issued), |
| 625 | ceph_cap_string(issued|cap->issued), seq, mds); |
| 626 | cap->cap_id = cap_id; |
| 627 | cap->issued = issued; |
| 628 | cap->implemented |= issued; |
Yan, Zheng | 964266c | 2013-02-27 09:26:09 +0800 | [diff] [blame] | 629 | if (mseq > cap->mseq) |
| 630 | cap->mds_wanted = wanted; |
| 631 | else |
| 632 | cap->mds_wanted |= wanted; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 633 | cap->seq = seq; |
| 634 | cap->issue_seq = seq; |
| 635 | cap->mseq = mseq; |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 636 | cap->cap_gen = session->s_cap_gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 637 | |
| 638 | if (fmode >= 0) |
| 639 | __ceph_get_fmode(ci, fmode); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 640 | spin_unlock(&ci->i_ceph_lock); |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 641 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Return true if cap has not timed out and belongs to the current |
| 647 | * generation of the MDS session (i.e. has not gone 'stale' due to |
| 648 | * us losing touch with the mds). |
| 649 | */ |
| 650 | static int __cap_is_valid(struct ceph_cap *cap) |
| 651 | { |
| 652 | unsigned long ttl; |
Sage Weil | cdac830 | 2009-11-10 16:02:23 -0800 | [diff] [blame] | 653 | u32 gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 654 | |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 655 | spin_lock(&cap->session->s_gen_ttl_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 656 | gen = cap->session->s_cap_gen; |
| 657 | ttl = cap->session->s_cap_ttl; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 658 | spin_unlock(&cap->session->s_gen_ttl_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 659 | |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 660 | if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 661 | dout("__cap_is_valid %p cap %p issued %s " |
| 662 | "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode, |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 663 | cap, ceph_cap_string(cap->issued), cap->cap_gen, gen); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * Return set of valid cap bits issued to us. Note that caps time |
| 672 | * out, and may be invalidated in bulk if the client session times out |
| 673 | * and session->s_cap_gen is bumped. |
| 674 | */ |
| 675 | int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) |
| 676 | { |
Sage Weil | 7af8f1e | 2010-03-01 15:17:34 -0800 | [diff] [blame] | 677 | int have = ci->i_snap_caps | ci->i_cap_exporting_issued; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 678 | struct ceph_cap *cap; |
| 679 | struct rb_node *p; |
| 680 | |
| 681 | if (implemented) |
| 682 | *implemented = 0; |
| 683 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 684 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 685 | if (!__cap_is_valid(cap)) |
| 686 | continue; |
| 687 | dout("__ceph_caps_issued %p cap %p issued %s\n", |
| 688 | &ci->vfs_inode, cap, ceph_cap_string(cap->issued)); |
| 689 | have |= cap->issued; |
| 690 | if (implemented) |
| 691 | *implemented |= cap->implemented; |
| 692 | } |
| 693 | return have; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * Get cap bits issued by caps other than @ocap |
| 698 | */ |
| 699 | int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) |
| 700 | { |
| 701 | int have = ci->i_snap_caps; |
| 702 | struct ceph_cap *cap; |
| 703 | struct rb_node *p; |
| 704 | |
| 705 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 706 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 707 | if (cap == ocap) |
| 708 | continue; |
| 709 | if (!__cap_is_valid(cap)) |
| 710 | continue; |
| 711 | have |= cap->issued; |
| 712 | } |
| 713 | return have; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Move a cap to the end of the LRU (oldest caps at list head, newest |
| 718 | * at list tail). |
| 719 | */ |
| 720 | static void __touch_cap(struct ceph_cap *cap) |
| 721 | { |
| 722 | struct ceph_mds_session *s = cap->session; |
| 723 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 724 | spin_lock(&s->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 725 | if (s->s_cap_iterator == NULL) { |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 726 | dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap, |
| 727 | s->s_mds); |
| 728 | list_move_tail(&cap->session_caps, &s->s_caps); |
| 729 | } else { |
| 730 | dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n", |
| 731 | &cap->ci->vfs_inode, cap, s->s_mds); |
| 732 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 733 | spin_unlock(&s->s_cap_lock); |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Check if we hold the given mask. If so, move the cap(s) to the |
| 738 | * front of their respective LRUs. (This is the preferred way for |
| 739 | * callers to check for caps they want.) |
| 740 | */ |
| 741 | int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) |
| 742 | { |
| 743 | struct ceph_cap *cap; |
| 744 | struct rb_node *p; |
| 745 | int have = ci->i_snap_caps; |
| 746 | |
| 747 | if ((have & mask) == mask) { |
| 748 | dout("__ceph_caps_issued_mask %p snap issued %s" |
| 749 | " (mask %s)\n", &ci->vfs_inode, |
| 750 | ceph_cap_string(have), |
| 751 | ceph_cap_string(mask)); |
| 752 | return 1; |
| 753 | } |
| 754 | |
| 755 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 756 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 757 | if (!__cap_is_valid(cap)) |
| 758 | continue; |
| 759 | if ((cap->issued & mask) == mask) { |
| 760 | dout("__ceph_caps_issued_mask %p cap %p issued %s" |
| 761 | " (mask %s)\n", &ci->vfs_inode, cap, |
| 762 | ceph_cap_string(cap->issued), |
| 763 | ceph_cap_string(mask)); |
| 764 | if (touch) |
| 765 | __touch_cap(cap); |
| 766 | return 1; |
| 767 | } |
| 768 | |
| 769 | /* does a combination of caps satisfy mask? */ |
| 770 | have |= cap->issued; |
| 771 | if ((have & mask) == mask) { |
| 772 | dout("__ceph_caps_issued_mask %p combo issued %s" |
| 773 | " (mask %s)\n", &ci->vfs_inode, |
| 774 | ceph_cap_string(cap->issued), |
| 775 | ceph_cap_string(mask)); |
| 776 | if (touch) { |
| 777 | struct rb_node *q; |
| 778 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 779 | /* touch this + preceding caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 780 | __touch_cap(cap); |
| 781 | for (q = rb_first(&ci->i_caps); q != p; |
| 782 | q = rb_next(q)) { |
| 783 | cap = rb_entry(q, struct ceph_cap, |
| 784 | ci_node); |
| 785 | if (!__cap_is_valid(cap)) |
| 786 | continue; |
| 787 | __touch_cap(cap); |
| 788 | } |
| 789 | } |
| 790 | return 1; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Return true if mask caps are currently being revoked by an MDS. |
| 799 | */ |
| 800 | int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) |
| 801 | { |
| 802 | struct inode *inode = &ci->vfs_inode; |
| 803 | struct ceph_cap *cap; |
| 804 | struct rb_node *p; |
| 805 | int ret = 0; |
| 806 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 807 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 808 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 809 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 810 | if (__cap_is_valid(cap) && |
| 811 | (cap->implemented & ~cap->issued & mask)) { |
| 812 | ret = 1; |
| 813 | break; |
| 814 | } |
| 815 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 816 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 817 | dout("ceph_caps_revoking %p %s = %d\n", inode, |
| 818 | ceph_cap_string(mask), ret); |
| 819 | return ret; |
| 820 | } |
| 821 | |
| 822 | int __ceph_caps_used(struct ceph_inode_info *ci) |
| 823 | { |
| 824 | int used = 0; |
| 825 | if (ci->i_pin_ref) |
| 826 | used |= CEPH_CAP_PIN; |
| 827 | if (ci->i_rd_ref) |
| 828 | used |= CEPH_CAP_FILE_RD; |
Sage Weil | a43fb73 | 2010-09-17 09:54:08 -0700 | [diff] [blame] | 829 | if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 830 | used |= CEPH_CAP_FILE_CACHE; |
| 831 | if (ci->i_wr_ref) |
| 832 | used |= CEPH_CAP_FILE_WR; |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 833 | if (ci->i_wb_ref || ci->i_wrbuffer_ref) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 834 | used |= CEPH_CAP_FILE_BUFFER; |
| 835 | return used; |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * wanted, by virtue of open file modes |
| 840 | */ |
| 841 | int __ceph_caps_file_wanted(struct ceph_inode_info *ci) |
| 842 | { |
| 843 | int want = 0; |
| 844 | int mode; |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 845 | for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 846 | if (ci->i_nr_by_mode[mode]) |
| 847 | want |= ceph_caps_for_mode(mode); |
| 848 | return want; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Return caps we have registered with the MDS(s) as 'wanted'. |
| 853 | */ |
| 854 | int __ceph_caps_mds_wanted(struct ceph_inode_info *ci) |
| 855 | { |
| 856 | struct ceph_cap *cap; |
| 857 | struct rb_node *p; |
| 858 | int mds_wanted = 0; |
| 859 | |
| 860 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 861 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 862 | if (!__cap_is_valid(cap)) |
| 863 | continue; |
| 864 | mds_wanted |= cap->mds_wanted; |
| 865 | } |
| 866 | return mds_wanted; |
| 867 | } |
| 868 | |
| 869 | /* |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 870 | * called under i_ceph_lock |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 871 | */ |
| 872 | static int __ceph_is_any_caps(struct ceph_inode_info *ci) |
| 873 | { |
| 874 | return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0; |
| 875 | } |
| 876 | |
| 877 | /* |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 878 | * Remove a cap. Take steps to deal with a racing iterate_session_caps. |
| 879 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 880 | * caller should hold i_ceph_lock. |
Sage Weil | a636974 | 2010-02-22 13:59:00 -0800 | [diff] [blame] | 881 | * caller will not hold session s_mutex if called from destroy_inode. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 882 | */ |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 883 | void __ceph_remove_cap(struct ceph_cap *cap) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 884 | { |
| 885 | struct ceph_mds_session *session = cap->session; |
| 886 | struct ceph_inode_info *ci = cap->ci; |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 887 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 888 | ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 889 | int removed = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 890 | |
| 891 | dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); |
| 892 | |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 893 | /* remove from session list */ |
| 894 | spin_lock(&session->s_cap_lock); |
| 895 | if (session->s_cap_iterator == cap) { |
| 896 | /* not yet, we are iterating over this very cap */ |
| 897 | dout("__ceph_remove_cap delaying %p removal from session %p\n", |
| 898 | cap, cap->session); |
| 899 | } else { |
| 900 | list_del_init(&cap->session_caps); |
| 901 | session->s_nr_caps--; |
| 902 | cap->session = NULL; |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 903 | removed = 1; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 904 | } |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 905 | /* protect backpointer with s_cap_lock: see iterate_session_caps */ |
| 906 | cap->ci = NULL; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 907 | spin_unlock(&session->s_cap_lock); |
| 908 | |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 909 | /* remove from inode list */ |
| 910 | rb_erase(&cap->ci_node, &ci->i_caps); |
| 911 | if (ci->i_auth_cap == cap) |
| 912 | ci->i_auth_cap = NULL; |
| 913 | |
| 914 | if (removed) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 915 | ceph_put_cap(mdsc, cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 916 | |
| 917 | if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) { |
| 918 | struct ceph_snap_realm *realm = ci->i_snap_realm; |
| 919 | spin_lock(&realm->inodes_with_caps_lock); |
| 920 | list_del_init(&ci->i_snap_realm_item); |
| 921 | ci->i_snap_realm_counter++; |
| 922 | ci->i_snap_realm = NULL; |
| 923 | spin_unlock(&realm->inodes_with_caps_lock); |
| 924 | ceph_put_snap_realm(mdsc, realm); |
| 925 | } |
| 926 | if (!__ceph_is_any_real_caps(ci)) |
| 927 | __cap_delay_cancel(mdsc, ci); |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * Build and send a cap message to the given MDS. |
| 932 | * |
| 933 | * Caller should be holding s_mutex. |
| 934 | */ |
| 935 | static int send_cap_msg(struct ceph_mds_session *session, |
| 936 | u64 ino, u64 cid, int op, |
| 937 | int caps, int wanted, int dirty, |
| 938 | u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq, |
| 939 | u64 size, u64 max_size, |
| 940 | struct timespec *mtime, struct timespec *atime, |
| 941 | u64 time_warp_seq, |
Eric W. Biederman | 05cb11c | 2013-01-31 02:56:19 -0800 | [diff] [blame] | 942 | kuid_t uid, kgid_t gid, umode_t mode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 943 | u64 xattr_version, |
| 944 | struct ceph_buffer *xattrs_buf, |
| 945 | u64 follows) |
| 946 | { |
| 947 | struct ceph_mds_caps *fc; |
| 948 | struct ceph_msg *msg; |
| 949 | |
| 950 | dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s" |
| 951 | " seq %u/%u mseq %u follows %lld size %llu/%llu" |
| 952 | " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op), |
| 953 | cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted), |
| 954 | ceph_cap_string(dirty), |
| 955 | seq, issue_seq, mseq, follows, size, max_size, |
| 956 | xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0); |
| 957 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 958 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 959 | if (!msg) |
| 960 | return -ENOMEM; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 961 | |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 962 | msg->hdr.tid = cpu_to_le64(flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 963 | |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 964 | fc = msg->front.iov_base; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 965 | memset(fc, 0, sizeof(*fc)); |
| 966 | |
| 967 | fc->cap_id = cpu_to_le64(cid); |
| 968 | fc->op = cpu_to_le32(op); |
| 969 | fc->seq = cpu_to_le32(seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 970 | fc->issue_seq = cpu_to_le32(issue_seq); |
| 971 | fc->migrate_seq = cpu_to_le32(mseq); |
| 972 | fc->caps = cpu_to_le32(caps); |
| 973 | fc->wanted = cpu_to_le32(wanted); |
| 974 | fc->dirty = cpu_to_le32(dirty); |
| 975 | fc->ino = cpu_to_le64(ino); |
| 976 | fc->snap_follows = cpu_to_le64(follows); |
| 977 | |
| 978 | fc->size = cpu_to_le64(size); |
| 979 | fc->max_size = cpu_to_le64(max_size); |
| 980 | if (mtime) |
| 981 | ceph_encode_timespec(&fc->mtime, mtime); |
| 982 | if (atime) |
| 983 | ceph_encode_timespec(&fc->atime, atime); |
| 984 | fc->time_warp_seq = cpu_to_le32(time_warp_seq); |
| 985 | |
Eric W. Biederman | 05cb11c | 2013-01-31 02:56:19 -0800 | [diff] [blame] | 986 | fc->uid = cpu_to_le32(from_kuid(&init_user_ns, uid)); |
| 987 | fc->gid = cpu_to_le32(from_kgid(&init_user_ns, gid)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 988 | fc->mode = cpu_to_le32(mode); |
| 989 | |
| 990 | fc->xattr_version = cpu_to_le64(xattr_version); |
| 991 | if (xattrs_buf) { |
| 992 | msg->middle = ceph_buffer_get(xattrs_buf); |
| 993 | fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len); |
| 994 | msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len); |
| 995 | } |
| 996 | |
| 997 | ceph_con_send(&session->s_con, msg); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Yan, Zheng | d40ee0d | 2013-02-18 13:43:43 +0800 | [diff] [blame] | 1001 | void __queue_cap_release(struct ceph_mds_session *session, |
| 1002 | u64 ino, u64 cap_id, u32 migrate_seq, |
| 1003 | u32 issue_seq) |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 1004 | { |
| 1005 | struct ceph_msg *msg; |
| 1006 | struct ceph_mds_cap_release *head; |
| 1007 | struct ceph_mds_cap_item *item; |
| 1008 | |
| 1009 | spin_lock(&session->s_cap_lock); |
| 1010 | BUG_ON(!session->s_num_cap_releases); |
| 1011 | msg = list_first_entry(&session->s_cap_releases, |
| 1012 | struct ceph_msg, list_head); |
| 1013 | |
| 1014 | dout(" adding %llx release to mds%d msg %p (%d left)\n", |
| 1015 | ino, session->s_mds, msg, session->s_num_cap_releases); |
| 1016 | |
| 1017 | BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE); |
| 1018 | head = msg->front.iov_base; |
Wei Yongjun | b905a7f | 2012-09-28 12:59:16 +0800 | [diff] [blame] | 1019 | le32_add_cpu(&head->num, 1); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 1020 | item = msg->front.iov_base + msg->front.iov_len; |
| 1021 | item->ino = cpu_to_le64(ino); |
| 1022 | item->cap_id = cpu_to_le64(cap_id); |
| 1023 | item->migrate_seq = cpu_to_le32(migrate_seq); |
| 1024 | item->seq = cpu_to_le32(issue_seq); |
| 1025 | |
| 1026 | session->s_num_cap_releases--; |
| 1027 | |
| 1028 | msg->front.iov_len += sizeof(*item); |
| 1029 | if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { |
| 1030 | dout(" release msg %p full\n", msg); |
| 1031 | list_move_tail(&msg->list_head, &session->s_cap_releases_done); |
| 1032 | } else { |
| 1033 | dout(" release msg %p at %d/%d (%d)\n", msg, |
| 1034 | (int)le32_to_cpu(head->num), |
| 1035 | (int)CEPH_CAPS_PER_RELEASE, |
| 1036 | (int)msg->front.iov_len); |
| 1037 | } |
| 1038 | spin_unlock(&session->s_cap_lock); |
| 1039 | } |
| 1040 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1041 | /* |
Sage Weil | a636974 | 2010-02-22 13:59:00 -0800 | [diff] [blame] | 1042 | * Queue cap releases when an inode is dropped from our cache. Since |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1043 | * 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] | 1044 | */ |
| 1045 | void ceph_queue_caps_release(struct inode *inode) |
| 1046 | { |
| 1047 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1048 | struct rb_node *p; |
| 1049 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1050 | p = rb_first(&ci->i_caps); |
| 1051 | while (p) { |
| 1052 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); |
| 1053 | struct ceph_mds_session *session = cap->session; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1054 | |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 1055 | __queue_cap_release(session, ceph_ino(inode), cap->cap_id, |
| 1056 | cap->mseq, cap->issue_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1057 | p = rb_next(p); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1058 | __ceph_remove_cap(cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1059 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * 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] | 1064 | * drop i_ceph_lock and send the message. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1065 | * |
| 1066 | * Make note of max_size reported/requested from mds, revoked caps |
| 1067 | * that have now been implemented. |
| 1068 | * |
| 1069 | * Make half-hearted attempt ot to invalidate page cache if we are |
| 1070 | * dropping RDCACHE. Note that this will leave behind locked pages |
| 1071 | * that we'll then need to deal with elsewhere. |
| 1072 | * |
| 1073 | * Return non-zero if delayed release, or we experienced an error |
| 1074 | * such that the caller should requeue + retry later. |
| 1075 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1076 | * called with i_ceph_lock, then drops it. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1077 | * caller should hold snap_rwsem (read), s_mutex. |
| 1078 | */ |
| 1079 | static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, |
| 1080 | int op, int used, int want, int retain, int flushing, |
| 1081 | unsigned *pflush_tid) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1082 | __releases(cap->ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1083 | { |
| 1084 | struct ceph_inode_info *ci = cap->ci; |
| 1085 | struct inode *inode = &ci->vfs_inode; |
| 1086 | u64 cap_id = cap->cap_id; |
Sage Weil | 68c2832 | 2010-02-09 13:41:47 -0800 | [diff] [blame] | 1087 | int held, revoking, dropping, keep; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1088 | u64 seq, issue_seq, mseq, time_warp_seq, follows; |
| 1089 | u64 size, max_size; |
| 1090 | struct timespec mtime, atime; |
| 1091 | int wake = 0; |
Al Viro | 5706b27 | 2011-07-26 04:52:22 -0400 | [diff] [blame] | 1092 | umode_t mode; |
Eric W. Biederman | 05cb11c | 2013-01-31 02:56:19 -0800 | [diff] [blame] | 1093 | kuid_t uid; |
| 1094 | kgid_t gid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1095 | struct ceph_mds_session *session; |
| 1096 | u64 xattr_version = 0; |
Sage Weil | 082afec | 2010-08-22 15:16:41 -0700 | [diff] [blame] | 1097 | struct ceph_buffer *xattr_blob = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1098 | int delayed = 0; |
| 1099 | u64 flush_tid = 0; |
| 1100 | int i; |
| 1101 | int ret; |
| 1102 | |
Sage Weil | 68c2832 | 2010-02-09 13:41:47 -0800 | [diff] [blame] | 1103 | held = cap->issued | cap->implemented; |
| 1104 | revoking = cap->implemented & ~cap->issued; |
| 1105 | retain &= ~revoking; |
| 1106 | dropping = cap->issued & ~retain; |
| 1107 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1108 | dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n", |
| 1109 | inode, cap, cap->session, |
| 1110 | ceph_cap_string(held), ceph_cap_string(held & retain), |
| 1111 | ceph_cap_string(revoking)); |
| 1112 | BUG_ON((retain & CEPH_CAP_PIN) == 0); |
| 1113 | |
| 1114 | session = cap->session; |
| 1115 | |
| 1116 | /* don't release wanted unless we've waited a bit. */ |
| 1117 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && |
| 1118 | time_before(jiffies, ci->i_hold_caps_min)) { |
| 1119 | dout(" delaying issued %s -> %s, wanted %s -> %s on send\n", |
| 1120 | ceph_cap_string(cap->issued), |
| 1121 | ceph_cap_string(cap->issued & retain), |
| 1122 | ceph_cap_string(cap->mds_wanted), |
| 1123 | ceph_cap_string(want)); |
| 1124 | want |= cap->mds_wanted; |
| 1125 | retain |= cap->issued; |
| 1126 | delayed = 1; |
| 1127 | } |
| 1128 | ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH); |
| 1129 | |
| 1130 | cap->issued &= retain; /* drop bits we don't want */ |
| 1131 | if (cap->implemented & ~cap->issued) { |
| 1132 | /* |
| 1133 | * Wake up any waiters on wanted -> needed transition. |
| 1134 | * This is due to the weird transition from buffered |
| 1135 | * to sync IO... we need to flush dirty pages _before_ |
| 1136 | * allowing sync writes to avoid reordering. |
| 1137 | */ |
| 1138 | wake = 1; |
| 1139 | } |
| 1140 | cap->implemented &= cap->issued | used; |
| 1141 | cap->mds_wanted = want; |
| 1142 | |
| 1143 | if (flushing) { |
| 1144 | /* |
| 1145 | * assign a tid for flush operations so we can avoid |
| 1146 | * flush1 -> dirty1 -> flush2 -> flushack1 -> mark |
| 1147 | * clean type races. track latest tid for every bit |
| 1148 | * so we can handle flush AxFw, flush Fw, and have the |
| 1149 | * first ack clean Ax. |
| 1150 | */ |
| 1151 | flush_tid = ++ci->i_cap_flush_last_tid; |
| 1152 | if (pflush_tid) |
| 1153 | *pflush_tid = flush_tid; |
| 1154 | dout(" cap_flush_tid %d\n", (int)flush_tid); |
| 1155 | for (i = 0; i < CEPH_CAP_BITS; i++) |
| 1156 | if (flushing & (1 << i)) |
| 1157 | ci->i_cap_flush_tid[i] = flush_tid; |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 1158 | |
| 1159 | follows = ci->i_head_snapc->seq; |
| 1160 | } else { |
| 1161 | follows = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | keep = cap->implemented; |
| 1165 | seq = cap->seq; |
| 1166 | issue_seq = cap->issue_seq; |
| 1167 | mseq = cap->mseq; |
| 1168 | size = inode->i_size; |
| 1169 | ci->i_reported_size = size; |
| 1170 | max_size = ci->i_wanted_max_size; |
| 1171 | ci->i_requested_max_size = max_size; |
| 1172 | mtime = inode->i_mtime; |
| 1173 | atime = inode->i_atime; |
| 1174 | time_warp_seq = ci->i_time_warp_seq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1175 | uid = inode->i_uid; |
| 1176 | gid = inode->i_gid; |
| 1177 | mode = inode->i_mode; |
| 1178 | |
Sage Weil | 082afec | 2010-08-22 15:16:41 -0700 | [diff] [blame] | 1179 | if (flushing & CEPH_CAP_XATTR_EXCL) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1180 | __ceph_build_xattrs_blob(ci); |
Sage Weil | 082afec | 2010-08-22 15:16:41 -0700 | [diff] [blame] | 1181 | xattr_blob = ci->i_xattrs.blob; |
| 1182 | xattr_version = ci->i_xattrs.version; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1185 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1186 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1187 | ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id, |
| 1188 | op, keep, want, flushing, seq, flush_tid, issue_seq, mseq, |
| 1189 | size, max_size, &mtime, &atime, time_warp_seq, |
Sage Weil | 082afec | 2010-08-22 15:16:41 -0700 | [diff] [blame] | 1190 | uid, gid, mode, xattr_version, xattr_blob, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1191 | follows); |
| 1192 | if (ret < 0) { |
| 1193 | dout("error sending cap msg, must requeue %p\n", inode); |
| 1194 | delayed = 1; |
| 1195 | } |
| 1196 | |
| 1197 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 1198 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1199 | |
| 1200 | return delayed; |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * When a snapshot is taken, clients accumulate dirty metadata on |
| 1205 | * inodes with capabilities in ceph_cap_snaps to describe the file |
| 1206 | * state at the time the snapshot was taken. This must be flushed |
| 1207 | * asynchronously back to the MDS once sync writes complete and dirty |
| 1208 | * data is written out. |
| 1209 | * |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1210 | * Unless @again is true, skip cap_snaps that were already sent to |
| 1211 | * the MDS (i.e., during this session). |
| 1212 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1213 | * Called under i_ceph_lock. Takes s_mutex as needed. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1214 | */ |
| 1215 | void __ceph_flush_snaps(struct ceph_inode_info *ci, |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1216 | struct ceph_mds_session **psession, |
| 1217 | int again) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1218 | __releases(ci->i_ceph_lock) |
| 1219 | __acquires(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1220 | { |
| 1221 | struct inode *inode = &ci->vfs_inode; |
| 1222 | int mds; |
| 1223 | struct ceph_cap_snap *capsnap; |
| 1224 | u32 mseq; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1225 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1226 | struct ceph_mds_session *session = NULL; /* if session != NULL, we hold |
| 1227 | session->s_mutex */ |
| 1228 | u64 next_follows = 0; /* keep track of how far we've gotten through the |
| 1229 | i_cap_snaps list, and skip these entries next time |
| 1230 | around to avoid an infinite loop */ |
| 1231 | |
| 1232 | if (psession) |
| 1233 | session = *psession; |
| 1234 | |
| 1235 | dout("__flush_snaps %p\n", inode); |
| 1236 | retry: |
| 1237 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 1238 | /* avoid an infiniute loop after retry */ |
| 1239 | if (capsnap->follows < next_follows) |
| 1240 | continue; |
| 1241 | /* |
| 1242 | * we need to wait for sync writes to complete and for dirty |
| 1243 | * pages to be written out. |
| 1244 | */ |
| 1245 | if (capsnap->dirty_pages || capsnap->writing) |
Sage Weil | cfc0bf6 | 2010-09-14 15:50:59 -0700 | [diff] [blame] | 1246 | break; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1247 | |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 1248 | /* |
| 1249 | * if cap writeback already occurred, we should have dropped |
| 1250 | * the capsnap in ceph_put_wrbuffer_cap_refs. |
| 1251 | */ |
| 1252 | BUG_ON(capsnap->dirty == 0); |
| 1253 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1254 | /* pick mds, take s_mutex */ |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 1255 | if (ci->i_auth_cap == NULL) { |
| 1256 | dout("no auth cap (migrating?), doing nothing\n"); |
| 1257 | goto out; |
| 1258 | } |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1259 | |
| 1260 | /* only flush each capsnap once */ |
| 1261 | if (!again && !list_empty(&capsnap->flushing_item)) { |
| 1262 | dout("already flushed %p, skipping\n", capsnap); |
| 1263 | continue; |
| 1264 | } |
| 1265 | |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 1266 | mds = ci->i_auth_cap->session->s_mds; |
| 1267 | mseq = ci->i_auth_cap->mseq; |
| 1268 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1269 | if (session && session->s_mds != mds) { |
| 1270 | dout("oops, wrong session %p mutex\n", session); |
| 1271 | mutex_unlock(&session->s_mutex); |
| 1272 | ceph_put_mds_session(session); |
| 1273 | session = NULL; |
| 1274 | } |
| 1275 | if (!session) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1276 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1277 | mutex_lock(&mdsc->mutex); |
| 1278 | session = __ceph_lookup_mds_session(mdsc, mds); |
| 1279 | mutex_unlock(&mdsc->mutex); |
| 1280 | if (session) { |
| 1281 | dout("inverting session/ino locks on %p\n", |
| 1282 | session); |
| 1283 | mutex_lock(&session->s_mutex); |
| 1284 | } |
| 1285 | /* |
| 1286 | * if session == NULL, we raced against a cap |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 1287 | * deletion or migration. retry, and we'll |
| 1288 | * get a better @mds value next time. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1289 | */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1290 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1291 | goto retry; |
| 1292 | } |
| 1293 | |
| 1294 | capsnap->flush_tid = ++ci->i_cap_flush_last_tid; |
| 1295 | atomic_inc(&capsnap->nref); |
| 1296 | if (!list_empty(&capsnap->flushing_item)) |
| 1297 | list_del_init(&capsnap->flushing_item); |
| 1298 | list_add_tail(&capsnap->flushing_item, |
| 1299 | &session->s_cap_snaps_flushing); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1300 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1301 | |
Sage Weil | cfc0bf6 | 2010-09-14 15:50:59 -0700 | [diff] [blame] | 1302 | dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n", |
| 1303 | inode, capsnap, capsnap->follows, capsnap->flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1304 | send_cap_msg(session, ceph_vino(inode).ino, 0, |
| 1305 | CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0, |
| 1306 | capsnap->dirty, 0, capsnap->flush_tid, 0, mseq, |
| 1307 | capsnap->size, 0, |
| 1308 | &capsnap->mtime, &capsnap->atime, |
| 1309 | capsnap->time_warp_seq, |
| 1310 | capsnap->uid, capsnap->gid, capsnap->mode, |
Sage Weil | 4a625be | 2010-08-22 15:03:56 -0700 | [diff] [blame] | 1311 | capsnap->xattr_version, capsnap->xattr_blob, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1312 | capsnap->follows); |
| 1313 | |
| 1314 | next_follows = capsnap->follows + 1; |
| 1315 | ceph_put_cap_snap(capsnap); |
| 1316 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1317 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1318 | goto retry; |
| 1319 | } |
| 1320 | |
| 1321 | /* we flushed them all; remove this inode from the queue */ |
| 1322 | spin_lock(&mdsc->snap_flush_lock); |
| 1323 | list_del_init(&ci->i_snap_flush_item); |
| 1324 | spin_unlock(&mdsc->snap_flush_lock); |
| 1325 | |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 1326 | out: |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1327 | if (psession) |
| 1328 | *psession = session; |
| 1329 | else if (session) { |
| 1330 | mutex_unlock(&session->s_mutex); |
| 1331 | ceph_put_mds_session(session); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | static void ceph_flush_snaps(struct ceph_inode_info *ci) |
| 1336 | { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1337 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1338 | __ceph_flush_snaps(ci, NULL, 0); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1339 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 1343 | * Mark caps dirty. If inode is newly dirty, return the dirty flags. |
| 1344 | * Caller is then responsible for calling __mark_inode_dirty with the |
| 1345 | * returned flags value. |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1346 | */ |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 1347 | int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask) |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1348 | { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1349 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1350 | ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1351 | struct inode *inode = &ci->vfs_inode; |
| 1352 | int was = ci->i_dirty_caps; |
| 1353 | int dirty = 0; |
| 1354 | |
| 1355 | dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode, |
| 1356 | ceph_cap_string(mask), ceph_cap_string(was), |
| 1357 | ceph_cap_string(was | mask)); |
| 1358 | ci->i_dirty_caps |= mask; |
| 1359 | if (was == 0) { |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 1360 | if (!ci->i_head_snapc) |
| 1361 | ci->i_head_snapc = ceph_get_snap_context( |
| 1362 | ci->i_snap_realm->cached_context); |
Yan, Zheng | 0685235 | 2012-11-19 10:49:07 +0800 | [diff] [blame] | 1363 | dout(" inode %p now dirty snapc %p auth cap %p\n", |
| 1364 | &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1365 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
| 1366 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 0685235 | 2012-11-19 10:49:07 +0800 | [diff] [blame] | 1367 | if (ci->i_auth_cap) |
| 1368 | list_add(&ci->i_dirty_item, &mdsc->cap_dirty); |
| 1369 | else |
| 1370 | list_add(&ci->i_dirty_item, |
| 1371 | &mdsc->cap_dirty_migrating); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1372 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1373 | if (ci->i_flushing_caps == 0) { |
Sage Weil | 3772d26 | 2011-05-03 09:28:08 -0700 | [diff] [blame] | 1374 | ihold(inode); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1375 | dirty |= I_DIRTY_SYNC; |
| 1376 | } |
| 1377 | } |
| 1378 | BUG_ON(list_empty(&ci->i_dirty_item)); |
| 1379 | if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && |
| 1380 | (mask & CEPH_CAP_FILE_BUFFER)) |
| 1381 | dirty |= I_DIRTY_DATASYNC; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1382 | __cap_delay_requeue(mdsc, ci); |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 1383 | return dirty; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1387 | * Add dirty inode to the flushing list. Assigned a seq number so we |
| 1388 | * can wait for caps to flush without starving. |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1389 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1390 | * Called under i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1391 | */ |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1392 | static int __mark_caps_flushing(struct inode *inode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1393 | struct ceph_mds_session *session) |
| 1394 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1395 | 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] | 1396 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1397 | int flushing; |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 1398 | |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1399 | BUG_ON(ci->i_dirty_caps == 0); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1400 | BUG_ON(list_empty(&ci->i_dirty_item)); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1401 | |
| 1402 | flushing = ci->i_dirty_caps; |
| 1403 | dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n", |
| 1404 | ceph_cap_string(flushing), |
| 1405 | ceph_cap_string(ci->i_flushing_caps), |
| 1406 | ceph_cap_string(ci->i_flushing_caps | flushing)); |
| 1407 | ci->i_flushing_caps |= flushing; |
| 1408 | ci->i_dirty_caps = 0; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1409 | dout(" inode %p now !dirty\n", inode); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1410 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1411 | spin_lock(&mdsc->cap_dirty_lock); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1412 | list_del_init(&ci->i_dirty_item); |
| 1413 | |
| 1414 | ci->i_cap_flush_seq = ++mdsc->cap_flush_seq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1415 | if (list_empty(&ci->i_flushing_item)) { |
| 1416 | list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); |
| 1417 | mdsc->num_cap_flushing++; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1418 | dout(" inode %p now flushing seq %lld\n", inode, |
| 1419 | ci->i_cap_flush_seq); |
| 1420 | } else { |
| 1421 | list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing); |
| 1422 | dout(" inode %p now flushing (more) seq %lld\n", inode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1423 | ci->i_cap_flush_seq); |
| 1424 | } |
| 1425 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1426 | |
| 1427 | return flushing; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /* |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1431 | * try to invalidate mapping pages without blocking. |
| 1432 | */ |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1433 | static int try_nonblocking_invalidate(struct inode *inode) |
| 1434 | { |
| 1435 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1436 | u32 invalidating_gen = ci->i_rdcache_gen; |
| 1437 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1438 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1439 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1440 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1441 | |
Sage Weil | 18a3819 | 2010-09-17 10:46:44 -0700 | [diff] [blame] | 1442 | if (inode->i_data.nrpages == 0 && |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1443 | invalidating_gen == ci->i_rdcache_gen) { |
| 1444 | /* success. */ |
| 1445 | dout("try_nonblocking_invalidate %p success\n", inode); |
Sage Weil | cd045cb | 2010-11-04 11:05:05 -0700 | [diff] [blame] | 1446 | /* save any racing async invalidate some trouble */ |
| 1447 | ci->i_rdcache_revoking = ci->i_rdcache_gen - 1; |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1448 | return 0; |
| 1449 | } |
| 1450 | dout("try_nonblocking_invalidate %p failed\n", inode); |
| 1451 | return -1; |
| 1452 | } |
| 1453 | |
| 1454 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1455 | * Swiss army knife function to examine currently used and wanted |
| 1456 | * versus held caps. Release, flush, ack revoked caps to mds as |
| 1457 | * appropriate. |
| 1458 | * |
| 1459 | * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay |
| 1460 | * cap release further. |
| 1461 | * CHECK_CAPS_AUTHONLY - we should only check the auth cap |
| 1462 | * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without |
| 1463 | * further delay. |
| 1464 | */ |
| 1465 | void ceph_check_caps(struct ceph_inode_info *ci, int flags, |
| 1466 | struct ceph_mds_session *session) |
| 1467 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1468 | struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); |
| 1469 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1470 | struct inode *inode = &ci->vfs_inode; |
| 1471 | struct ceph_cap *cap; |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1472 | int file_wanted, used, cap_used; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1473 | int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */ |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1474 | int issued, implemented, want, retain, revoking, flushing = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1475 | int mds = -1; /* keep track of how far we've gone through i_caps list |
| 1476 | to avoid an infinite loop on retry */ |
| 1477 | struct rb_node *p; |
| 1478 | int tried_invalidate = 0; |
| 1479 | int delayed = 0, sent = 0, force_requeue = 0, num; |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1480 | int queue_invalidate = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1481 | int is_delayed = flags & CHECK_CAPS_NODELAY; |
| 1482 | |
| 1483 | /* if we are unmounting, flush any unused caps immediately. */ |
| 1484 | if (mdsc->stopping) |
| 1485 | is_delayed = 1; |
| 1486 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1487 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1488 | |
| 1489 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
| 1490 | flags |= CHECK_CAPS_FLUSH; |
| 1491 | |
| 1492 | /* flush snaps first time around only */ |
| 1493 | if (!list_empty(&ci->i_cap_snaps)) |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1494 | __ceph_flush_snaps(ci, &session, 0); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1495 | goto retry_locked; |
| 1496 | retry: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1497 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1498 | retry_locked: |
| 1499 | file_wanted = __ceph_caps_file_wanted(ci); |
| 1500 | used = __ceph_caps_used(ci); |
| 1501 | want = file_wanted | used; |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1502 | issued = __ceph_caps_issued(ci, &implemented); |
| 1503 | revoking = implemented & ~issued; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1504 | |
| 1505 | retain = want | CEPH_CAP_PIN; |
| 1506 | if (!mdsc->stopping && inode->i_nlink > 0) { |
| 1507 | if (want) { |
| 1508 | retain |= CEPH_CAP_ANY; /* be greedy */ |
| 1509 | } else { |
| 1510 | retain |= CEPH_CAP_ANY_SHARED; |
| 1511 | /* |
| 1512 | * keep RD only if we didn't have the file open RW, |
| 1513 | * because then the mds would revoke it anyway to |
| 1514 | * journal max_size=0. |
| 1515 | */ |
| 1516 | if (ci->i_max_size == 0) |
| 1517 | retain |= CEPH_CAP_ANY_RD; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | 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] | 1522 | " issued %s revoking %s retain %s %s%s%s\n", inode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1523 | ceph_cap_string(file_wanted), |
| 1524 | ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), |
| 1525 | ceph_cap_string(ci->i_flushing_caps), |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1526 | ceph_cap_string(issued), ceph_cap_string(revoking), |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1527 | ceph_cap_string(retain), |
| 1528 | (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", |
| 1529 | (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "", |
| 1530 | (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : ""); |
| 1531 | |
| 1532 | /* |
| 1533 | * If we no longer need to hold onto old our caps, and we may |
| 1534 | * have cached pages, but don't want them, then try to invalidate. |
| 1535 | * If we fail, it's because pages are locked.... try again later. |
| 1536 | */ |
| 1537 | if ((!is_delayed || mdsc->stopping) && |
| 1538 | ci->i_wrbuffer_ref == 0 && /* no dirty pages... */ |
Sage Weil | 93afd44 | 2010-09-17 08:38:25 -0700 | [diff] [blame] | 1539 | inode->i_data.nrpages && /* have cached pages */ |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1540 | (file_wanted == 0 || /* no open files */ |
Sage Weil | 2962507 | 2010-05-27 10:40:43 -0700 | [diff] [blame] | 1541 | (revoking & (CEPH_CAP_FILE_CACHE| |
| 1542 | CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1543 | !tried_invalidate) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1544 | dout("check_caps trying to invalidate on %p\n", inode); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1545 | if (try_nonblocking_invalidate(inode) < 0) { |
Sage Weil | 2962507 | 2010-05-27 10:40:43 -0700 | [diff] [blame] | 1546 | if (revoking & (CEPH_CAP_FILE_CACHE| |
| 1547 | CEPH_CAP_FILE_LAZYIO)) { |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1548 | dout("check_caps queuing invalidate\n"); |
| 1549 | queue_invalidate = 1; |
| 1550 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
| 1551 | } else { |
| 1552 | dout("check_caps failed to invalidate pages\n"); |
| 1553 | /* we failed to invalidate pages. check these |
| 1554 | caps again later. */ |
| 1555 | force_requeue = 1; |
| 1556 | __cap_set_timeouts(mdsc, ci); |
| 1557 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1558 | } |
| 1559 | tried_invalidate = 1; |
| 1560 | goto retry_locked; |
| 1561 | } |
| 1562 | |
| 1563 | num = 0; |
| 1564 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 1565 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 1566 | num++; |
| 1567 | |
| 1568 | /* avoid looping forever */ |
| 1569 | if (mds >= cap->mds || |
| 1570 | ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) |
| 1571 | continue; |
| 1572 | |
| 1573 | /* NOTE: no side-effects allowed, until we take s_mutex */ |
| 1574 | |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1575 | cap_used = used; |
| 1576 | if (ci->i_auth_cap && cap != ci->i_auth_cap) |
| 1577 | cap_used &= ~ci->i_auth_cap->issued; |
| 1578 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1579 | revoking = cap->implemented & ~cap->issued; |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1580 | dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n", |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1581 | cap->mds, cap, ceph_cap_string(cap->issued), |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1582 | ceph_cap_string(cap_used), |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1583 | ceph_cap_string(cap->implemented), |
| 1584 | ceph_cap_string(revoking)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1585 | |
| 1586 | if (cap == ci->i_auth_cap && |
| 1587 | (cap->issued & CEPH_CAP_FILE_WR)) { |
| 1588 | /* request larger max_size from MDS? */ |
| 1589 | if (ci->i_wanted_max_size > ci->i_max_size && |
| 1590 | ci->i_wanted_max_size > ci->i_requested_max_size) { |
| 1591 | dout("requesting new max_size\n"); |
| 1592 | goto ack; |
| 1593 | } |
| 1594 | |
| 1595 | /* approaching file_max? */ |
| 1596 | if ((inode->i_size << 1) >= ci->i_max_size && |
| 1597 | (ci->i_reported_size << 1) < ci->i_max_size) { |
| 1598 | dout("i_size approaching max_size\n"); |
| 1599 | goto ack; |
| 1600 | } |
| 1601 | } |
| 1602 | /* flush anything dirty? */ |
| 1603 | if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) && |
| 1604 | ci->i_dirty_caps) { |
| 1605 | dout("flushing dirty caps\n"); |
| 1606 | goto ack; |
| 1607 | } |
| 1608 | |
| 1609 | /* completed revocation? going down and there are no caps? */ |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1610 | if (revoking && (revoking & cap_used) == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1611 | dout("completed revocation of %s\n", |
| 1612 | ceph_cap_string(cap->implemented & ~cap->issued)); |
| 1613 | goto ack; |
| 1614 | } |
| 1615 | |
| 1616 | /* want more caps from mds? */ |
| 1617 | if (want & ~(cap->mds_wanted | cap->issued)) |
| 1618 | goto ack; |
| 1619 | |
| 1620 | /* things we might delay */ |
| 1621 | if ((cap->issued & ~retain) == 0 && |
| 1622 | cap->mds_wanted == want) |
| 1623 | continue; /* nope, all good */ |
| 1624 | |
| 1625 | if (is_delayed) |
| 1626 | goto ack; |
| 1627 | |
| 1628 | /* delay? */ |
| 1629 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && |
| 1630 | time_before(jiffies, ci->i_hold_caps_max)) { |
| 1631 | dout(" delaying issued %s -> %s, wanted %s -> %s\n", |
| 1632 | ceph_cap_string(cap->issued), |
| 1633 | ceph_cap_string(cap->issued & retain), |
| 1634 | ceph_cap_string(cap->mds_wanted), |
| 1635 | ceph_cap_string(want)); |
| 1636 | delayed++; |
| 1637 | continue; |
| 1638 | } |
| 1639 | |
| 1640 | ack: |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 1641 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
| 1642 | dout(" skipping %p I_NOFLUSH set\n", inode); |
| 1643 | continue; |
| 1644 | } |
| 1645 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1646 | if (session && session != cap->session) { |
| 1647 | dout("oops, wrong session %p mutex\n", session); |
| 1648 | mutex_unlock(&session->s_mutex); |
| 1649 | session = NULL; |
| 1650 | } |
| 1651 | if (!session) { |
| 1652 | session = cap->session; |
| 1653 | if (mutex_trylock(&session->s_mutex) == 0) { |
| 1654 | dout("inverting session/ino locks on %p\n", |
| 1655 | session); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1656 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1657 | if (took_snap_rwsem) { |
| 1658 | up_read(&mdsc->snap_rwsem); |
| 1659 | took_snap_rwsem = 0; |
| 1660 | } |
| 1661 | mutex_lock(&session->s_mutex); |
| 1662 | goto retry; |
| 1663 | } |
| 1664 | } |
| 1665 | /* take snap_rwsem after session mutex */ |
| 1666 | if (!took_snap_rwsem) { |
| 1667 | if (down_read_trylock(&mdsc->snap_rwsem) == 0) { |
| 1668 | dout("inverting snap/in locks on %p\n", |
| 1669 | inode); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1670 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1671 | down_read(&mdsc->snap_rwsem); |
| 1672 | took_snap_rwsem = 1; |
| 1673 | goto retry; |
| 1674 | } |
| 1675 | took_snap_rwsem = 1; |
| 1676 | } |
| 1677 | |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1678 | if (cap == ci->i_auth_cap && ci->i_dirty_caps) |
| 1679 | flushing = __mark_caps_flushing(inode, session); |
Sage Weil | 24be0c4 | 2011-01-18 08:48:06 -0800 | [diff] [blame] | 1680 | else |
| 1681 | flushing = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1682 | |
| 1683 | mds = cap->mds; /* remember mds, so we don't repeat */ |
| 1684 | sent++; |
| 1685 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1686 | /* __send_cap drops i_ceph_lock */ |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1687 | delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, cap_used, |
| 1688 | want, retain, flushing, NULL); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1689 | goto retry; /* retake i_ceph_lock and restart our cap scan. */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | * Reschedule delayed caps release if we delayed anything, |
| 1694 | * otherwise cancel. |
| 1695 | */ |
| 1696 | if (delayed && is_delayed) |
| 1697 | force_requeue = 1; /* __send_cap delayed release; requeue */ |
| 1698 | if (!delayed && !is_delayed) |
| 1699 | __cap_delay_cancel(mdsc, ci); |
| 1700 | else if (!is_delayed || force_requeue) |
| 1701 | __cap_delay_requeue(mdsc, ci); |
| 1702 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1703 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1704 | |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1705 | if (queue_invalidate) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 1706 | ceph_queue_invalidate(inode); |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1707 | |
Sage Weil | cdc2ce0 | 2010-03-16 13:39:28 -0700 | [diff] [blame] | 1708 | if (session) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1709 | mutex_unlock(&session->s_mutex); |
| 1710 | if (took_snap_rwsem) |
| 1711 | up_read(&mdsc->snap_rwsem); |
| 1712 | } |
| 1713 | |
| 1714 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1715 | * Try to flush dirty caps back to the auth mds. |
| 1716 | */ |
| 1717 | static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session, |
| 1718 | unsigned *flush_tid) |
| 1719 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1720 | 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] | 1721 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1722 | int unlock_session = session ? 0 : 1; |
| 1723 | int flushing = 0; |
| 1724 | |
| 1725 | retry: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1726 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 1727 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
| 1728 | dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode); |
| 1729 | goto out; |
| 1730 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1731 | if (ci->i_dirty_caps && ci->i_auth_cap) { |
| 1732 | struct ceph_cap *cap = ci->i_auth_cap; |
| 1733 | int used = __ceph_caps_used(ci); |
| 1734 | int want = __ceph_caps_wanted(ci); |
| 1735 | int delayed; |
| 1736 | |
| 1737 | if (!session) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1738 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1739 | session = cap->session; |
| 1740 | mutex_lock(&session->s_mutex); |
| 1741 | goto retry; |
| 1742 | } |
| 1743 | BUG_ON(session != cap->session); |
| 1744 | if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) |
| 1745 | goto out; |
| 1746 | |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1747 | flushing = __mark_caps_flushing(inode, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1748 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1749 | /* __send_cap drops i_ceph_lock */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1750 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want, |
| 1751 | cap->issued | cap->implemented, flushing, |
| 1752 | flush_tid); |
| 1753 | if (!delayed) |
| 1754 | goto out_unlocked; |
| 1755 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1756 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1757 | __cap_delay_requeue(mdsc, ci); |
| 1758 | } |
| 1759 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1760 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1761 | out_unlocked: |
| 1762 | if (session && unlock_session) |
| 1763 | mutex_unlock(&session->s_mutex); |
| 1764 | return flushing; |
| 1765 | } |
| 1766 | |
| 1767 | /* |
| 1768 | * Return true if we've flushed caps through the given flush_tid. |
| 1769 | */ |
| 1770 | static int caps_are_flushed(struct inode *inode, unsigned tid) |
| 1771 | { |
| 1772 | struct ceph_inode_info *ci = ceph_inode(inode); |
Dan Carpenter | a5ee751 | 2010-05-07 10:27:14 +0200 | [diff] [blame] | 1773 | int i, ret = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1774 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1775 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1776 | for (i = 0; i < CEPH_CAP_BITS; i++) |
| 1777 | if ((ci->i_flushing_caps & (1 << i)) && |
| 1778 | ci->i_cap_flush_tid[i] <= tid) { |
| 1779 | /* still flushing this bit */ |
| 1780 | ret = 0; |
| 1781 | break; |
| 1782 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1783 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1784 | return ret; |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * Wait on any unsafe replies for the given inode. First wait on the |
| 1789 | * newest request, and make that the upper bound. Then, if there are |
| 1790 | * more requests, keep waiting on the oldest as long as it is still older |
| 1791 | * than the original request. |
| 1792 | */ |
| 1793 | static void sync_write_wait(struct inode *inode) |
| 1794 | { |
| 1795 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1796 | struct list_head *head = &ci->i_unsafe_writes; |
| 1797 | struct ceph_osd_request *req; |
| 1798 | u64 last_tid; |
| 1799 | |
| 1800 | spin_lock(&ci->i_unsafe_lock); |
| 1801 | if (list_empty(head)) |
| 1802 | goto out; |
| 1803 | |
| 1804 | /* set upper bound as _last_ entry in chain */ |
| 1805 | req = list_entry(head->prev, struct ceph_osd_request, |
| 1806 | r_unsafe_item); |
| 1807 | last_tid = req->r_tid; |
| 1808 | |
| 1809 | do { |
| 1810 | ceph_osdc_get_request(req); |
| 1811 | spin_unlock(&ci->i_unsafe_lock); |
| 1812 | dout("sync_write_wait on tid %llu (until %llu)\n", |
| 1813 | req->r_tid, last_tid); |
| 1814 | wait_for_completion(&req->r_safe_completion); |
| 1815 | spin_lock(&ci->i_unsafe_lock); |
| 1816 | ceph_osdc_put_request(req); |
| 1817 | |
| 1818 | /* |
| 1819 | * from here on look at first entry in chain, since we |
| 1820 | * only want to wait for anything older than last_tid |
| 1821 | */ |
| 1822 | if (list_empty(head)) |
| 1823 | break; |
| 1824 | req = list_entry(head->next, struct ceph_osd_request, |
| 1825 | r_unsafe_item); |
| 1826 | } while (req->r_tid < last_tid); |
| 1827 | out: |
| 1828 | spin_unlock(&ci->i_unsafe_lock); |
| 1829 | } |
| 1830 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1831 | 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] | 1832 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 1833 | struct inode *inode = file->f_mapping->host; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1834 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1835 | unsigned flush_tid; |
| 1836 | int ret; |
| 1837 | int dirty; |
| 1838 | |
| 1839 | dout("fsync %p%s\n", inode, datasync ? " datasync" : ""); |
| 1840 | sync_write_wait(inode); |
| 1841 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1842 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1843 | if (ret < 0) |
| 1844 | return ret; |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1845 | mutex_lock(&inode->i_mutex); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1846 | |
| 1847 | dirty = try_flush_caps(inode, NULL, &flush_tid); |
| 1848 | dout("fsync dirty caps are %s\n", ceph_cap_string(dirty)); |
| 1849 | |
| 1850 | /* |
| 1851 | * only wait on non-file metadata writeback (the mds |
| 1852 | * can recover size and mtime, so we don't need to |
| 1853 | * wait for that) |
| 1854 | */ |
| 1855 | if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { |
| 1856 | dout("fsync waiting for flush_tid %u\n", flush_tid); |
| 1857 | ret = wait_event_interruptible(ci->i_cap_wq, |
| 1858 | caps_are_flushed(inode, flush_tid)); |
| 1859 | } |
| 1860 | |
| 1861 | dout("fsync %p%s done\n", inode, datasync ? " datasync" : ""); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1862 | mutex_unlock(&inode->i_mutex); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1863 | return ret; |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * Flush any dirty caps back to the mds. If we aren't asked to wait, |
| 1868 | * queue inode for flush but don't do so immediately, because we can |
| 1869 | * get by with fewer MDS messages if we wait for data writeback to |
| 1870 | * complete first. |
| 1871 | */ |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 1872 | int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1873 | { |
| 1874 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1875 | unsigned flush_tid; |
| 1876 | int err = 0; |
| 1877 | int dirty; |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 1878 | int wait = wbc->sync_mode == WB_SYNC_ALL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1879 | |
| 1880 | dout("write_inode %p wait=%d\n", inode, wait); |
| 1881 | if (wait) { |
| 1882 | dirty = try_flush_caps(inode, NULL, &flush_tid); |
| 1883 | if (dirty) |
| 1884 | err = wait_event_interruptible(ci->i_cap_wq, |
| 1885 | caps_are_flushed(inode, flush_tid)); |
| 1886 | } else { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1887 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1888 | ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1889 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1890 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1891 | if (__ceph_caps_dirty(ci)) |
| 1892 | __cap_delay_requeue_front(mdsc, ci); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1893 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1894 | } |
| 1895 | return err; |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * After a recovering MDS goes active, we need to resend any caps |
| 1900 | * we were flushing. |
| 1901 | * |
| 1902 | * Caller holds session->s_mutex. |
| 1903 | */ |
| 1904 | static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc, |
| 1905 | struct ceph_mds_session *session) |
| 1906 | { |
| 1907 | struct ceph_cap_snap *capsnap; |
| 1908 | |
| 1909 | dout("kick_flushing_capsnaps mds%d\n", session->s_mds); |
| 1910 | list_for_each_entry(capsnap, &session->s_cap_snaps_flushing, |
| 1911 | flushing_item) { |
| 1912 | struct ceph_inode_info *ci = capsnap->ci; |
| 1913 | struct inode *inode = &ci->vfs_inode; |
| 1914 | struct ceph_cap *cap; |
| 1915 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1916 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1917 | cap = ci->i_auth_cap; |
| 1918 | if (cap && cap->session == session) { |
| 1919 | dout("kick_flushing_caps %p cap %p capsnap %p\n", inode, |
| 1920 | cap, capsnap); |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1921 | __ceph_flush_snaps(ci, &session, 1); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1922 | } else { |
| 1923 | pr_err("%p auth cap %p not mds%d ???\n", inode, |
| 1924 | cap, session->s_mds); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1925 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1926 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 1931 | struct ceph_mds_session *session) |
| 1932 | { |
| 1933 | struct ceph_inode_info *ci; |
| 1934 | |
| 1935 | kick_flushing_capsnaps(mdsc, session); |
| 1936 | |
| 1937 | dout("kick_flushing_caps mds%d\n", session->s_mds); |
| 1938 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { |
| 1939 | struct inode *inode = &ci->vfs_inode; |
| 1940 | struct ceph_cap *cap; |
| 1941 | int delayed = 0; |
| 1942 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1943 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1944 | cap = ci->i_auth_cap; |
| 1945 | if (cap && cap->session == session) { |
| 1946 | dout("kick_flushing_caps %p cap %p %s\n", inode, |
| 1947 | cap, ceph_cap_string(ci->i_flushing_caps)); |
| 1948 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, |
| 1949 | __ceph_caps_used(ci), |
| 1950 | __ceph_caps_wanted(ci), |
| 1951 | cap->issued | cap->implemented, |
| 1952 | ci->i_flushing_caps, NULL); |
| 1953 | if (delayed) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1954 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1955 | __cap_delay_requeue(mdsc, ci); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1956 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1957 | } |
| 1958 | } else { |
| 1959 | pr_err("%p auth cap %p not mds%d ???\n", inode, |
| 1960 | cap, session->s_mds); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1961 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1966 | static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc, |
| 1967 | struct ceph_mds_session *session, |
| 1968 | struct inode *inode) |
| 1969 | { |
| 1970 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1971 | struct ceph_cap *cap; |
| 1972 | int delayed = 0; |
| 1973 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1974 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1975 | cap = ci->i_auth_cap; |
| 1976 | dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode, |
| 1977 | ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq); |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 1978 | |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1979 | __ceph_flush_snaps(ci, &session, 1); |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 1980 | |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1981 | if (ci->i_flushing_caps) { |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 1982 | spin_lock(&mdsc->cap_dirty_lock); |
| 1983 | list_move_tail(&ci->i_flushing_item, |
| 1984 | &cap->session->s_cap_flushing); |
| 1985 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1986 | |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1987 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, |
| 1988 | __ceph_caps_used(ci), |
| 1989 | __ceph_caps_wanted(ci), |
| 1990 | cap->issued | cap->implemented, |
| 1991 | ci->i_flushing_caps, NULL); |
| 1992 | if (delayed) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1993 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1994 | __cap_delay_requeue(mdsc, ci); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1995 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1996 | } |
| 1997 | } else { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1998 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2002 | |
| 2003 | /* |
| 2004 | * Take references to capabilities we hold, so that we don't release |
| 2005 | * them to the MDS prematurely. |
| 2006 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2007 | * Protected by i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2008 | */ |
| 2009 | static void __take_cap_refs(struct ceph_inode_info *ci, int got) |
| 2010 | { |
| 2011 | if (got & CEPH_CAP_PIN) |
| 2012 | ci->i_pin_ref++; |
| 2013 | if (got & CEPH_CAP_FILE_RD) |
| 2014 | ci->i_rd_ref++; |
| 2015 | if (got & CEPH_CAP_FILE_CACHE) |
| 2016 | ci->i_rdcache_ref++; |
| 2017 | if (got & CEPH_CAP_FILE_WR) |
| 2018 | ci->i_wr_ref++; |
| 2019 | if (got & CEPH_CAP_FILE_BUFFER) { |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2020 | if (ci->i_wb_ref == 0) |
Sage Weil | 3772d26 | 2011-05-03 09:28:08 -0700 | [diff] [blame] | 2021 | ihold(&ci->vfs_inode); |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2022 | ci->i_wb_ref++; |
| 2023 | dout("__take_cap_refs %p wb %d -> %d (?)\n", |
| 2024 | &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | * Try to grab cap references. Specify those refs we @want, and the |
| 2030 | * minimal set we @need. Also include the larger offset we are writing |
| 2031 | * to (when applicable), and check against max_size here as well. |
| 2032 | * Note that caller is responsible for ensuring max_size increases are |
| 2033 | * requested from the MDS. |
| 2034 | */ |
| 2035 | static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, |
| 2036 | int *got, loff_t endoff, int *check_max, int *err) |
| 2037 | { |
| 2038 | struct inode *inode = &ci->vfs_inode; |
| 2039 | int ret = 0; |
| 2040 | int have, implemented; |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 2041 | int file_wanted; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2042 | |
| 2043 | dout("get_cap_refs %p need %s want %s\n", inode, |
| 2044 | ceph_cap_string(need), ceph_cap_string(want)); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2045 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2046 | |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 2047 | /* make sure file is actually open */ |
| 2048 | file_wanted = __ceph_caps_file_wanted(ci); |
| 2049 | if ((file_wanted & need) == 0) { |
| 2050 | dout("try_get_cap_refs need %s file_wanted %s, EBADF\n", |
| 2051 | ceph_cap_string(need), ceph_cap_string(file_wanted)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2052 | *err = -EBADF; |
| 2053 | ret = 1; |
| 2054 | goto out; |
| 2055 | } |
| 2056 | |
Yan, Zheng | 37505d5 | 2013-04-12 16:11:10 +0800 | [diff] [blame] | 2057 | /* finish pending truncate */ |
| 2058 | while (ci->i_truncate_pending) { |
| 2059 | spin_unlock(&ci->i_ceph_lock); |
| 2060 | __ceph_do_pending_vmtruncate(inode, !(need & CEPH_CAP_FILE_WR)); |
| 2061 | spin_lock(&ci->i_ceph_lock); |
| 2062 | } |
| 2063 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2064 | if (need & CEPH_CAP_FILE_WR) { |
| 2065 | if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { |
| 2066 | dout("get_cap_refs %p endoff %llu > maxsize %llu\n", |
| 2067 | inode, endoff, ci->i_max_size); |
| 2068 | if (endoff > ci->i_wanted_max_size) { |
| 2069 | *check_max = 1; |
| 2070 | ret = 1; |
| 2071 | } |
| 2072 | goto out; |
| 2073 | } |
| 2074 | /* |
| 2075 | * If a sync write is in progress, we must wait, so that we |
| 2076 | * can get a final snapshot value for size+mtime. |
| 2077 | */ |
| 2078 | if (__ceph_have_pending_cap_snap(ci)) { |
| 2079 | dout("get_cap_refs %p cap_snap_pending\n", inode); |
| 2080 | goto out; |
| 2081 | } |
| 2082 | } |
| 2083 | have = __ceph_caps_issued(ci, &implemented); |
| 2084 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2085 | if ((have & need) == need) { |
| 2086 | /* |
| 2087 | * Look at (implemented & ~have & not) so that we keep waiting |
| 2088 | * on transition from wanted -> needed caps. This is needed |
| 2089 | * for WRBUFFER|WR -> WR to avoid a new WR sync write from |
| 2090 | * going before a prior buffered writeback happens. |
| 2091 | */ |
| 2092 | int not = want & ~(have & need); |
| 2093 | int revoking = implemented & ~have; |
| 2094 | dout("get_cap_refs %p have %s but not %s (revoking %s)\n", |
| 2095 | inode, ceph_cap_string(have), ceph_cap_string(not), |
| 2096 | ceph_cap_string(revoking)); |
| 2097 | if ((revoking & not) == 0) { |
| 2098 | *got = need | (have & want); |
| 2099 | __take_cap_refs(ci, *got); |
| 2100 | ret = 1; |
| 2101 | } |
| 2102 | } else { |
| 2103 | dout("get_cap_refs %p have %s needed %s\n", inode, |
| 2104 | ceph_cap_string(have), ceph_cap_string(need)); |
| 2105 | } |
| 2106 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2107 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2108 | dout("get_cap_refs %p ret %d got %s\n", inode, |
| 2109 | ret, ceph_cap_string(*got)); |
| 2110 | return ret; |
| 2111 | } |
| 2112 | |
| 2113 | /* |
| 2114 | * Check the offset we are writing up to against our current |
| 2115 | * max_size. If necessary, tell the MDS we want to write to |
| 2116 | * a larger offset. |
| 2117 | */ |
| 2118 | static void check_max_size(struct inode *inode, loff_t endoff) |
| 2119 | { |
| 2120 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2121 | int check = 0; |
| 2122 | |
| 2123 | /* do we need to explicitly request a larger max_size? */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2124 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2125 | if ((endoff >= ci->i_max_size || |
| 2126 | endoff > (inode->i_size << 1)) && |
| 2127 | endoff > ci->i_wanted_max_size) { |
| 2128 | dout("write %p at large endoff %llu, req max_size\n", |
| 2129 | inode, endoff); |
| 2130 | ci->i_wanted_max_size = endoff; |
| 2131 | check = 1; |
| 2132 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2133 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2134 | if (check) |
| 2135 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); |
| 2136 | } |
| 2137 | |
| 2138 | /* |
| 2139 | * Wait for caps, and take cap references. If we can't get a WR cap |
| 2140 | * due to a small max_size, make sure we check_max_size (and possibly |
| 2141 | * ask the mds) so we don't get hung up indefinitely. |
| 2142 | */ |
| 2143 | int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got, |
| 2144 | loff_t endoff) |
| 2145 | { |
| 2146 | int check_max, ret, err; |
| 2147 | |
| 2148 | retry: |
| 2149 | if (endoff > 0) |
| 2150 | check_max_size(&ci->vfs_inode, endoff); |
| 2151 | check_max = 0; |
| 2152 | err = 0; |
| 2153 | ret = wait_event_interruptible(ci->i_cap_wq, |
| 2154 | try_get_cap_refs(ci, need, want, |
| 2155 | got, endoff, |
| 2156 | &check_max, &err)); |
| 2157 | if (err) |
| 2158 | ret = err; |
| 2159 | if (check_max) |
| 2160 | goto retry; |
| 2161 | return ret; |
| 2162 | } |
| 2163 | |
| 2164 | /* |
| 2165 | * Take cap refs. Caller must already know we hold at least one ref |
| 2166 | * on the caps in question or we don't know this is safe. |
| 2167 | */ |
| 2168 | void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) |
| 2169 | { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2170 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2171 | __take_cap_refs(ci, caps); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2172 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | /* |
| 2176 | * Release cap refs. |
| 2177 | * |
| 2178 | * If we released the last ref on any given cap, call ceph_check_caps |
| 2179 | * to release (or schedule a release). |
| 2180 | * |
| 2181 | * If we are releasing a WR cap (from a sync write), finalize any affected |
| 2182 | * cap_snap, and wake up any waiters. |
| 2183 | */ |
| 2184 | void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) |
| 2185 | { |
| 2186 | struct inode *inode = &ci->vfs_inode; |
| 2187 | int last = 0, put = 0, flushsnaps = 0, wake = 0; |
| 2188 | struct ceph_cap_snap *capsnap; |
| 2189 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2190 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2191 | if (had & CEPH_CAP_PIN) |
| 2192 | --ci->i_pin_ref; |
| 2193 | if (had & CEPH_CAP_FILE_RD) |
| 2194 | if (--ci->i_rd_ref == 0) |
| 2195 | last++; |
| 2196 | if (had & CEPH_CAP_FILE_CACHE) |
| 2197 | if (--ci->i_rdcache_ref == 0) |
| 2198 | last++; |
| 2199 | if (had & CEPH_CAP_FILE_BUFFER) { |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2200 | if (--ci->i_wb_ref == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2201 | last++; |
| 2202 | put++; |
| 2203 | } |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2204 | dout("put_cap_refs %p wb %d -> %d (?)\n", |
| 2205 | inode, ci->i_wb_ref+1, ci->i_wb_ref); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2206 | } |
| 2207 | if (had & CEPH_CAP_FILE_WR) |
| 2208 | if (--ci->i_wr_ref == 0) { |
| 2209 | last++; |
| 2210 | if (!list_empty(&ci->i_cap_snaps)) { |
| 2211 | capsnap = list_first_entry(&ci->i_cap_snaps, |
| 2212 | struct ceph_cap_snap, |
| 2213 | ci_item); |
| 2214 | if (capsnap->writing) { |
| 2215 | capsnap->writing = 0; |
| 2216 | flushsnaps = |
| 2217 | __ceph_finish_cap_snap(ci, |
| 2218 | capsnap); |
| 2219 | wake = 1; |
| 2220 | } |
| 2221 | } |
| 2222 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2223 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2224 | |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2225 | dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had), |
| 2226 | last ? " last" : "", put ? " put" : ""); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2227 | |
| 2228 | if (last && !flushsnaps) |
| 2229 | ceph_check_caps(ci, 0, NULL); |
| 2230 | else if (flushsnaps) |
| 2231 | ceph_flush_snaps(ci); |
| 2232 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2233 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2234 | if (put) |
| 2235 | iput(inode); |
| 2236 | } |
| 2237 | |
| 2238 | /* |
| 2239 | * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap |
| 2240 | * context. Adjust per-snap dirty page accounting as appropriate. |
| 2241 | * Once all dirty data for a cap_snap is flushed, flush snapped file |
| 2242 | * metadata back to the MDS. If we dropped the last ref, call |
| 2243 | * ceph_check_caps. |
| 2244 | */ |
| 2245 | void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, |
| 2246 | struct ceph_snap_context *snapc) |
| 2247 | { |
| 2248 | struct inode *inode = &ci->vfs_inode; |
| 2249 | int last = 0; |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2250 | int complete_capsnap = 0; |
| 2251 | int drop_capsnap = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2252 | int found = 0; |
| 2253 | struct ceph_cap_snap *capsnap = NULL; |
| 2254 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2255 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2256 | ci->i_wrbuffer_ref -= nr; |
| 2257 | last = !ci->i_wrbuffer_ref; |
| 2258 | |
| 2259 | if (ci->i_head_snapc == snapc) { |
| 2260 | ci->i_wrbuffer_ref_head -= nr; |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 2261 | if (ci->i_wrbuffer_ref_head == 0 && |
| 2262 | ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) { |
| 2263 | BUG_ON(!ci->i_head_snapc); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2264 | ceph_put_snap_context(ci->i_head_snapc); |
| 2265 | ci->i_head_snapc = NULL; |
| 2266 | } |
| 2267 | dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n", |
| 2268 | inode, |
| 2269 | ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr, |
| 2270 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, |
| 2271 | last ? " LAST" : ""); |
| 2272 | } else { |
| 2273 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 2274 | if (capsnap->context == snapc) { |
| 2275 | found = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2276 | break; |
| 2277 | } |
| 2278 | } |
| 2279 | BUG_ON(!found); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2280 | capsnap->dirty_pages -= nr; |
| 2281 | if (capsnap->dirty_pages == 0) { |
| 2282 | complete_capsnap = 1; |
| 2283 | if (capsnap->dirty == 0) |
| 2284 | /* cap writeback completed before we created |
| 2285 | * the cap_snap; no FLUSHSNAP is needed */ |
| 2286 | drop_capsnap = 1; |
| 2287 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2288 | dout("put_wrbuffer_cap_refs on %p cap_snap %p " |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2289 | " snap %lld %d/%d -> %d/%d %s%s%s\n", |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2290 | inode, capsnap, capsnap->context->seq, |
| 2291 | ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, |
| 2292 | ci->i_wrbuffer_ref, capsnap->dirty_pages, |
| 2293 | last ? " (wrbuffer last)" : "", |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2294 | complete_capsnap ? " (complete capsnap)" : "", |
| 2295 | drop_capsnap ? " (drop capsnap)" : ""); |
| 2296 | if (drop_capsnap) { |
| 2297 | ceph_put_snap_context(capsnap->context); |
| 2298 | list_del(&capsnap->ci_item); |
| 2299 | list_del(&capsnap->flushing_item); |
| 2300 | ceph_put_cap_snap(capsnap); |
| 2301 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2304 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2305 | |
| 2306 | if (last) { |
| 2307 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); |
| 2308 | iput(inode); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2309 | } else if (complete_capsnap) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2310 | ceph_flush_snaps(ci); |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2311 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2312 | } |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2313 | if (drop_capsnap) |
| 2314 | iput(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | /* |
| 2318 | * Handle a cap GRANT message from the MDS. (Note that a GRANT may |
| 2319 | * actually be a revocation if it specifies a smaller cap set.) |
| 2320 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2321 | * caller holds s_mutex and i_ceph_lock, we drop both. |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2322 | * |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2323 | * return value: |
| 2324 | * 0 - ok |
| 2325 | * 1 - check_caps on auth cap only (writeback) |
| 2326 | * 2 - check_caps (ack revoke) |
| 2327 | */ |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2328 | static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant, |
| 2329 | struct ceph_mds_session *session, |
| 2330 | struct ceph_cap *cap, |
| 2331 | struct ceph_buffer *xattr_buf) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2332 | __releases(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2333 | { |
| 2334 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2335 | int mds = session->s_mds; |
Sage Weil | 2f56f56 | 2010-10-27 20:59:49 -0700 | [diff] [blame] | 2336 | int seq = le32_to_cpu(grant->seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2337 | int newcaps = le32_to_cpu(grant->caps); |
| 2338 | int issued, implemented, used, wanted, dirty; |
| 2339 | u64 size = le64_to_cpu(grant->size); |
| 2340 | u64 max_size = le64_to_cpu(grant->max_size); |
| 2341 | struct timespec mtime, atime, ctime; |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2342 | int check_caps = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2343 | int wake = 0; |
| 2344 | int writeback = 0; |
| 2345 | int revoked_rdcache = 0; |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2346 | int queue_invalidate = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2347 | |
Sage Weil | 2f56f56 | 2010-10-27 20:59:49 -0700 | [diff] [blame] | 2348 | dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n", |
| 2349 | inode, cap, mds, seq, ceph_cap_string(newcaps)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2350 | dout(" size %llu max_size %llu, i_size %llu\n", size, max_size, |
| 2351 | inode->i_size); |
| 2352 | |
| 2353 | /* |
| 2354 | * If CACHE is being revoked, and we have no dirty buffers, |
| 2355 | * try to invalidate (once). (If there are dirty buffers, we |
| 2356 | * will invalidate _after_ writeback.) |
| 2357 | */ |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 2358 | if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && |
| 2359 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
Yehuda Sadeh | bcd2cbd | 2010-02-19 00:12:21 +0000 | [diff] [blame] | 2360 | !ci->i_wrbuffer_ref) { |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 2361 | if (try_nonblocking_invalidate(inode) == 0) { |
| 2362 | revoked_rdcache = 1; |
| 2363 | } else { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2364 | /* there were locked pages.. invalidate later |
| 2365 | in a separate thread. */ |
| 2366 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2367 | queue_invalidate = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2368 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
| 2369 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2370 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | /* side effects now are allowed */ |
| 2374 | |
| 2375 | issued = __ceph_caps_issued(ci, &implemented); |
| 2376 | issued |= implemented | __ceph_caps_dirty(ci); |
| 2377 | |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 2378 | cap->cap_gen = session->s_cap_gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2379 | |
| 2380 | __check_cap_issue(ci, cap, newcaps); |
| 2381 | |
| 2382 | if ((issued & CEPH_CAP_AUTH_EXCL) == 0) { |
| 2383 | inode->i_mode = le32_to_cpu(grant->mode); |
Eric W. Biederman | 05cb11c | 2013-01-31 02:56:19 -0800 | [diff] [blame] | 2384 | inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid)); |
| 2385 | 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] | 2386 | 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] | 2387 | from_kuid(&init_user_ns, inode->i_uid), |
| 2388 | from_kgid(&init_user_ns, inode->i_gid)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | if ((issued & CEPH_CAP_LINK_EXCL) == 0) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2392 | set_nlink(inode, le32_to_cpu(grant->nlink)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2393 | |
| 2394 | if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) { |
| 2395 | int len = le32_to_cpu(grant->xattr_len); |
| 2396 | u64 version = le64_to_cpu(grant->xattr_version); |
| 2397 | |
| 2398 | if (version > ci->i_xattrs.version) { |
| 2399 | dout(" got new xattrs v%llu on %p len %d\n", |
| 2400 | version, inode, len); |
| 2401 | if (ci->i_xattrs.blob) |
| 2402 | ceph_buffer_put(ci->i_xattrs.blob); |
| 2403 | ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); |
| 2404 | ci->i_xattrs.version = version; |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | /* size/ctime/mtime/atime? */ |
| 2409 | ceph_fill_file_size(inode, issued, |
| 2410 | le32_to_cpu(grant->truncate_seq), |
| 2411 | le64_to_cpu(grant->truncate_size), size); |
| 2412 | ceph_decode_timespec(&mtime, &grant->mtime); |
| 2413 | ceph_decode_timespec(&atime, &grant->atime); |
| 2414 | ceph_decode_timespec(&ctime, &grant->ctime); |
| 2415 | ceph_fill_file_time(inode, issued, |
| 2416 | le32_to_cpu(grant->time_warp_seq), &ctime, &mtime, |
| 2417 | &atime); |
| 2418 | |
| 2419 | /* max size increase? */ |
Yan, Zheng | 5e62ad3 | 2012-11-19 10:49:04 +0800 | [diff] [blame] | 2420 | if (ci->i_auth_cap == cap && max_size != ci->i_max_size) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2421 | dout("max_size %lld -> %llu\n", ci->i_max_size, max_size); |
| 2422 | ci->i_max_size = max_size; |
| 2423 | if (max_size >= ci->i_wanted_max_size) { |
| 2424 | ci->i_wanted_max_size = 0; /* reset */ |
| 2425 | ci->i_requested_max_size = 0; |
| 2426 | } |
| 2427 | wake = 1; |
| 2428 | } |
| 2429 | |
| 2430 | /* check cap bits */ |
| 2431 | wanted = __ceph_caps_wanted(ci); |
| 2432 | used = __ceph_caps_used(ci); |
| 2433 | dirty = __ceph_caps_dirty(ci); |
| 2434 | dout(" my wanted = %s, used = %s, dirty %s\n", |
| 2435 | ceph_cap_string(wanted), |
| 2436 | ceph_cap_string(used), |
| 2437 | ceph_cap_string(dirty)); |
| 2438 | if (wanted != le32_to_cpu(grant->wanted)) { |
| 2439 | dout("mds wanted %s -> %s\n", |
| 2440 | ceph_cap_string(le32_to_cpu(grant->wanted)), |
| 2441 | ceph_cap_string(wanted)); |
Yan, Zheng | 390306c | 2013-01-04 15:30:10 +0800 | [diff] [blame] | 2442 | /* imported cap may not have correct mds_wanted */ |
| 2443 | if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) |
| 2444 | check_caps = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | cap->seq = seq; |
| 2448 | |
| 2449 | /* file layout may have changed */ |
| 2450 | ci->i_layout = grant->layout; |
| 2451 | |
| 2452 | /* revocation, grant, or no-op? */ |
| 2453 | if (cap->issued & ~newcaps) { |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 2454 | int revoking = cap->issued & ~newcaps; |
| 2455 | |
| 2456 | dout("revocation: %s -> %s (revoking %s)\n", |
| 2457 | ceph_cap_string(cap->issued), |
| 2458 | ceph_cap_string(newcaps), |
| 2459 | ceph_cap_string(revoking)); |
Sage Weil | 0eb6cd4 | 2010-08-05 13:53:18 -0700 | [diff] [blame] | 2460 | if (revoking & used & CEPH_CAP_FILE_BUFFER) |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 2461 | writeback = 1; /* initiate writeback; will delay ack */ |
| 2462 | else if (revoking == CEPH_CAP_FILE_CACHE && |
| 2463 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
| 2464 | queue_invalidate) |
| 2465 | ; /* do nothing yet, invalidation will be queued */ |
| 2466 | else if (cap == ci->i_auth_cap) |
| 2467 | check_caps = 1; /* check auth cap only */ |
| 2468 | else |
| 2469 | check_caps = 2; /* check all caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2470 | cap->issued = newcaps; |
Sage Weil | 978097c | 2010-03-08 15:27:53 -0800 | [diff] [blame] | 2471 | cap->implemented |= newcaps; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2472 | } else if (cap->issued == newcaps) { |
| 2473 | dout("caps unchanged: %s -> %s\n", |
| 2474 | ceph_cap_string(cap->issued), ceph_cap_string(newcaps)); |
| 2475 | } else { |
| 2476 | dout("grant: %s -> %s\n", ceph_cap_string(cap->issued), |
| 2477 | ceph_cap_string(newcaps)); |
| 2478 | cap->issued = newcaps; |
| 2479 | cap->implemented |= newcaps; /* add bits only, to |
| 2480 | * avoid stepping on a |
| 2481 | * pending revocation */ |
| 2482 | wake = 1; |
| 2483 | } |
Sage Weil | 978097c | 2010-03-08 15:27:53 -0800 | [diff] [blame] | 2484 | BUG_ON(cap->issued & ~cap->implemented); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2485 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2486 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2487 | if (writeback) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2488 | /* |
| 2489 | * queue inode for writeback: we can't actually call |
| 2490 | * filemap_write_and_wait, etc. from message handler |
| 2491 | * context. |
| 2492 | */ |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2493 | ceph_queue_writeback(inode); |
| 2494 | if (queue_invalidate) |
| 2495 | ceph_queue_invalidate(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2496 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2497 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2498 | |
| 2499 | if (check_caps == 1) |
| 2500 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY, |
| 2501 | session); |
| 2502 | else if (check_caps == 2) |
| 2503 | ceph_check_caps(ci, CHECK_CAPS_NODELAY, session); |
| 2504 | else |
| 2505 | mutex_unlock(&session->s_mutex); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | /* |
| 2509 | * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the |
| 2510 | * MDS has been safely committed. |
| 2511 | */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2512 | static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2513 | struct ceph_mds_caps *m, |
| 2514 | struct ceph_mds_session *session, |
| 2515 | struct ceph_cap *cap) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2516 | __releases(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2517 | { |
| 2518 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2519 | 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] | 2520 | unsigned seq = le32_to_cpu(m->seq); |
| 2521 | int dirty = le32_to_cpu(m->dirty); |
| 2522 | int cleaned = 0; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2523 | int drop = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2524 | int i; |
| 2525 | |
| 2526 | for (i = 0; i < CEPH_CAP_BITS; i++) |
| 2527 | if ((dirty & (1 << i)) && |
| 2528 | flush_tid == ci->i_cap_flush_tid[i]) |
| 2529 | cleaned |= 1 << i; |
| 2530 | |
| 2531 | dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s," |
| 2532 | " flushing %s -> %s\n", |
| 2533 | inode, session->s_mds, seq, ceph_cap_string(dirty), |
| 2534 | ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps), |
| 2535 | ceph_cap_string(ci->i_flushing_caps & ~cleaned)); |
| 2536 | |
| 2537 | if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned)) |
| 2538 | goto out; |
| 2539 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2540 | ci->i_flushing_caps &= ~cleaned; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2541 | |
| 2542 | spin_lock(&mdsc->cap_dirty_lock); |
| 2543 | if (ci->i_flushing_caps == 0) { |
| 2544 | list_del_init(&ci->i_flushing_item); |
| 2545 | if (!list_empty(&session->s_cap_flushing)) |
| 2546 | dout(" mds%d still flushing cap on %p\n", |
| 2547 | session->s_mds, |
| 2548 | &list_entry(session->s_cap_flushing.next, |
| 2549 | struct ceph_inode_info, |
| 2550 | i_flushing_item)->vfs_inode); |
| 2551 | mdsc->num_cap_flushing--; |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2552 | wake_up_all(&mdsc->cap_flushing_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2553 | dout(" inode %p now !flushing\n", inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2554 | |
| 2555 | if (ci->i_dirty_caps == 0) { |
| 2556 | dout(" inode %p now clean\n", inode); |
| 2557 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
| 2558 | drop = 1; |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 2559 | if (ci->i_wrbuffer_ref_head == 0) { |
| 2560 | BUG_ON(!ci->i_head_snapc); |
| 2561 | ceph_put_snap_context(ci->i_head_snapc); |
| 2562 | ci->i_head_snapc = NULL; |
| 2563 | } |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 2564 | } else { |
| 2565 | BUG_ON(list_empty(&ci->i_dirty_item)); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2566 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2567 | } |
| 2568 | spin_unlock(&mdsc->cap_dirty_lock); |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2569 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2570 | |
| 2571 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2572 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2573 | if (drop) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2574 | iput(inode); |
| 2575 | } |
| 2576 | |
| 2577 | /* |
| 2578 | * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can |
| 2579 | * throw away our cap_snap. |
| 2580 | * |
| 2581 | * Caller hold s_mutex. |
| 2582 | */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2583 | static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2584 | struct ceph_mds_caps *m, |
| 2585 | struct ceph_mds_session *session) |
| 2586 | { |
| 2587 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2588 | u64 follows = le64_to_cpu(m->snap_follows); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2589 | struct ceph_cap_snap *capsnap; |
| 2590 | int drop = 0; |
| 2591 | |
| 2592 | dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n", |
| 2593 | inode, ci, session->s_mds, follows); |
| 2594 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2595 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2596 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 2597 | if (capsnap->follows == follows) { |
| 2598 | if (capsnap->flush_tid != flush_tid) { |
| 2599 | dout(" cap_snap %p follows %lld tid %lld !=" |
| 2600 | " %lld\n", capsnap, follows, |
| 2601 | flush_tid, capsnap->flush_tid); |
| 2602 | break; |
| 2603 | } |
| 2604 | WARN_ON(capsnap->dirty_pages || capsnap->writing); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2605 | dout(" removing %p cap_snap %p follows %lld\n", |
| 2606 | inode, capsnap, follows); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2607 | ceph_put_snap_context(capsnap->context); |
| 2608 | list_del(&capsnap->ci_item); |
| 2609 | list_del(&capsnap->flushing_item); |
| 2610 | ceph_put_cap_snap(capsnap); |
| 2611 | drop = 1; |
| 2612 | break; |
| 2613 | } else { |
| 2614 | dout(" skipping cap_snap %p follows %lld\n", |
| 2615 | capsnap, capsnap->follows); |
| 2616 | } |
| 2617 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2618 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2619 | if (drop) |
| 2620 | iput(inode); |
| 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * Handle TRUNC from MDS, indicating file truncation. |
| 2625 | * |
| 2626 | * caller hold s_mutex. |
| 2627 | */ |
| 2628 | static void handle_cap_trunc(struct inode *inode, |
| 2629 | struct ceph_mds_caps *trunc, |
| 2630 | struct ceph_mds_session *session) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2631 | __releases(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2632 | { |
| 2633 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2634 | int mds = session->s_mds; |
| 2635 | int seq = le32_to_cpu(trunc->seq); |
| 2636 | u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); |
| 2637 | u64 truncate_size = le64_to_cpu(trunc->truncate_size); |
| 2638 | u64 size = le64_to_cpu(trunc->size); |
| 2639 | int implemented = 0; |
| 2640 | int dirty = __ceph_caps_dirty(ci); |
| 2641 | int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); |
| 2642 | int queue_trunc = 0; |
| 2643 | |
| 2644 | issued |= implemented | dirty; |
| 2645 | |
| 2646 | dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n", |
| 2647 | inode, mds, seq, truncate_size, truncate_seq); |
| 2648 | queue_trunc = ceph_fill_file_size(inode, issued, |
| 2649 | truncate_seq, truncate_size, size); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2650 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2651 | |
| 2652 | if (queue_trunc) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2653 | ceph_queue_vmtruncate(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | /* |
| 2657 | * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a |
| 2658 | * different one. If we are the most recent migration we've seen (as |
| 2659 | * indicated by mseq), make note of the migrating cap bits for the |
| 2660 | * duration (until we see the corresponding IMPORT). |
| 2661 | * |
| 2662 | * caller holds s_mutex |
| 2663 | */ |
| 2664 | static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2665 | struct ceph_mds_session *session, |
| 2666 | int *open_target_sessions) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2667 | { |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 2668 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2669 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2670 | int mds = session->s_mds; |
| 2671 | unsigned mseq = le32_to_cpu(ex->migrate_seq); |
| 2672 | struct ceph_cap *cap = NULL, *t; |
| 2673 | struct rb_node *p; |
| 2674 | int remember = 1; |
| 2675 | |
| 2676 | dout("handle_cap_export inode %p ci %p mds%d mseq %d\n", |
| 2677 | inode, ci, mds, mseq); |
| 2678 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2679 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2680 | |
| 2681 | /* make sure we haven't seen a higher mseq */ |
| 2682 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 2683 | t = rb_entry(p, struct ceph_cap, ci_node); |
| 2684 | if (ceph_seq_cmp(t->mseq, mseq) > 0) { |
| 2685 | dout(" higher mseq on cap from mds%d\n", |
| 2686 | t->session->s_mds); |
| 2687 | remember = 0; |
| 2688 | } |
| 2689 | if (t->session->s_mds == mds) |
| 2690 | cap = t; |
| 2691 | } |
| 2692 | |
| 2693 | if (cap) { |
| 2694 | if (remember) { |
| 2695 | /* make note */ |
| 2696 | ci->i_cap_exporting_mds = mds; |
| 2697 | ci->i_cap_exporting_mseq = mseq; |
| 2698 | ci->i_cap_exporting_issued = cap->issued; |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2699 | |
| 2700 | /* |
| 2701 | * make sure we have open sessions with all possible |
| 2702 | * export targets, so that we get the matching IMPORT |
| 2703 | */ |
| 2704 | *open_target_sessions = 1; |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 2705 | |
| 2706 | /* |
| 2707 | * we can't flush dirty caps that we've seen the |
| 2708 | * EXPORT but no IMPORT for |
| 2709 | */ |
| 2710 | spin_lock(&mdsc->cap_dirty_lock); |
| 2711 | if (!list_empty(&ci->i_dirty_item)) { |
| 2712 | dout(" moving %p to cap_dirty_migrating\n", |
| 2713 | inode); |
| 2714 | list_move(&ci->i_dirty_item, |
| 2715 | &mdsc->cap_dirty_migrating); |
| 2716 | } |
| 2717 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2718 | } |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 2719 | __ceph_remove_cap(cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2720 | } |
Sage Weil | 4ea0043 | 2010-03-16 10:36:40 -0700 | [diff] [blame] | 2721 | /* else, we already released it */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2722 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2723 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | /* |
| 2727 | * Handle cap IMPORT. If there are temp bits from an older EXPORT, |
| 2728 | * clean them up. |
| 2729 | * |
| 2730 | * caller holds s_mutex. |
| 2731 | */ |
| 2732 | static void handle_cap_import(struct ceph_mds_client *mdsc, |
| 2733 | struct inode *inode, struct ceph_mds_caps *im, |
| 2734 | struct ceph_mds_session *session, |
| 2735 | void *snaptrace, int snaptrace_len) |
| 2736 | { |
| 2737 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2738 | int mds = session->s_mds; |
| 2739 | unsigned issued = le32_to_cpu(im->caps); |
| 2740 | unsigned wanted = le32_to_cpu(im->wanted); |
| 2741 | unsigned seq = le32_to_cpu(im->seq); |
| 2742 | unsigned mseq = le32_to_cpu(im->migrate_seq); |
| 2743 | u64 realmino = le64_to_cpu(im->realm); |
| 2744 | u64 cap_id = le64_to_cpu(im->cap_id); |
| 2745 | |
| 2746 | if (ci->i_cap_exporting_mds >= 0 && |
| 2747 | ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) { |
| 2748 | dout("handle_cap_import inode %p ci %p mds%d mseq %d" |
| 2749 | " - cleared exporting from mds%d\n", |
| 2750 | inode, ci, mds, mseq, |
| 2751 | ci->i_cap_exporting_mds); |
| 2752 | ci->i_cap_exporting_issued = 0; |
| 2753 | ci->i_cap_exporting_mseq = 0; |
| 2754 | ci->i_cap_exporting_mds = -1; |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 2755 | |
| 2756 | spin_lock(&mdsc->cap_dirty_lock); |
| 2757 | if (!list_empty(&ci->i_dirty_item)) { |
| 2758 | dout(" moving %p back to cap_dirty\n", inode); |
| 2759 | list_move(&ci->i_dirty_item, &mdsc->cap_dirty); |
| 2760 | } |
| 2761 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2762 | } else { |
| 2763 | dout("handle_cap_import inode %p ci %p mds%d mseq %d\n", |
| 2764 | inode, ci, mds, mseq); |
| 2765 | } |
| 2766 | |
| 2767 | down_write(&mdsc->snap_rwsem); |
| 2768 | ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len, |
| 2769 | false); |
| 2770 | downgrade_write(&mdsc->snap_rwsem); |
| 2771 | ceph_add_cap(inode, session, cap_id, -1, |
| 2772 | issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH, |
| 2773 | NULL /* no caps context */); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2774 | kick_flushing_inode_caps(mdsc, session, inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2775 | up_read(&mdsc->snap_rwsem); |
Sage Weil | feb4cc9 | 2010-11-07 09:39:00 -0800 | [diff] [blame] | 2776 | |
| 2777 | /* make sure we re-request max_size, if necessary */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2778 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 0e5e177 | 2012-11-19 10:49:09 +0800 | [diff] [blame] | 2779 | ci->i_wanted_max_size = 0; /* reset */ |
Sage Weil | feb4cc9 | 2010-11-07 09:39:00 -0800 | [diff] [blame] | 2780 | ci->i_requested_max_size = 0; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2781 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | /* |
| 2785 | * Handle a caps message from the MDS. |
| 2786 | * |
| 2787 | * Identify the appropriate session, inode, and call the right handler |
| 2788 | * based on the cap op. |
| 2789 | */ |
| 2790 | void ceph_handle_caps(struct ceph_mds_session *session, |
| 2791 | struct ceph_msg *msg) |
| 2792 | { |
| 2793 | struct ceph_mds_client *mdsc = session->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2794 | struct super_block *sb = mdsc->fsc->sb; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2795 | struct inode *inode; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2796 | struct ceph_inode_info *ci; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2797 | struct ceph_cap *cap; |
| 2798 | struct ceph_mds_caps *h; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2799 | int mds = session->s_mds; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2800 | int op; |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 2801 | u32 seq, mseq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2802 | struct ceph_vino vino; |
| 2803 | u64 cap_id; |
| 2804 | u64 size, max_size; |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2805 | u64 tid; |
Sage Weil | 70edb55 | 2010-03-01 13:20:50 -0800 | [diff] [blame] | 2806 | void *snaptrace; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 2807 | size_t snaptrace_len; |
| 2808 | void *flock; |
| 2809 | u32 flock_len; |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2810 | int open_target_sessions = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2811 | |
| 2812 | dout("handle_caps from mds%d\n", mds); |
| 2813 | |
| 2814 | /* decode */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2815 | tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2816 | if (msg->front.iov_len < sizeof(*h)) |
| 2817 | goto bad; |
| 2818 | h = msg->front.iov_base; |
| 2819 | op = le32_to_cpu(h->op); |
| 2820 | vino.ino = le64_to_cpu(h->ino); |
| 2821 | vino.snap = CEPH_NOSNAP; |
| 2822 | cap_id = le64_to_cpu(h->cap_id); |
| 2823 | seq = le32_to_cpu(h->seq); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 2824 | mseq = le32_to_cpu(h->migrate_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2825 | size = le64_to_cpu(h->size); |
| 2826 | max_size = le64_to_cpu(h->max_size); |
| 2827 | |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 2828 | snaptrace = h + 1; |
| 2829 | snaptrace_len = le32_to_cpu(h->snap_trace_len); |
| 2830 | |
| 2831 | if (le16_to_cpu(msg->hdr.version) >= 2) { |
| 2832 | void *p, *end; |
| 2833 | |
| 2834 | p = snaptrace + snaptrace_len; |
| 2835 | end = msg->front.iov_base + msg->front.iov_len; |
| 2836 | ceph_decode_32_safe(&p, end, flock_len, bad); |
| 2837 | flock = p; |
| 2838 | } else { |
| 2839 | flock = NULL; |
| 2840 | flock_len = 0; |
| 2841 | } |
| 2842 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2843 | mutex_lock(&session->s_mutex); |
| 2844 | session->s_seq++; |
| 2845 | dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq, |
| 2846 | (unsigned)seq); |
| 2847 | |
Yan, Zheng | 66f5869 | 2013-01-04 14:45:18 +0800 | [diff] [blame] | 2848 | if (op == CEPH_CAP_OP_IMPORT) |
| 2849 | ceph_add_cap_releases(mdsc, session); |
| 2850 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2851 | /* lookup ino */ |
| 2852 | inode = ceph_find_inode(sb, vino); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2853 | ci = ceph_inode(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2854 | dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino, |
| 2855 | vino.snap, inode); |
| 2856 | if (!inode) { |
| 2857 | dout(" i don't have ino %llx\n", vino.ino); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 2858 | |
| 2859 | if (op == CEPH_CAP_OP_IMPORT) |
| 2860 | __queue_cap_release(session, vino.ino, cap_id, |
| 2861 | mseq, seq); |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 2862 | goto flush_cap_releases; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | /* these will work even if we don't have a cap yet */ |
| 2866 | switch (op) { |
| 2867 | case CEPH_CAP_OP_FLUSHSNAP_ACK: |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2868 | handle_cap_flushsnap_ack(inode, tid, h, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2869 | goto done; |
| 2870 | |
| 2871 | case CEPH_CAP_OP_EXPORT: |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2872 | handle_cap_export(inode, h, session, &open_target_sessions); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2873 | goto done; |
| 2874 | |
| 2875 | case CEPH_CAP_OP_IMPORT: |
| 2876 | handle_cap_import(mdsc, inode, h, session, |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 2877 | snaptrace, snaptrace_len); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | /* the rest require a cap */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2881 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2882 | cap = __get_cap_for_mds(ceph_inode(inode), mds); |
| 2883 | if (!cap) { |
Sage Weil | 9dbd412 | 2010-06-10 13:21:20 -0700 | [diff] [blame] | 2884 | dout(" no cap on %p ino %llx.%llx from mds%d\n", |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2885 | inode, ceph_ino(inode), ceph_snap(inode), mds); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2886 | spin_unlock(&ci->i_ceph_lock); |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 2887 | goto flush_cap_releases; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2890 | /* note that each of these drops i_ceph_lock for us */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2891 | switch (op) { |
| 2892 | case CEPH_CAP_OP_REVOKE: |
| 2893 | case CEPH_CAP_OP_GRANT: |
Yan, Zheng | 0e5e177 | 2012-11-19 10:49:09 +0800 | [diff] [blame] | 2894 | case CEPH_CAP_OP_IMPORT: |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2895 | handle_cap_grant(inode, h, session, cap, msg->middle); |
| 2896 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2897 | |
| 2898 | case CEPH_CAP_OP_FLUSH_ACK: |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2899 | handle_cap_flush_ack(inode, tid, h, session, cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2900 | break; |
| 2901 | |
| 2902 | case CEPH_CAP_OP_TRUNC: |
| 2903 | handle_cap_trunc(inode, h, session); |
| 2904 | break; |
| 2905 | |
| 2906 | default: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2907 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2908 | pr_err("ceph_handle_caps: unknown cap op %d %s\n", op, |
| 2909 | ceph_cap_op_name(op)); |
| 2910 | } |
| 2911 | |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 2912 | goto done; |
| 2913 | |
| 2914 | flush_cap_releases: |
| 2915 | /* |
| 2916 | * send any full release message to try to move things |
| 2917 | * along for the mds (who clearly thinks we still have this |
| 2918 | * cap). |
| 2919 | */ |
| 2920 | ceph_add_cap_releases(mdsc, session); |
| 2921 | ceph_send_cap_releases(mdsc, session); |
| 2922 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2923 | done: |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2924 | mutex_unlock(&session->s_mutex); |
| 2925 | done_unlocked: |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2926 | if (inode) |
| 2927 | iput(inode); |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2928 | if (open_target_sessions) |
| 2929 | ceph_mdsc_open_export_target_sessions(mdsc, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2930 | return; |
| 2931 | |
| 2932 | bad: |
| 2933 | pr_err("ceph_handle_caps: corrupt message\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2934 | ceph_msg_dump(msg); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2935 | return; |
| 2936 | } |
| 2937 | |
| 2938 | /* |
| 2939 | * Delayed work handler to process end of delayed cap release LRU list. |
| 2940 | */ |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2941 | void ceph_check_delayed_caps(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2942 | { |
| 2943 | struct ceph_inode_info *ci; |
| 2944 | int flags = CHECK_CAPS_NODELAY; |
| 2945 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2946 | dout("check_delayed_caps\n"); |
| 2947 | while (1) { |
| 2948 | spin_lock(&mdsc->cap_delay_lock); |
| 2949 | if (list_empty(&mdsc->cap_delay_list)) |
| 2950 | break; |
| 2951 | ci = list_first_entry(&mdsc->cap_delay_list, |
| 2952 | struct ceph_inode_info, |
| 2953 | i_cap_delay_list); |
| 2954 | if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && |
| 2955 | time_before(jiffies, ci->i_hold_caps_max)) |
| 2956 | break; |
| 2957 | list_del_init(&ci->i_cap_delay_list); |
| 2958 | spin_unlock(&mdsc->cap_delay_lock); |
| 2959 | dout("check_delayed_caps on %p\n", &ci->vfs_inode); |
| 2960 | ceph_check_caps(ci, flags, NULL); |
| 2961 | } |
| 2962 | spin_unlock(&mdsc->cap_delay_lock); |
| 2963 | } |
| 2964 | |
| 2965 | /* |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2966 | * Flush all dirty caps to the mds |
| 2967 | */ |
| 2968 | void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) |
| 2969 | { |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 2970 | struct ceph_inode_info *ci; |
| 2971 | struct inode *inode; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2972 | |
| 2973 | dout("flush_dirty_caps\n"); |
| 2974 | spin_lock(&mdsc->cap_dirty_lock); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 2975 | while (!list_empty(&mdsc->cap_dirty)) { |
| 2976 | ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info, |
| 2977 | i_dirty_item); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 2978 | inode = &ci->vfs_inode; |
| 2979 | ihold(inode); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 2980 | dout("flush_dirty_caps %p\n", inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2981 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 2982 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL); |
| 2983 | iput(inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2984 | spin_lock(&mdsc->cap_dirty_lock); |
| 2985 | } |
| 2986 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 2987 | dout("flush_dirty_caps done\n"); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2991 | * Drop open file reference. If we were the last open file, |
| 2992 | * we may need to release capabilities to the MDS (or schedule |
| 2993 | * their delayed release). |
| 2994 | */ |
| 2995 | void ceph_put_fmode(struct ceph_inode_info *ci, int fmode) |
| 2996 | { |
| 2997 | struct inode *inode = &ci->vfs_inode; |
| 2998 | int last = 0; |
| 2999 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3000 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3001 | dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode, |
| 3002 | ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1); |
| 3003 | BUG_ON(ci->i_nr_by_mode[fmode] == 0); |
| 3004 | if (--ci->i_nr_by_mode[fmode] == 0) |
| 3005 | last++; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3006 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3007 | |
| 3008 | if (last && ci->i_vino.snap == CEPH_NOSNAP) |
| 3009 | ceph_check_caps(ci, 0, NULL); |
| 3010 | } |
| 3011 | |
| 3012 | /* |
| 3013 | * Helpers for embedding cap and dentry lease releases into mds |
| 3014 | * requests. |
| 3015 | * |
| 3016 | * @force is used by dentry_release (below) to force inclusion of a |
| 3017 | * record for the directory inode, even when there aren't any caps to |
| 3018 | * drop. |
| 3019 | */ |
| 3020 | int ceph_encode_inode_release(void **p, struct inode *inode, |
| 3021 | int mds, int drop, int unless, int force) |
| 3022 | { |
| 3023 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 3024 | struct ceph_cap *cap; |
| 3025 | struct ceph_mds_request_release *rel = *p; |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 3026 | int used, dirty; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3027 | int ret = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3028 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3029 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 3030 | used = __ceph_caps_used(ci); |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 3031 | dirty = __ceph_caps_dirty(ci); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 3032 | |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 3033 | dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n", |
| 3034 | inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop), |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 3035 | ceph_cap_string(unless)); |
| 3036 | |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 3037 | /* only drop unused, clean caps */ |
| 3038 | drop &= ~(used | dirty); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 3039 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3040 | cap = __get_cap_for_mds(ci, mds); |
| 3041 | if (cap && __cap_is_valid(cap)) { |
| 3042 | if (force || |
| 3043 | ((cap->issued & drop) && |
| 3044 | (cap->issued & unless) == 0)) { |
| 3045 | if ((cap->issued & drop) && |
| 3046 | (cap->issued & unless) == 0) { |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 3047 | int wanted = __ceph_caps_wanted(ci); |
| 3048 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0) |
| 3049 | wanted |= cap->mds_wanted; |
| 3050 | dout("encode_inode_release %p cap %p " |
| 3051 | "%s -> %s, wanted %s -> %s\n", inode, cap, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3052 | ceph_cap_string(cap->issued), |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 3053 | ceph_cap_string(cap->issued & ~drop), |
| 3054 | ceph_cap_string(cap->mds_wanted), |
| 3055 | ceph_cap_string(wanted)); |
| 3056 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3057 | cap->issued &= ~drop; |
| 3058 | cap->implemented &= ~drop; |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 3059 | cap->mds_wanted = wanted; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3060 | } else { |
| 3061 | dout("encode_inode_release %p cap %p %s" |
| 3062 | " (force)\n", inode, cap, |
| 3063 | ceph_cap_string(cap->issued)); |
| 3064 | } |
| 3065 | |
| 3066 | rel->ino = cpu_to_le64(ceph_ino(inode)); |
| 3067 | rel->cap_id = cpu_to_le64(cap->cap_id); |
| 3068 | rel->seq = cpu_to_le32(cap->seq); |
| 3069 | rel->issue_seq = cpu_to_le32(cap->issue_seq), |
| 3070 | rel->mseq = cpu_to_le32(cap->mseq); |
| 3071 | rel->caps = cpu_to_le32(cap->issued); |
| 3072 | rel->wanted = cpu_to_le32(cap->mds_wanted); |
| 3073 | rel->dname_len = 0; |
| 3074 | rel->dname_seq = 0; |
| 3075 | *p += sizeof(*rel); |
| 3076 | ret = 1; |
| 3077 | } else { |
| 3078 | dout("encode_inode_release %p cap %p %s\n", |
| 3079 | inode, cap, ceph_cap_string(cap->issued)); |
| 3080 | } |
| 3081 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3082 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3083 | return ret; |
| 3084 | } |
| 3085 | |
| 3086 | int ceph_encode_dentry_release(void **p, struct dentry *dentry, |
| 3087 | int mds, int drop, int unless) |
| 3088 | { |
| 3089 | struct inode *dir = dentry->d_parent->d_inode; |
| 3090 | struct ceph_mds_request_release *rel = *p; |
| 3091 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 3092 | int force = 0; |
| 3093 | int ret; |
| 3094 | |
| 3095 | /* |
| 3096 | * 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] | 3097 | * 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] | 3098 | * doesn't have to be perfect; the mds will revoke anything we don't |
| 3099 | * release. |
| 3100 | */ |
| 3101 | spin_lock(&dentry->d_lock); |
| 3102 | if (di->lease_session && di->lease_session->s_mds == mds) |
| 3103 | force = 1; |
| 3104 | spin_unlock(&dentry->d_lock); |
| 3105 | |
| 3106 | ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); |
| 3107 | |
| 3108 | spin_lock(&dentry->d_lock); |
| 3109 | if (ret && di->lease_session && di->lease_session->s_mds == mds) { |
| 3110 | dout("encode_dentry_release %p mds%d seq %d\n", |
| 3111 | dentry, mds, (int)di->lease_seq); |
| 3112 | rel->dname_len = cpu_to_le32(dentry->d_name.len); |
| 3113 | memcpy(*p, dentry->d_name.name, dentry->d_name.len); |
| 3114 | *p += dentry->d_name.len; |
| 3115 | rel->dname_seq = cpu_to_le32(di->lease_seq); |
Sage Weil | 1dadcce | 2010-07-23 13:54:21 -0700 | [diff] [blame] | 3116 | __ceph_mdsc_drop_dentry_lease(dentry); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3117 | } |
| 3118 | spin_unlock(&dentry->d_lock); |
| 3119 | return ret; |
| 3120 | } |