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