1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h> 3355da1ebSSage Weil 4355da1ebSSage Weil #include <linux/module.h> 5355da1ebSSage Weil #include <linux/fs.h> 6355da1ebSSage Weil #include <linux/slab.h> 7355da1ebSSage Weil #include <linux/string.h> 8355da1ebSSage Weil #include <linux/uaccess.h> 9355da1ebSSage Weil #include <linux/kernel.h> 10355da1ebSSage Weil #include <linux/writeback.h> 11355da1ebSSage Weil #include <linux/vmalloc.h> 122cdeb1e4SAndreas Gruenbacher #include <linux/xattr.h> 134db658eaSLinus Torvalds #include <linux/posix_acl.h> 143e7fbe9cSYan, Zheng #include <linux/random.h> 15a407846eSYan, Zheng #include <linux/sort.h> 16a35ead31SJeff Layton #include <linux/iversion.h> 17355da1ebSSage Weil 18355da1ebSSage Weil #include "super.h" 193d14c5d2SYehuda Sadeh #include "mds_client.h" 2099ccbd22SMilosz Tanski #include "cache.h" 213d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h> 22355da1ebSSage Weil 23355da1ebSSage Weil /* 24355da1ebSSage Weil * Ceph inode operations 25355da1ebSSage Weil * 26355da1ebSSage Weil * Implement basic inode helpers (get, alloc) and inode ops (getattr, 27355da1ebSSage Weil * setattr, etc.), xattr helpers, and helpers for assimilating 28355da1ebSSage Weil * metadata returned by the MDS into our cache. 29355da1ebSSage Weil * 30355da1ebSSage Weil * Also define helpers for doing asynchronous writeback, invalidation, 31355da1ebSSage Weil * and truncation for the benefit of those who can't afford to block 32355da1ebSSage Weil * (typically because they are in the message handler path). 33355da1ebSSage Weil */ 34355da1ebSSage Weil 35355da1ebSSage Weil static const struct inode_operations ceph_symlink_iops; 36355da1ebSSage Weil 371cf89a8dSYan, Zheng static void ceph_inode_work(struct work_struct *work); 38355da1ebSSage Weil 39355da1ebSSage Weil /* 40355da1ebSSage Weil * find or create an inode, given the ceph ino number 41355da1ebSSage Weil */ 42ad1fee96SYehuda Sadeh static int ceph_set_ino_cb(struct inode *inode, void *data) 43ad1fee96SYehuda Sadeh { 44ebce3eb2SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode); 451dd8d470SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 46ebce3eb2SJeff Layton 47ebce3eb2SJeff Layton ci->i_vino = *(struct ceph_vino *)data; 48ebce3eb2SJeff Layton inode->i_ino = ceph_vino_to_ino_t(ci->i_vino); 49a35ead31SJeff Layton inode_set_iversion_raw(inode, 0); 501dd8d470SXiubo Li percpu_counter_inc(&mdsc->metric.total_inodes); 511dd8d470SXiubo Li 52ad1fee96SYehuda Sadeh return 0; 53ad1fee96SYehuda Sadeh } 54ad1fee96SYehuda Sadeh 55355da1ebSSage Weil struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino) 56355da1ebSSage Weil { 57355da1ebSSage Weil struct inode *inode; 58355da1ebSSage Weil 59d4f6b31dSJeff Layton if (ceph_vino_is_reserved(vino)) 60d4f6b31dSJeff Layton return ERR_PTR(-EREMOTEIO); 61d4f6b31dSJeff Layton 62ebce3eb2SJeff Layton inode = iget5_locked(sb, (unsigned long)vino.ino, ceph_ino_compare, 63ebce3eb2SJeff Layton ceph_set_ino_cb, &vino); 64d37b1d99SMarkus Elfring if (!inode) 65355da1ebSSage Weil return ERR_PTR(-ENOMEM); 66355da1ebSSage Weil 67ebce3eb2SJeff Layton dout("get_inode on %llu=%llx.%llx got %p new %d\n", ceph_present_inode(inode), 68ebce3eb2SJeff Layton ceph_vinop(inode), inode, !!(inode->i_state & I_NEW)); 69355da1ebSSage Weil return inode; 70355da1ebSSage Weil } 71355da1ebSSage Weil 72355da1ebSSage Weil /* 73355da1ebSSage Weil * get/constuct snapdir inode for a given directory 74355da1ebSSage Weil */ 75355da1ebSSage Weil struct inode *ceph_get_snapdir(struct inode *parent) 76355da1ebSSage Weil { 77355da1ebSSage Weil struct ceph_vino vino = { 78355da1ebSSage Weil .ino = ceph_ino(parent), 79355da1ebSSage Weil .snap = CEPH_SNAPDIR, 80355da1ebSSage Weil }; 81355da1ebSSage Weil struct inode *inode = ceph_get_inode(parent->i_sb, vino); 82b377ff13SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 83355da1ebSSage Weil 84355da1ebSSage Weil if (IS_ERR(inode)) 857e34bc52SJulia Lawall return inode; 863e10a15fSJeff Layton 873e10a15fSJeff Layton if (!S_ISDIR(parent->i_mode)) { 883e10a15fSJeff Layton pr_warn_once("bad snapdir parent type (mode=0%o)\n", 893e10a15fSJeff Layton parent->i_mode); 90322794d3SXiubo Li goto err; 913e10a15fSJeff Layton } 923e10a15fSJeff Layton 933e10a15fSJeff Layton if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) { 943e10a15fSJeff Layton pr_warn_once("bad snapdir inode type (mode=0%o)\n", 953e10a15fSJeff Layton inode->i_mode); 96322794d3SXiubo Li goto err; 973e10a15fSJeff Layton } 983e10a15fSJeff Layton 99355da1ebSSage Weil inode->i_mode = parent->i_mode; 100355da1ebSSage Weil inode->i_uid = parent->i_uid; 101355da1ebSSage Weil inode->i_gid = parent->i_gid; 102ef915725SLuis Henriques inode->i_mtime = parent->i_mtime; 103ef915725SLuis Henriques inode->i_ctime = parent->i_ctime; 104ef915725SLuis Henriques inode->i_atime = parent->i_atime; 105b377ff13SSage Weil ci->i_rbytes = 0; 106ef915725SLuis Henriques ci->i_btime = ceph_inode(parent)->i_btime; 107893e456bSJeff Layton 108d3c51ae1SJeff Layton if (inode->i_state & I_NEW) { 109d3c51ae1SJeff Layton inode->i_op = &ceph_snapdir_iops; 110d3c51ae1SJeff Layton inode->i_fop = &ceph_snapdir_fops; 111d3c51ae1SJeff Layton ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */ 112893e456bSJeff Layton unlock_new_inode(inode); 113d3c51ae1SJeff Layton } 114893e456bSJeff Layton 115355da1ebSSage Weil return inode; 116322794d3SXiubo Li err: 117322794d3SXiubo Li if ((inode->i_state & I_NEW)) 118322794d3SXiubo Li discard_new_inode(inode); 119322794d3SXiubo Li else 120322794d3SXiubo Li iput(inode); 121322794d3SXiubo Li return ERR_PTR(-ENOTDIR); 122355da1ebSSage Weil } 123355da1ebSSage Weil 124355da1ebSSage Weil const struct inode_operations ceph_file_iops = { 125355da1ebSSage Weil .permission = ceph_permission, 126355da1ebSSage Weil .setattr = ceph_setattr, 127355da1ebSSage Weil .getattr = ceph_getattr, 128355da1ebSSage Weil .listxattr = ceph_listxattr, 129cac2f8b8SChristian Brauner .get_inode_acl = ceph_get_acl, 13072466d0bSSage Weil .set_acl = ceph_set_acl, 131355da1ebSSage Weil }; 132355da1ebSSage Weil 133355da1ebSSage Weil 134355da1ebSSage Weil /* 135355da1ebSSage Weil * We use a 'frag tree' to keep track of the MDS's directory fragments 136355da1ebSSage Weil * for a given inode (usually there is just a single fragment). We 137355da1ebSSage Weil * need to know when a child frag is delegated to a new MDS, or when 138355da1ebSSage Weil * it is flagged as replicated, so we can direct our requests 139355da1ebSSage Weil * accordingly. 140355da1ebSSage Weil */ 141355da1ebSSage Weil 142355da1ebSSage Weil /* 143355da1ebSSage Weil * find/create a frag in the tree 144355da1ebSSage Weil */ 145355da1ebSSage Weil static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci, 146355da1ebSSage Weil u32 f) 147355da1ebSSage Weil { 148355da1ebSSage Weil struct rb_node **p; 149355da1ebSSage Weil struct rb_node *parent = NULL; 150355da1ebSSage Weil struct ceph_inode_frag *frag; 151355da1ebSSage Weil int c; 152355da1ebSSage Weil 153355da1ebSSage Weil p = &ci->i_fragtree.rb_node; 154355da1ebSSage Weil while (*p) { 155355da1ebSSage Weil parent = *p; 156355da1ebSSage Weil frag = rb_entry(parent, struct ceph_inode_frag, node); 157355da1ebSSage Weil c = ceph_frag_compare(f, frag->frag); 158355da1ebSSage Weil if (c < 0) 159355da1ebSSage Weil p = &(*p)->rb_left; 160355da1ebSSage Weil else if (c > 0) 161355da1ebSSage Weil p = &(*p)->rb_right; 162355da1ebSSage Weil else 163355da1ebSSage Weil return frag; 164355da1ebSSage Weil } 165355da1ebSSage Weil 166355da1ebSSage Weil frag = kmalloc(sizeof(*frag), GFP_NOFS); 16751308806SMarkus Elfring if (!frag) 168355da1ebSSage Weil return ERR_PTR(-ENOMEM); 16951308806SMarkus Elfring 170355da1ebSSage Weil frag->frag = f; 171355da1ebSSage Weil frag->split_by = 0; 172355da1ebSSage Weil frag->mds = -1; 173355da1ebSSage Weil frag->ndist = 0; 174355da1ebSSage Weil 175355da1ebSSage Weil rb_link_node(&frag->node, parent, p); 176355da1ebSSage Weil rb_insert_color(&frag->node, &ci->i_fragtree); 177355da1ebSSage Weil 178355da1ebSSage Weil dout("get_or_create_frag added %llx.%llx frag %x\n", 179874c8ca1SDavid Howells ceph_vinop(&ci->netfs.inode), f); 180355da1ebSSage Weil return frag; 181355da1ebSSage Weil } 182355da1ebSSage Weil 183355da1ebSSage Weil /* 184355da1ebSSage Weil * find a specific frag @f 185355da1ebSSage Weil */ 186355da1ebSSage Weil struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f) 187355da1ebSSage Weil { 188355da1ebSSage Weil struct rb_node *n = ci->i_fragtree.rb_node; 189355da1ebSSage Weil 190355da1ebSSage Weil while (n) { 191355da1ebSSage Weil struct ceph_inode_frag *frag = 192355da1ebSSage Weil rb_entry(n, struct ceph_inode_frag, node); 193355da1ebSSage Weil int c = ceph_frag_compare(f, frag->frag); 194355da1ebSSage Weil if (c < 0) 195355da1ebSSage Weil n = n->rb_left; 196355da1ebSSage Weil else if (c > 0) 197355da1ebSSage Weil n = n->rb_right; 198355da1ebSSage Weil else 199355da1ebSSage Weil return frag; 200355da1ebSSage Weil } 201355da1ebSSage Weil return NULL; 202355da1ebSSage Weil } 203355da1ebSSage Weil 204355da1ebSSage Weil /* 205355da1ebSSage Weil * Choose frag containing the given value @v. If @pfrag is 206355da1ebSSage Weil * specified, copy the frag delegation info to the caller if 207355da1ebSSage Weil * it is present. 208355da1ebSSage Weil */ 2093e7fbe9cSYan, Zheng static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v, 2103e7fbe9cSYan, Zheng struct ceph_inode_frag *pfrag, int *found) 211355da1ebSSage Weil { 212355da1ebSSage Weil u32 t = ceph_frag_make(0, 0); 213355da1ebSSage Weil struct ceph_inode_frag *frag; 214355da1ebSSage Weil unsigned nway, i; 215355da1ebSSage Weil u32 n; 216355da1ebSSage Weil 217355da1ebSSage Weil if (found) 218355da1ebSSage Weil *found = 0; 219355da1ebSSage Weil 220355da1ebSSage Weil while (1) { 221355da1ebSSage Weil WARN_ON(!ceph_frag_contains_value(t, v)); 222355da1ebSSage Weil frag = __ceph_find_frag(ci, t); 223355da1ebSSage Weil if (!frag) 224355da1ebSSage Weil break; /* t is a leaf */ 225355da1ebSSage Weil if (frag->split_by == 0) { 226355da1ebSSage Weil if (pfrag) 227355da1ebSSage Weil memcpy(pfrag, frag, sizeof(*pfrag)); 228355da1ebSSage Weil if (found) 229355da1ebSSage Weil *found = 1; 230355da1ebSSage Weil break; 231355da1ebSSage Weil } 232355da1ebSSage Weil 233355da1ebSSage Weil /* choose child */ 234355da1ebSSage Weil nway = 1 << frag->split_by; 235355da1ebSSage Weil dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t, 236355da1ebSSage Weil frag->split_by, nway); 237355da1ebSSage Weil for (i = 0; i < nway; i++) { 238355da1ebSSage Weil n = ceph_frag_make_child(t, frag->split_by, i); 239355da1ebSSage Weil if (ceph_frag_contains_value(n, v)) { 240355da1ebSSage Weil t = n; 241355da1ebSSage Weil break; 242355da1ebSSage Weil } 243355da1ebSSage Weil } 244355da1ebSSage Weil BUG_ON(i == nway); 245355da1ebSSage Weil } 246355da1ebSSage Weil dout("choose_frag(%x) = %x\n", v, t); 247355da1ebSSage Weil 248355da1ebSSage Weil return t; 249355da1ebSSage Weil } 250355da1ebSSage Weil 2513e7fbe9cSYan, Zheng u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v, 2523e7fbe9cSYan, Zheng struct ceph_inode_frag *pfrag, int *found) 2533e7fbe9cSYan, Zheng { 2543e7fbe9cSYan, Zheng u32 ret; 2553e7fbe9cSYan, Zheng mutex_lock(&ci->i_fragtree_mutex); 2563e7fbe9cSYan, Zheng ret = __ceph_choose_frag(ci, v, pfrag, found); 2573e7fbe9cSYan, Zheng mutex_unlock(&ci->i_fragtree_mutex); 2583e7fbe9cSYan, Zheng return ret; 2593e7fbe9cSYan, Zheng } 2603e7fbe9cSYan, Zheng 261355da1ebSSage Weil /* 262355da1ebSSage Weil * Process dirfrag (delegation) info from the mds. Include leaf 263355da1ebSSage Weil * fragment in tree ONLY if ndist > 0. Otherwise, only 264355da1ebSSage Weil * branches/splits are included in i_fragtree) 265355da1ebSSage Weil */ 266355da1ebSSage Weil static int ceph_fill_dirfrag(struct inode *inode, 267355da1ebSSage Weil struct ceph_mds_reply_dirfrag *dirinfo) 268355da1ebSSage Weil { 269355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 270355da1ebSSage Weil struct ceph_inode_frag *frag; 271355da1ebSSage Weil u32 id = le32_to_cpu(dirinfo->frag); 272355da1ebSSage Weil int mds = le32_to_cpu(dirinfo->auth); 273355da1ebSSage Weil int ndist = le32_to_cpu(dirinfo->ndist); 2748d08503cSYan, Zheng int diri_auth = -1; 275355da1ebSSage Weil int i; 276355da1ebSSage Weil int err = 0; 277355da1ebSSage Weil 2788d08503cSYan, Zheng spin_lock(&ci->i_ceph_lock); 2798d08503cSYan, Zheng if (ci->i_auth_cap) 2808d08503cSYan, Zheng diri_auth = ci->i_auth_cap->mds; 2818d08503cSYan, Zheng spin_unlock(&ci->i_ceph_lock); 2828d08503cSYan, Zheng 28342172119SYan, Zheng if (mds == -1) /* CDIR_AUTH_PARENT */ 28442172119SYan, Zheng mds = diri_auth; 28542172119SYan, Zheng 286355da1ebSSage Weil mutex_lock(&ci->i_fragtree_mutex); 2878d08503cSYan, Zheng if (ndist == 0 && mds == diri_auth) { 288355da1ebSSage Weil /* no delegation info needed. */ 289355da1ebSSage Weil frag = __ceph_find_frag(ci, id); 290355da1ebSSage Weil if (!frag) 291355da1ebSSage Weil goto out; 292355da1ebSSage Weil if (frag->split_by == 0) { 293355da1ebSSage Weil /* tree leaf, remove */ 294355da1ebSSage Weil dout("fill_dirfrag removed %llx.%llx frag %x" 295355da1ebSSage Weil " (no ref)\n", ceph_vinop(inode), id); 296355da1ebSSage Weil rb_erase(&frag->node, &ci->i_fragtree); 297355da1ebSSage Weil kfree(frag); 298355da1ebSSage Weil } else { 299355da1ebSSage Weil /* tree branch, keep and clear */ 300355da1ebSSage Weil dout("fill_dirfrag cleared %llx.%llx frag %x" 301355da1ebSSage Weil " referral\n", ceph_vinop(inode), id); 302355da1ebSSage Weil frag->mds = -1; 303355da1ebSSage Weil frag->ndist = 0; 304355da1ebSSage Weil } 305355da1ebSSage Weil goto out; 306355da1ebSSage Weil } 307355da1ebSSage Weil 308355da1ebSSage Weil 309355da1ebSSage Weil /* find/add this frag to store mds delegation info */ 310355da1ebSSage Weil frag = __get_or_create_frag(ci, id); 311355da1ebSSage Weil if (IS_ERR(frag)) { 312355da1ebSSage Weil /* this is not the end of the world; we can continue 313355da1ebSSage Weil with bad/inaccurate delegation info */ 314355da1ebSSage Weil pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n", 315355da1ebSSage Weil ceph_vinop(inode), le32_to_cpu(dirinfo->frag)); 316355da1ebSSage Weil err = -ENOMEM; 317355da1ebSSage Weil goto out; 318355da1ebSSage Weil } 319355da1ebSSage Weil 320355da1ebSSage Weil frag->mds = mds; 321355da1ebSSage Weil frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP); 322355da1ebSSage Weil for (i = 0; i < frag->ndist; i++) 323355da1ebSSage Weil frag->dist[i] = le32_to_cpu(dirinfo->dist[i]); 324355da1ebSSage Weil dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n", 325355da1ebSSage Weil ceph_vinop(inode), frag->frag, frag->ndist); 326355da1ebSSage Weil 327355da1ebSSage Weil out: 328355da1ebSSage Weil mutex_unlock(&ci->i_fragtree_mutex); 329355da1ebSSage Weil return err; 330355da1ebSSage Weil } 331355da1ebSSage Weil 332a407846eSYan, Zheng static int frag_tree_split_cmp(const void *l, const void *r) 333a407846eSYan, Zheng { 334a407846eSYan, Zheng struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l; 335a407846eSYan, Zheng struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r; 336fe2ed425SJeff Layton return ceph_frag_compare(le32_to_cpu(ls->frag), 337fe2ed425SJeff Layton le32_to_cpu(rs->frag)); 338a407846eSYan, Zheng } 339a407846eSYan, Zheng 340a4b7431fSYan, Zheng static bool is_frag_child(u32 f, struct ceph_inode_frag *frag) 341a4b7431fSYan, Zheng { 342a4b7431fSYan, Zheng if (!frag) 343a4b7431fSYan, Zheng return f == ceph_frag_make(0, 0); 344a4b7431fSYan, Zheng if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by) 345a4b7431fSYan, Zheng return false; 346a4b7431fSYan, Zheng return ceph_frag_contains_value(frag->frag, ceph_frag_value(f)); 347a4b7431fSYan, Zheng } 348a4b7431fSYan, Zheng 3493e7fbe9cSYan, Zheng static int ceph_fill_fragtree(struct inode *inode, 3503e7fbe9cSYan, Zheng struct ceph_frag_tree_head *fragtree, 3513e7fbe9cSYan, Zheng struct ceph_mds_reply_dirfrag *dirinfo) 3523e7fbe9cSYan, Zheng { 3533e7fbe9cSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 354a4b7431fSYan, Zheng struct ceph_inode_frag *frag, *prev_frag = NULL; 3553e7fbe9cSYan, Zheng struct rb_node *rb_node; 3561b1bc16dSYan, Zheng unsigned i, split_by, nsplits; 3571b1bc16dSYan, Zheng u32 id; 3583e7fbe9cSYan, Zheng bool update = false; 3593e7fbe9cSYan, Zheng 3603e7fbe9cSYan, Zheng mutex_lock(&ci->i_fragtree_mutex); 3613e7fbe9cSYan, Zheng nsplits = le32_to_cpu(fragtree->nsplits); 3621b1bc16dSYan, Zheng if (nsplits != ci->i_fragtree_nsplits) { 3631b1bc16dSYan, Zheng update = true; 3641b1bc16dSYan, Zheng } else if (nsplits) { 3658032bf12SJason A. Donenfeld i = get_random_u32_below(nsplits); 3663e7fbe9cSYan, Zheng id = le32_to_cpu(fragtree->splits[i].frag); 3673e7fbe9cSYan, Zheng if (!__ceph_find_frag(ci, id)) 3683e7fbe9cSYan, Zheng update = true; 3693e7fbe9cSYan, Zheng } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) { 3703e7fbe9cSYan, Zheng rb_node = rb_first(&ci->i_fragtree); 3713e7fbe9cSYan, Zheng frag = rb_entry(rb_node, struct ceph_inode_frag, node); 3723e7fbe9cSYan, Zheng if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node)) 3733e7fbe9cSYan, Zheng update = true; 3743e7fbe9cSYan, Zheng } 3753e7fbe9cSYan, Zheng if (!update && dirinfo) { 3763e7fbe9cSYan, Zheng id = le32_to_cpu(dirinfo->frag); 3773e7fbe9cSYan, Zheng if (id != __ceph_choose_frag(ci, id, NULL, NULL)) 3783e7fbe9cSYan, Zheng update = true; 3793e7fbe9cSYan, Zheng } 3803e7fbe9cSYan, Zheng if (!update) 3813e7fbe9cSYan, Zheng goto out_unlock; 3823e7fbe9cSYan, Zheng 383a407846eSYan, Zheng if (nsplits > 1) { 384a407846eSYan, Zheng sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]), 385a407846eSYan, Zheng frag_tree_split_cmp, NULL); 386a407846eSYan, Zheng } 387a407846eSYan, Zheng 3883e7fbe9cSYan, Zheng dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode)); 3893e7fbe9cSYan, Zheng rb_node = rb_first(&ci->i_fragtree); 3903e7fbe9cSYan, Zheng for (i = 0; i < nsplits; i++) { 3913e7fbe9cSYan, Zheng id = le32_to_cpu(fragtree->splits[i].frag); 3921b1bc16dSYan, Zheng split_by = le32_to_cpu(fragtree->splits[i].by); 3931b1bc16dSYan, Zheng if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) { 3941b1bc16dSYan, Zheng pr_err("fill_fragtree %llx.%llx invalid split %d/%u, " 3951b1bc16dSYan, Zheng "frag %x split by %d\n", ceph_vinop(inode), 3961b1bc16dSYan, Zheng i, nsplits, id, split_by); 3971b1bc16dSYan, Zheng continue; 3981b1bc16dSYan, Zheng } 3993e7fbe9cSYan, Zheng frag = NULL; 4003e7fbe9cSYan, Zheng while (rb_node) { 4013e7fbe9cSYan, Zheng frag = rb_entry(rb_node, struct ceph_inode_frag, node); 4023e7fbe9cSYan, Zheng if (ceph_frag_compare(frag->frag, id) >= 0) { 4033e7fbe9cSYan, Zheng if (frag->frag != id) 4043e7fbe9cSYan, Zheng frag = NULL; 4053e7fbe9cSYan, Zheng else 4063e7fbe9cSYan, Zheng rb_node = rb_next(rb_node); 4073e7fbe9cSYan, Zheng break; 4083e7fbe9cSYan, Zheng } 4093e7fbe9cSYan, Zheng rb_node = rb_next(rb_node); 410a4b7431fSYan, Zheng /* delete stale split/leaf node */ 411a4b7431fSYan, Zheng if (frag->split_by > 0 || 412a4b7431fSYan, Zheng !is_frag_child(frag->frag, prev_frag)) { 4133e7fbe9cSYan, Zheng rb_erase(&frag->node, &ci->i_fragtree); 4141b1bc16dSYan, Zheng if (frag->split_by > 0) 4151b1bc16dSYan, Zheng ci->i_fragtree_nsplits--; 4163e7fbe9cSYan, Zheng kfree(frag); 417a4b7431fSYan, Zheng } 4183e7fbe9cSYan, Zheng frag = NULL; 4193e7fbe9cSYan, Zheng } 4203e7fbe9cSYan, Zheng if (!frag) { 4213e7fbe9cSYan, Zheng frag = __get_or_create_frag(ci, id); 4223e7fbe9cSYan, Zheng if (IS_ERR(frag)) 4233e7fbe9cSYan, Zheng continue; 4243e7fbe9cSYan, Zheng } 4251b1bc16dSYan, Zheng if (frag->split_by == 0) 4261b1bc16dSYan, Zheng ci->i_fragtree_nsplits++; 4271b1bc16dSYan, Zheng frag->split_by = split_by; 4283e7fbe9cSYan, Zheng dout(" frag %x split by %d\n", frag->frag, frag->split_by); 429a4b7431fSYan, Zheng prev_frag = frag; 4303e7fbe9cSYan, Zheng } 4313e7fbe9cSYan, Zheng while (rb_node) { 4323e7fbe9cSYan, Zheng frag = rb_entry(rb_node, struct ceph_inode_frag, node); 4333e7fbe9cSYan, Zheng rb_node = rb_next(rb_node); 434a4b7431fSYan, Zheng /* delete stale split/leaf node */ 435a4b7431fSYan, Zheng if (frag->split_by > 0 || 436a4b7431fSYan, Zheng !is_frag_child(frag->frag, prev_frag)) { 4373e7fbe9cSYan, Zheng rb_erase(&frag->node, &ci->i_fragtree); 4381b1bc16dSYan, Zheng if (frag->split_by > 0) 4391b1bc16dSYan, Zheng ci->i_fragtree_nsplits--; 4403e7fbe9cSYan, Zheng kfree(frag); 4413e7fbe9cSYan, Zheng } 442a4b7431fSYan, Zheng } 4433e7fbe9cSYan, Zheng out_unlock: 4443e7fbe9cSYan, Zheng mutex_unlock(&ci->i_fragtree_mutex); 4453e7fbe9cSYan, Zheng return 0; 4463e7fbe9cSYan, Zheng } 447355da1ebSSage Weil 448355da1ebSSage Weil /* 449355da1ebSSage Weil * initialize a newly allocated inode. 450355da1ebSSage Weil */ 451355da1ebSSage Weil struct inode *ceph_alloc_inode(struct super_block *sb) 452355da1ebSSage Weil { 453355da1ebSSage Weil struct ceph_inode_info *ci; 454355da1ebSSage Weil int i; 455355da1ebSSage Weil 456fd60b288SMuchun Song ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS); 457355da1ebSSage Weil if (!ci) 458355da1ebSSage Weil return NULL; 459355da1ebSSage Weil 460874c8ca1SDavid Howells dout("alloc_inode %p\n", &ci->netfs.inode); 461355da1ebSSage Weil 462bc899ee1SDavid Howells /* Set parameters for the netfs library */ 463e81fb419SLinus Torvalds netfs_inode_init(&ci->netfs, &ceph_netfs_ops); 464bc899ee1SDavid Howells 465be655596SSage Weil spin_lock_init(&ci->i_ceph_lock); 466be655596SSage Weil 467355da1ebSSage Weil ci->i_version = 0; 46831c542a1SYan, Zheng ci->i_inline_version = 0; 469355da1ebSSage Weil ci->i_time_warp_seq = 0; 470355da1ebSSage Weil ci->i_ceph_flags = 0; 471fdd4e158SYan, Zheng atomic64_set(&ci->i_ordered_count, 1); 472fdd4e158SYan, Zheng atomic64_set(&ci->i_release_count, 1); 473fdd4e158SYan, Zheng atomic64_set(&ci->i_complete_seq[0], 0); 474fdd4e158SYan, Zheng atomic64_set(&ci->i_complete_seq[1], 0); 475355da1ebSSage Weil ci->i_symlink = NULL; 476355da1ebSSage Weil 477fb18a575SLuis Henriques ci->i_max_bytes = 0; 478fb18a575SLuis Henriques ci->i_max_files = 0; 479fb18a575SLuis Henriques 4806c0f3af7SSage Weil memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout)); 481785892feSJeff Layton memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout)); 48230c156d9SYan, Zheng RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL); 4836c0f3af7SSage Weil 484355da1ebSSage Weil ci->i_fragtree = RB_ROOT; 485355da1ebSSage Weil mutex_init(&ci->i_fragtree_mutex); 486355da1ebSSage Weil 487355da1ebSSage Weil ci->i_xattrs.blob = NULL; 488355da1ebSSage Weil ci->i_xattrs.prealloc_blob = NULL; 489355da1ebSSage Weil ci->i_xattrs.dirty = false; 490355da1ebSSage Weil ci->i_xattrs.index = RB_ROOT; 491355da1ebSSage Weil ci->i_xattrs.count = 0; 492355da1ebSSage Weil ci->i_xattrs.names_size = 0; 493355da1ebSSage Weil ci->i_xattrs.vals_size = 0; 494355da1ebSSage Weil ci->i_xattrs.version = 0; 495355da1ebSSage Weil ci->i_xattrs.index_version = 0; 496355da1ebSSage Weil 497355da1ebSSage Weil ci->i_caps = RB_ROOT; 498355da1ebSSage Weil ci->i_auth_cap = NULL; 499355da1ebSSage Weil ci->i_dirty_caps = 0; 500355da1ebSSage Weil ci->i_flushing_caps = 0; 501355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_dirty_item); 502355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_flushing_item); 503f66fd9f0SYan, Zheng ci->i_prealloc_cap_flush = NULL; 504e4500b5eSYan, Zheng INIT_LIST_HEAD(&ci->i_cap_flush_list); 505355da1ebSSage Weil init_waitqueue_head(&ci->i_cap_wq); 506355da1ebSSage Weil ci->i_hold_caps_max = 0; 507355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_cap_delay_list); 508355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_cap_snaps); 509355da1ebSSage Weil ci->i_head_snapc = NULL; 510355da1ebSSage Weil ci->i_snap_caps = 0; 511355da1ebSSage Weil 512719a2514SYan, Zheng ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ; 513774a6a11SYan, Zheng for (i = 0; i < CEPH_FILE_MODE_BITS; i++) 514355da1ebSSage Weil ci->i_nr_by_mode[i] = 0; 515355da1ebSSage Weil 516b0d7c223SYan, Zheng mutex_init(&ci->i_truncate_mutex); 517355da1ebSSage Weil ci->i_truncate_seq = 0; 518355da1ebSSage Weil ci->i_truncate_size = 0; 519355da1ebSSage Weil ci->i_truncate_pending = 0; 520355da1ebSSage Weil 521355da1ebSSage Weil ci->i_max_size = 0; 522355da1ebSSage Weil ci->i_reported_size = 0; 523355da1ebSSage Weil ci->i_wanted_max_size = 0; 524355da1ebSSage Weil ci->i_requested_max_size = 0; 525355da1ebSSage Weil 526355da1ebSSage Weil ci->i_pin_ref = 0; 527355da1ebSSage Weil ci->i_rd_ref = 0; 528355da1ebSSage Weil ci->i_rdcache_ref = 0; 529355da1ebSSage Weil ci->i_wr_ref = 0; 530d3d0720dSHenry C Chang ci->i_wb_ref = 0; 531f85122afSJeff Layton ci->i_fx_ref = 0; 532355da1ebSSage Weil ci->i_wrbuffer_ref = 0; 533355da1ebSSage Weil ci->i_wrbuffer_ref_head = 0; 53489aa5930SYan, Zheng atomic_set(&ci->i_filelock_ref, 0); 5351e9c2eb6SYan, Zheng atomic_set(&ci->i_shared_gen, 1); 536355da1ebSSage Weil ci->i_rdcache_gen = 0; 537355da1ebSSage Weil ci->i_rdcache_revoking = 0; 538355da1ebSSage Weil 539355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_unsafe_dirops); 54068cd5b4bSYan, Zheng INIT_LIST_HEAD(&ci->i_unsafe_iops); 541355da1ebSSage Weil spin_lock_init(&ci->i_unsafe_lock); 542355da1ebSSage Weil 543355da1ebSSage Weil ci->i_snap_realm = NULL; 544355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_snap_realm_item); 545355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_snap_flush_item); 546355da1ebSSage Weil 5471cf89a8dSYan, Zheng INIT_WORK(&ci->i_work, ceph_inode_work); 5481cf89a8dSYan, Zheng ci->i_work_mask = 0; 549245ce991SJeff Layton memset(&ci->i_btime, '\0', sizeof(ci->i_btime)); 550874c8ca1SDavid Howells return &ci->netfs.inode; 551355da1ebSSage Weil } 552355da1ebSSage Weil 553cfa6d412SAl Viro void ceph_free_inode(struct inode *inode) 554fa0d7e3dSNick Piggin { 555fa0d7e3dSNick Piggin struct ceph_inode_info *ci = ceph_inode(inode); 556fa0d7e3dSNick Piggin 557daf5cc27SAl Viro kfree(ci->i_symlink); 558fa0d7e3dSNick Piggin kmem_cache_free(ceph_inode_cachep, ci); 559fa0d7e3dSNick Piggin } 560fa0d7e3dSNick Piggin 56187bc5b89SYan, Zheng void ceph_evict_inode(struct inode *inode) 562355da1ebSSage Weil { 563355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 5641dd8d470SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 565355da1ebSSage Weil struct ceph_inode_frag *frag; 566355da1ebSSage Weil struct rb_node *n; 567355da1ebSSage Weil 56887bc5b89SYan, Zheng dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode)); 56987bc5b89SYan, Zheng 5701dd8d470SXiubo Li percpu_counter_dec(&mdsc->metric.total_inodes); 5711dd8d470SXiubo Li 57287bc5b89SYan, Zheng truncate_inode_pages_final(&inode->i_data); 573400e1286SJeff Layton if (inode->i_state & I_PINNING_FSCACHE_WB) 574400e1286SJeff Layton ceph_fscache_unuse_cookie(inode, true); 57587bc5b89SYan, Zheng clear_inode(inode); 576355da1ebSSage Weil 57799ccbd22SMilosz Tanski ceph_fscache_unregister_inode_cookie(ci); 57899ccbd22SMilosz Tanski 579d6e47819SYan, Zheng __ceph_remove_caps(ci); 580355da1ebSSage Weil 58155ab5520SLuís Henriques if (__ceph_has_quota(ci, QUOTA_GET_ANY)) 582d557c48dSLuis Henriques ceph_adjust_quota_realms_count(inode, false); 583d557c48dSLuis Henriques 5848b218b8aSSage Weil /* 5858b218b8aSSage Weil * we may still have a snap_realm reference if there are stray 586d9df2783SYan, Zheng * caps in i_snap_caps. 5878b218b8aSSage Weil */ 5888b218b8aSSage Weil if (ci->i_snap_realm) { 58975c9627eSYan, Zheng if (ceph_snap(inode) == CEPH_NOSNAP) { 59075c9627eSYan, Zheng dout(" dropping residual ref to snap realm %p\n", 5910ba92e1cSJeff Layton ci->i_snap_realm); 5920ba92e1cSJeff Layton ceph_change_snap_realm(inode, NULL); 59375c9627eSYan, Zheng } else { 59475c9627eSYan, Zheng ceph_put_snapid_map(mdsc, ci->i_snapid_map); 59575c9627eSYan, Zheng ci->i_snap_realm = NULL; 59675c9627eSYan, Zheng } 5978b218b8aSSage Weil } 5988b218b8aSSage Weil 599355da1ebSSage Weil while ((n = rb_first(&ci->i_fragtree)) != NULL) { 600355da1ebSSage Weil frag = rb_entry(n, struct ceph_inode_frag, node); 601355da1ebSSage Weil rb_erase(n, &ci->i_fragtree); 602355da1ebSSage Weil kfree(frag); 603355da1ebSSage Weil } 6041b1bc16dSYan, Zheng ci->i_fragtree_nsplits = 0; 605355da1ebSSage Weil 606355da1ebSSage Weil __ceph_destroy_xattrs(ci); 607b6c1d5b8SSage Weil if (ci->i_xattrs.blob) 608355da1ebSSage Weil ceph_buffer_put(ci->i_xattrs.blob); 609b6c1d5b8SSage Weil if (ci->i_xattrs.prealloc_blob) 610355da1ebSSage Weil ceph_buffer_put(ci->i_xattrs.prealloc_blob); 611355da1ebSSage Weil 612779fe0fbSYan, Zheng ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns)); 613785892feSJeff Layton ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns)); 614355da1ebSSage Weil } 615355da1ebSSage Weil 616224a7542SYan, Zheng static inline blkcnt_t calc_inode_blocks(u64 size) 617224a7542SYan, Zheng { 618224a7542SYan, Zheng return (size + (1<<9) - 1) >> 9; 619224a7542SYan, Zheng } 620224a7542SYan, Zheng 621355da1ebSSage Weil /* 622355da1ebSSage Weil * Helpers to fill in size, ctime, mtime, and atime. We have to be 623355da1ebSSage Weil * careful because either the client or MDS may have more up to date 624355da1ebSSage Weil * info, depending on which capabilities are held, and whether 625355da1ebSSage Weil * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime 626355da1ebSSage Weil * and size are monotonically increasing, except when utimes() or 627355da1ebSSage Weil * truncate() increments the corresponding _seq values.) 628355da1ebSSage Weil */ 629355da1ebSSage Weil int ceph_fill_file_size(struct inode *inode, int issued, 630355da1ebSSage Weil u32 truncate_seq, u64 truncate_size, u64 size) 631355da1ebSSage Weil { 632355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 633355da1ebSSage Weil int queue_trunc = 0; 6342d6795fbSJeff Layton loff_t isize = i_size_read(inode); 635355da1ebSSage Weil 636355da1ebSSage Weil if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 || 6372d6795fbSJeff Layton (truncate_seq == ci->i_truncate_seq && size > isize)) { 6382d6795fbSJeff Layton dout("size %lld -> %llu\n", isize, size); 639a3d714c3SYan, Zheng if (size > 0 && S_ISDIR(inode->i_mode)) { 640a3d714c3SYan, Zheng pr_err("fill_file_size non-zero size for directory\n"); 641a3d714c3SYan, Zheng size = 0; 642a3d714c3SYan, Zheng } 64399c88e69SYan, Zheng i_size_write(inode, size); 644224a7542SYan, Zheng inode->i_blocks = calc_inode_blocks(size); 645400e1286SJeff Layton /* 646400e1286SJeff Layton * If we're expanding, then we should be able to just update 647400e1286SJeff Layton * the existing cookie. 648400e1286SJeff Layton */ 649400e1286SJeff Layton if (size > isize) 650400e1286SJeff Layton ceph_fscache_update(inode); 651355da1ebSSage Weil ci->i_reported_size = size; 652355da1ebSSage Weil if (truncate_seq != ci->i_truncate_seq) { 653355da1ebSSage Weil dout("truncate_seq %u -> %u\n", 654355da1ebSSage Weil ci->i_truncate_seq, truncate_seq); 655355da1ebSSage Weil ci->i_truncate_seq = truncate_seq; 656b0d7c223SYan, Zheng 657b0d7c223SYan, Zheng /* the MDS should have revoked these caps */ 658b0d7c223SYan, Zheng WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL | 659b0d7c223SYan, Zheng CEPH_CAP_FILE_RD | 660b0d7c223SYan, Zheng CEPH_CAP_FILE_WR | 661b0d7c223SYan, Zheng CEPH_CAP_FILE_LAZYIO)); 6623d497d85SYehuda Sadeh /* 6633d497d85SYehuda Sadeh * If we hold relevant caps, or in the case where we're 6643d497d85SYehuda Sadeh * not the only client referencing this file and we 6653d497d85SYehuda Sadeh * don't hold those caps, then we need to check whether 6663d497d85SYehuda Sadeh * the file is either opened or mmaped 6673d497d85SYehuda Sadeh */ 668b0d7c223SYan, Zheng if ((issued & (CEPH_CAP_FILE_CACHE| 669b0d7c223SYan, Zheng CEPH_CAP_FILE_BUFFER)) || 6703d497d85SYehuda Sadeh mapping_mapped(inode->i_mapping) || 671719a2514SYan, Zheng __ceph_is_file_opened(ci)) { 672355da1ebSSage Weil ci->i_truncate_pending++; 673355da1ebSSage Weil queue_trunc = 1; 674355da1ebSSage Weil } 675355da1ebSSage Weil } 676355da1ebSSage Weil } 677355da1ebSSage Weil if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 && 678355da1ebSSage Weil ci->i_truncate_size != truncate_size) { 679355da1ebSSage Weil dout("truncate_size %lld -> %llu\n", ci->i_truncate_size, 680355da1ebSSage Weil truncate_size); 681355da1ebSSage Weil ci->i_truncate_size = truncate_size; 682355da1ebSSage Weil } 683355da1ebSSage Weil return queue_trunc; 684355da1ebSSage Weil } 685355da1ebSSage Weil 686355da1ebSSage Weil void ceph_fill_file_time(struct inode *inode, int issued, 6879bbeab41SArnd Bergmann u64 time_warp_seq, struct timespec64 *ctime, 6889bbeab41SArnd Bergmann struct timespec64 *mtime, struct timespec64 *atime) 689355da1ebSSage Weil { 690355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 691355da1ebSSage Weil int warn = 0; 692355da1ebSSage Weil 693355da1ebSSage Weil if (issued & (CEPH_CAP_FILE_EXCL| 694355da1ebSSage Weil CEPH_CAP_FILE_WR| 695d8672d64SSage Weil CEPH_CAP_FILE_BUFFER| 696d8672d64SSage Weil CEPH_CAP_AUTH_EXCL| 697d8672d64SSage Weil CEPH_CAP_XATTR_EXCL)) { 698ffdeec7aSYan, Zheng if (ci->i_version == 0 || 6999bbeab41SArnd Bergmann timespec64_compare(ctime, &inode->i_ctime) > 0) { 70013442b03SDeepa Dinamani dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n", 7019bbeab41SArnd Bergmann inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, 7029bbeab41SArnd Bergmann ctime->tv_sec, ctime->tv_nsec); 7039bbeab41SArnd Bergmann inode->i_ctime = *ctime; 704355da1ebSSage Weil } 705ffdeec7aSYan, Zheng if (ci->i_version == 0 || 706ffdeec7aSYan, Zheng ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) { 707355da1ebSSage Weil /* the MDS did a utimes() */ 70813442b03SDeepa Dinamani dout("mtime %lld.%09ld -> %lld.%09ld " 709355da1ebSSage Weil "tw %d -> %d\n", 7109bbeab41SArnd Bergmann inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, 7119bbeab41SArnd Bergmann mtime->tv_sec, mtime->tv_nsec, 712355da1ebSSage Weil ci->i_time_warp_seq, (int)time_warp_seq); 713355da1ebSSage Weil 7149bbeab41SArnd Bergmann inode->i_mtime = *mtime; 7159bbeab41SArnd Bergmann inode->i_atime = *atime; 716355da1ebSSage Weil ci->i_time_warp_seq = time_warp_seq; 717355da1ebSSage Weil } else if (time_warp_seq == ci->i_time_warp_seq) { 718355da1ebSSage Weil /* nobody did utimes(); take the max */ 7199bbeab41SArnd Bergmann if (timespec64_compare(mtime, &inode->i_mtime) > 0) { 72013442b03SDeepa Dinamani dout("mtime %lld.%09ld -> %lld.%09ld inc\n", 7219bbeab41SArnd Bergmann inode->i_mtime.tv_sec, 722355da1ebSSage Weil inode->i_mtime.tv_nsec, 7239bbeab41SArnd Bergmann mtime->tv_sec, mtime->tv_nsec); 7249bbeab41SArnd Bergmann inode->i_mtime = *mtime; 725355da1ebSSage Weil } 7269bbeab41SArnd Bergmann if (timespec64_compare(atime, &inode->i_atime) > 0) { 72713442b03SDeepa Dinamani dout("atime %lld.%09ld -> %lld.%09ld inc\n", 7289bbeab41SArnd Bergmann inode->i_atime.tv_sec, 729355da1ebSSage Weil inode->i_atime.tv_nsec, 7309bbeab41SArnd Bergmann atime->tv_sec, atime->tv_nsec); 7319bbeab41SArnd Bergmann inode->i_atime = *atime; 732355da1ebSSage Weil } 733355da1ebSSage Weil } else if (issued & CEPH_CAP_FILE_EXCL) { 734355da1ebSSage Weil /* we did a utimes(); ignore mds values */ 735355da1ebSSage Weil } else { 736355da1ebSSage Weil warn = 1; 737355da1ebSSage Weil } 738355da1ebSSage Weil } else { 739d8672d64SSage Weil /* we have no write|excl caps; whatever the MDS says is true */ 740355da1ebSSage Weil if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) { 7419bbeab41SArnd Bergmann inode->i_ctime = *ctime; 7429bbeab41SArnd Bergmann inode->i_mtime = *mtime; 7439bbeab41SArnd Bergmann inode->i_atime = *atime; 744355da1ebSSage Weil ci->i_time_warp_seq = time_warp_seq; 745355da1ebSSage Weil } else { 746355da1ebSSage Weil warn = 1; 747355da1ebSSage Weil } 748355da1ebSSage Weil } 749355da1ebSSage Weil if (warn) /* time_warp_seq shouldn't go backwards */ 750355da1ebSSage Weil dout("%p mds time_warp_seq %llu < %u\n", 751355da1ebSSage Weil inode, time_warp_seq, ci->i_time_warp_seq); 752355da1ebSSage Weil } 753355da1ebSSage Weil 754355da1ebSSage Weil /* 755355da1ebSSage Weil * Populate an inode based on info from mds. May be called on new or 756355da1ebSSage Weil * existing inodes. 757355da1ebSSage Weil */ 758966c7160SJeff Layton int ceph_fill_inode(struct inode *inode, struct page *locked_page, 759355da1ebSSage Weil struct ceph_mds_reply_info_in *iinfo, 760355da1ebSSage Weil struct ceph_mds_reply_dirfrag *dirinfo, 76157c21994SJeff Layton struct ceph_mds_session *session, int cap_fmode, 762355da1ebSSage Weil struct ceph_cap_reservation *caps_reservation) 763355da1ebSSage Weil { 7642678da88SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 765355da1ebSSage Weil struct ceph_mds_reply_inode *info = iinfo->in; 766355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 7672af54a72SYan, Zheng int issued, new_issued, info_caps; 7689bbeab41SArnd Bergmann struct timespec64 mtime, atime, ctime; 769355da1ebSSage Weil struct ceph_buffer *xattr_blob = NULL; 770af8a85a4SLuis Henriques struct ceph_buffer *old_blob = NULL; 771779fe0fbSYan, Zheng struct ceph_string *pool_ns = NULL; 772d9df2783SYan, Zheng struct ceph_cap *new_cap = NULL; 773355da1ebSSage Weil int err = 0; 774d9df2783SYan, Zheng bool wake = false; 775f98a128aSYan, Zheng bool queue_trunc = false; 776f98a128aSYan, Zheng bool new_version = false; 77731c542a1SYan, Zheng bool fill_inline = false; 778ed94f87cSJeff Layton umode_t mode = le32_to_cpu(info->mode); 779ed94f87cSJeff Layton dev_t rdev = le32_to_cpu(info->rdev); 780355da1ebSSage Weil 78127171ae6SJeff Layton lockdep_assert_held(&mdsc->snap_rwsem); 78227171ae6SJeff Layton 783966c7160SJeff Layton dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__, 784355da1ebSSage Weil inode, ceph_vinop(inode), le64_to_cpu(info->version), 785355da1ebSSage Weil ci->i_version); 786355da1ebSSage Weil 787ed94f87cSJeff Layton /* Once I_NEW is cleared, we can't change type or dev numbers */ 788ed94f87cSJeff Layton if (inode->i_state & I_NEW) { 789ed94f87cSJeff Layton inode->i_mode = mode; 790ed94f87cSJeff Layton } else { 791ed94f87cSJeff Layton if (inode_wrong_type(inode, mode)) { 792ed94f87cSJeff Layton pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n", 793ed94f87cSJeff Layton ceph_vinop(inode), inode->i_mode, mode); 794ed94f87cSJeff Layton return -ESTALE; 795ed94f87cSJeff Layton } 796ed94f87cSJeff Layton 797ed94f87cSJeff Layton if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) { 798ed94f87cSJeff Layton pr_warn_once("dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n", 799ed94f87cSJeff Layton ceph_vinop(inode), MAJOR(inode->i_rdev), 800ed94f87cSJeff Layton MINOR(inode->i_rdev), MAJOR(rdev), 801ed94f87cSJeff Layton MINOR(rdev)); 802ed94f87cSJeff Layton return -ESTALE; 803ed94f87cSJeff Layton } 804ed94f87cSJeff Layton } 805ed94f87cSJeff Layton 8062af54a72SYan, Zheng info_caps = le32_to_cpu(info->cap.caps); 8072af54a72SYan, Zheng 808d9df2783SYan, Zheng /* prealloc new cap struct */ 8099a6bed4fSJeff Layton if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) { 810d9df2783SYan, Zheng new_cap = ceph_get_cap(mdsc, caps_reservation); 8119a6bed4fSJeff Layton if (!new_cap) 8129a6bed4fSJeff Layton return -ENOMEM; 8139a6bed4fSJeff Layton } 814d9df2783SYan, Zheng 815355da1ebSSage Weil /* 816355da1ebSSage Weil * prealloc xattr data, if it looks like we'll need it. only 817355da1ebSSage Weil * if len > 4 (meaning there are actually xattrs; the first 4 818355da1ebSSage Weil * bytes are the xattr count). 819355da1ebSSage Weil */ 820355da1ebSSage Weil if (iinfo->xattr_len > 4) { 821b6c1d5b8SSage Weil xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS); 822355da1ebSSage Weil if (!xattr_blob) 823966c7160SJeff Layton pr_err("%s ENOMEM xattr blob %d bytes\n", __func__, 824355da1ebSSage Weil iinfo->xattr_len); 825355da1ebSSage Weil } 826355da1ebSSage Weil 827779fe0fbSYan, Zheng if (iinfo->pool_ns_len > 0) 828779fe0fbSYan, Zheng pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data, 829779fe0fbSYan, Zheng iinfo->pool_ns_len); 830779fe0fbSYan, Zheng 83175c9627eSYan, Zheng if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map) 83275c9627eSYan, Zheng ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode)); 83375c9627eSYan, Zheng 834be655596SSage Weil spin_lock(&ci->i_ceph_lock); 835355da1ebSSage Weil 836355da1ebSSage Weil /* 837355da1ebSSage Weil * provided version will be odd if inode value is projected, 8388bd59e01SSage Weil * even if stable. skip the update if we have newer stable 8398bd59e01SSage Weil * info (ours>=theirs, e.g. due to racing mds replies), unless 8408bd59e01SSage Weil * we are getting projected (unstable) info (in which case the 8418bd59e01SSage Weil * version is odd, and we want ours>theirs). 8428bd59e01SSage Weil * us them 8438bd59e01SSage Weil * 2 2 skip 8448bd59e01SSage Weil * 3 2 skip 8458bd59e01SSage Weil * 3 3 update 846355da1ebSSage Weil */ 847f98a128aSYan, Zheng if (ci->i_version == 0 || 848f98a128aSYan, Zheng ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && 849f98a128aSYan, Zheng le64_to_cpu(info->version) > (ci->i_version & ~1))) 850f98a128aSYan, Zheng new_version = true; 851355da1ebSSage Weil 852a35ead31SJeff Layton /* Update change_attribute */ 853a35ead31SJeff Layton inode_set_max_iversion_raw(inode, iinfo->change_attr); 854a35ead31SJeff Layton 8552af54a72SYan, Zheng __ceph_caps_issued(ci, &issued); 8562af54a72SYan, Zheng issued |= __ceph_caps_dirty(ci); 8572af54a72SYan, Zheng new_issued = ~issued & info_caps; 858355da1ebSSage Weil 85975067034SLuis Henriques /* directories have fl_stripe_unit set to zero */ 86075067034SLuis Henriques if (le32_to_cpu(info->layout.fl_stripe_unit)) 86175067034SLuis Henriques inode->i_blkbits = 86275067034SLuis Henriques fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1; 86375067034SLuis Henriques else 86475067034SLuis Henriques inode->i_blkbits = CEPH_BLOCK_SHIFT; 865355da1ebSSage Weil 866d557c48dSLuis Henriques __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files); 867fb18a575SLuis Henriques 868f98a128aSYan, Zheng if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) && 869f98a128aSYan, Zheng (issued & CEPH_CAP_AUTH_EXCL) == 0) { 870ed94f87cSJeff Layton inode->i_mode = mode; 871ab871b90SEric W. Biederman inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid)); 872ab871b90SEric W. Biederman inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid)); 873355da1ebSSage Weil dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, 874bd2bae6aSEric W. Biederman from_kuid(&init_user_ns, inode->i_uid), 875bd2bae6aSEric W. Biederman from_kgid(&init_user_ns, inode->i_gid)); 876245ce991SJeff Layton ceph_decode_timespec64(&ci->i_btime, &iinfo->btime); 877193e7b37SDavid Disseldorp ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime); 878355da1ebSSage Weil } 879355da1ebSSage Weil 880f98a128aSYan, Zheng if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) && 881f98a128aSYan, Zheng (issued & CEPH_CAP_LINK_EXCL) == 0) 882bfe86848SMiklos Szeredi set_nlink(inode, le32_to_cpu(info->nlink)); 883355da1ebSSage Weil 884f98a128aSYan, Zheng if (new_version || (new_issued & CEPH_CAP_ANY_RD)) { 885355da1ebSSage Weil /* be careful with mtime, atime, size */ 8869bbeab41SArnd Bergmann ceph_decode_timespec64(&atime, &info->atime); 8879bbeab41SArnd Bergmann ceph_decode_timespec64(&mtime, &info->mtime); 8889bbeab41SArnd Bergmann ceph_decode_timespec64(&ctime, &info->ctime); 889f98a128aSYan, Zheng ceph_fill_file_time(inode, issued, 890f98a128aSYan, Zheng le32_to_cpu(info->time_warp_seq), 891f98a128aSYan, Zheng &ctime, &mtime, &atime); 892f98a128aSYan, Zheng } 893f98a128aSYan, Zheng 8942af54a72SYan, Zheng if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) { 8952af54a72SYan, Zheng ci->i_files = le64_to_cpu(info->files); 8962af54a72SYan, Zheng ci->i_subdirs = le64_to_cpu(info->subdirs); 8972af54a72SYan, Zheng } 8982af54a72SYan, Zheng 899f98a128aSYan, Zheng if (new_version || 900f98a128aSYan, Zheng (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) { 9017627151eSYan, Zheng s64 old_pool = ci->i_layout.pool_id; 902779fe0fbSYan, Zheng struct ceph_string *old_ns; 903779fe0fbSYan, Zheng 9047627151eSYan, Zheng ceph_file_layout_from_legacy(&ci->i_layout, &info->layout); 905779fe0fbSYan, Zheng old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, 906779fe0fbSYan, Zheng lockdep_is_held(&ci->i_ceph_lock)); 907779fe0fbSYan, Zheng rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns); 908779fe0fbSYan, Zheng 909779fe0fbSYan, Zheng if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns) 91010183a69SYan, Zheng ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; 91110183a69SYan, Zheng 912779fe0fbSYan, Zheng pool_ns = old_ns; 913f98a128aSYan, Zheng 914355da1ebSSage Weil queue_trunc = ceph_fill_file_size(inode, issued, 915355da1ebSSage Weil le32_to_cpu(info->truncate_seq), 916355da1ebSSage Weil le64_to_cpu(info->truncate_size), 917355da1ebSSage Weil le64_to_cpu(info->size)); 918f98a128aSYan, Zheng /* only update max_size on auth cap */ 919f98a128aSYan, Zheng if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && 920f98a128aSYan, Zheng ci->i_max_size != le64_to_cpu(info->max_size)) { 921f98a128aSYan, Zheng dout("max_size %lld -> %llu\n", ci->i_max_size, 922f98a128aSYan, Zheng le64_to_cpu(info->max_size)); 923f98a128aSYan, Zheng ci->i_max_size = le64_to_cpu(info->max_size); 924f98a128aSYan, Zheng } 925f98a128aSYan, Zheng } 926355da1ebSSage Weil 92749a9f4f6SYan, Zheng /* layout and rstat are not tracked by capability, update them if 92849a9f4f6SYan, Zheng * the inode info is from auth mds */ 92949a9f4f6SYan, Zheng if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) { 93049a9f4f6SYan, Zheng if (S_ISDIR(inode->i_mode)) { 93149a9f4f6SYan, Zheng ci->i_dir_layout = iinfo->dir_layout; 93249a9f4f6SYan, Zheng ci->i_rbytes = le64_to_cpu(info->rbytes); 93349a9f4f6SYan, Zheng ci->i_rfiles = le64_to_cpu(info->rfiles); 93449a9f4f6SYan, Zheng ci->i_rsubdirs = le64_to_cpu(info->rsubdirs); 93508796873SYan, Zheng ci->i_dir_pin = iinfo->dir_pin; 936e7f72952SYanhu Cao ci->i_rsnaps = iinfo->rsnaps; 9379bbeab41SArnd Bergmann ceph_decode_timespec64(&ci->i_rctime, &info->rctime); 93849a9f4f6SYan, Zheng } 93949a9f4f6SYan, Zheng } 94049a9f4f6SYan, Zheng 941355da1ebSSage Weil /* xattrs */ 942355da1ebSSage Weil /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */ 943508b32d8SYan, Zheng if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) && 944355da1ebSSage Weil le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) { 945355da1ebSSage Weil if (ci->i_xattrs.blob) 946af8a85a4SLuis Henriques old_blob = ci->i_xattrs.blob; 947355da1ebSSage Weil ci->i_xattrs.blob = xattr_blob; 948355da1ebSSage Weil if (xattr_blob) 949355da1ebSSage Weil memcpy(ci->i_xattrs.blob->vec.iov_base, 950355da1ebSSage Weil iinfo->xattr_data, iinfo->xattr_len); 951355da1ebSSage Weil ci->i_xattrs.version = le64_to_cpu(info->xattr_version); 9527221fe4cSGuangliang Zhao ceph_forget_all_cached_acls(inode); 953ac6713ccSYan, Zheng ceph_security_invalidate_secctx(inode); 954a6424e48SSage Weil xattr_blob = NULL; 955355da1ebSSage Weil } 956355da1ebSSage Weil 957ffdeec7aSYan, Zheng /* finally update i_version */ 958aae1a442SYan, Zheng if (le64_to_cpu(info->version) > ci->i_version) 959ffdeec7aSYan, Zheng ci->i_version = le64_to_cpu(info->version); 960ffdeec7aSYan, Zheng 961355da1ebSSage Weil inode->i_mapping->a_ops = &ceph_aops; 962355da1ebSSage Weil 963355da1ebSSage Weil switch (inode->i_mode & S_IFMT) { 964355da1ebSSage Weil case S_IFIFO: 965355da1ebSSage Weil case S_IFBLK: 966355da1ebSSage Weil case S_IFCHR: 967355da1ebSSage Weil case S_IFSOCK: 9685ba72e60SYan, Zheng inode->i_blkbits = PAGE_SHIFT; 969ed94f87cSJeff Layton init_special_inode(inode, inode->i_mode, rdev); 970355da1ebSSage Weil inode->i_op = &ceph_file_iops; 971355da1ebSSage Weil break; 972355da1ebSSage Weil case S_IFREG: 973355da1ebSSage Weil inode->i_op = &ceph_file_iops; 974355da1ebSSage Weil inode->i_fop = &ceph_file_fops; 975355da1ebSSage Weil break; 976355da1ebSSage Weil case S_IFLNK: 977355da1ebSSage Weil inode->i_op = &ceph_symlink_iops; 978355da1ebSSage Weil if (!ci->i_symlink) { 979810339ecSXi Wang u32 symlen = iinfo->symlink_len; 980355da1ebSSage Weil char *sym; 981355da1ebSSage Weil 982be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 983355da1ebSSage Weil 984224a7542SYan, Zheng if (symlen != i_size_read(inode)) { 985966c7160SJeff Layton pr_err("%s %llx.%llx BAD symlink " 986966c7160SJeff Layton "size %lld\n", __func__, 987966c7160SJeff Layton ceph_vinop(inode), 988224a7542SYan, Zheng i_size_read(inode)); 989224a7542SYan, Zheng i_size_write(inode, symlen); 990224a7542SYan, Zheng inode->i_blocks = calc_inode_blocks(symlen); 991224a7542SYan, Zheng } 992810339ecSXi Wang 993355da1ebSSage Weil err = -ENOMEM; 994810339ecSXi Wang sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS); 995355da1ebSSage Weil if (!sym) 996355da1ebSSage Weil goto out; 997355da1ebSSage Weil 998be655596SSage Weil spin_lock(&ci->i_ceph_lock); 999355da1ebSSage Weil if (!ci->i_symlink) 1000355da1ebSSage Weil ci->i_symlink = sym; 1001355da1ebSSage Weil else 1002355da1ebSSage Weil kfree(sym); /* lost a race */ 1003355da1ebSSage Weil } 1004ac194dccSAl Viro inode->i_link = ci->i_symlink; 1005355da1ebSSage Weil break; 1006355da1ebSSage Weil case S_IFDIR: 1007355da1ebSSage Weil inode->i_op = &ceph_dir_iops; 1008355da1ebSSage Weil inode->i_fop = &ceph_dir_fops; 1009355da1ebSSage Weil break; 1010355da1ebSSage Weil default: 1011966c7160SJeff Layton pr_err("%s %llx.%llx BAD mode 0%o\n", __func__, 1012355da1ebSSage Weil ceph_vinop(inode), inode->i_mode); 1013355da1ebSSage Weil } 1014355da1ebSSage Weil 1015355da1ebSSage Weil /* were we issued a capability? */ 10162af54a72SYan, Zheng if (info_caps) { 1017355da1ebSSage Weil if (ceph_snap(inode) == CEPH_NOSNAP) { 1018355da1ebSSage Weil ceph_add_cap(inode, session, 1019355da1ebSSage Weil le64_to_cpu(info->cap.cap_id), 1020135e671eSYan, Zheng info_caps, 1021355da1ebSSage Weil le32_to_cpu(info->cap.wanted), 1022355da1ebSSage Weil le32_to_cpu(info->cap.seq), 1023355da1ebSSage Weil le32_to_cpu(info->cap.mseq), 1024355da1ebSSage Weil le64_to_cpu(info->cap.realm), 1025d9df2783SYan, Zheng info->cap.flags, &new_cap); 10262f92b3d0SYan, Zheng 10272f92b3d0SYan, Zheng /* set dir completion flag? */ 10282f92b3d0SYan, Zheng if (S_ISDIR(inode->i_mode) && 10292f92b3d0SYan, Zheng ci->i_files == 0 && ci->i_subdirs == 0 && 10302af54a72SYan, Zheng (info_caps & CEPH_CAP_FILE_SHARED) && 10312f92b3d0SYan, Zheng (issued & CEPH_CAP_FILE_EXCL) == 0 && 10322f92b3d0SYan, Zheng !__ceph_dir_is_complete(ci)) { 10332f92b3d0SYan, Zheng dout(" marking %p complete (empty)\n", inode); 1034fdd4e158SYan, Zheng i_size_write(inode, 0); 10352f92b3d0SYan, Zheng __ceph_dir_set_complete(ci, 1036fdd4e158SYan, Zheng atomic64_read(&ci->i_release_count), 1037fdd4e158SYan, Zheng atomic64_read(&ci->i_ordered_count)); 10382f92b3d0SYan, Zheng } 10392f92b3d0SYan, Zheng 1040d9df2783SYan, Zheng wake = true; 1041355da1ebSSage Weil } else { 1042355da1ebSSage Weil dout(" %p got snap_caps %s\n", inode, 10432af54a72SYan, Zheng ceph_cap_string(info_caps)); 10442af54a72SYan, Zheng ci->i_snap_caps |= info_caps; 1045355da1ebSSage Weil } 1046355da1ebSSage Weil } 104731c542a1SYan, Zheng 104831c542a1SYan, Zheng if (iinfo->inline_version > 0 && 104931c542a1SYan, Zheng iinfo->inline_version >= ci->i_inline_version) { 105031c542a1SYan, Zheng int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; 105131c542a1SYan, Zheng ci->i_inline_version = iinfo->inline_version; 105248490776SXiubo Li if (ceph_has_inline_data(ci) && 10532af54a72SYan, Zheng (locked_page || (info_caps & cache_caps))) 105431c542a1SYan, Zheng fill_inline = true; 105531c542a1SYan, Zheng } 105631c542a1SYan, Zheng 1057719a2514SYan, Zheng if (cap_fmode >= 0) { 1058719a2514SYan, Zheng if (!info_caps) 1059719a2514SYan, Zheng pr_warn("mds issued no caps on %llx.%llx\n", 1060719a2514SYan, Zheng ceph_vinop(inode)); 1061719a2514SYan, Zheng __ceph_touch_fmode(ci, mdsc, cap_fmode); 1062719a2514SYan, Zheng } 1063719a2514SYan, Zheng 1064355da1ebSSage Weil spin_unlock(&ci->i_ceph_lock); 1065355da1ebSSage Weil 1066400e1286SJeff Layton ceph_fscache_register_inode_cookie(inode); 1067400e1286SJeff Layton 106831c542a1SYan, Zheng if (fill_inline) 106901deead0SYan, Zheng ceph_fill_inline_data(inode, locked_page, 107031c542a1SYan, Zheng iinfo->inline_data, iinfo->inline_len); 107131c542a1SYan, Zheng 1072d9df2783SYan, Zheng if (wake) 1073d9df2783SYan, Zheng wake_up_all(&ci->i_cap_wq); 1074d9df2783SYan, Zheng 1075355da1ebSSage Weil /* queue truncate if we saw i_size decrease */ 1076355da1ebSSage Weil if (queue_trunc) 1077355da1ebSSage Weil ceph_queue_vmtruncate(inode); 1078355da1ebSSage Weil 1079355da1ebSSage Weil /* populate frag tree */ 10803e7fbe9cSYan, Zheng if (S_ISDIR(inode->i_mode)) 10813e7fbe9cSYan, Zheng ceph_fill_fragtree(inode, &info->fragtree, dirinfo); 1082355da1ebSSage Weil 1083355da1ebSSage Weil /* update delegation info? */ 1084355da1ebSSage Weil if (dirinfo) 1085355da1ebSSage Weil ceph_fill_dirfrag(inode, dirinfo); 1086355da1ebSSage Weil 1087355da1ebSSage Weil err = 0; 1088355da1ebSSage Weil out: 1089d9df2783SYan, Zheng if (new_cap) 1090d9df2783SYan, Zheng ceph_put_cap(mdsc, new_cap); 1091af8a85a4SLuis Henriques ceph_buffer_put(old_blob); 1092355da1ebSSage Weil ceph_buffer_put(xattr_blob); 1093779fe0fbSYan, Zheng ceph_put_string(pool_ns); 1094355da1ebSSage Weil return err; 1095355da1ebSSage Weil } 1096355da1ebSSage Weil 1097355da1ebSSage Weil /* 1098543212b3SYan, Zheng * caller should hold session s_mutex and dentry->d_lock. 1099355da1ebSSage Weil */ 1100543212b3SYan, Zheng static void __update_dentry_lease(struct inode *dir, struct dentry *dentry, 1101355da1ebSSage Weil struct ceph_mds_reply_lease *lease, 1102355da1ebSSage Weil struct ceph_mds_session *session, 1103f5d55f03SJeff Layton unsigned long from_time, 1104543212b3SYan, Zheng struct ceph_mds_session **old_lease_session) 1105355da1ebSSage Weil { 1106355da1ebSSage Weil struct ceph_dentry_info *di = ceph_dentry(dentry); 1107f5e17aedSJeff Layton unsigned mask = le16_to_cpu(lease->mask); 1108355da1ebSSage Weil long unsigned duration = le32_to_cpu(lease->duration_ms); 1109355da1ebSSage Weil long unsigned ttl = from_time + (duration * HZ) / 1000; 1110355da1ebSSage Weil long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000; 1111355da1ebSSage Weil 11122f90b852SSage Weil dout("update_dentry_lease %p duration %lu ms ttl %lu\n", 11132f90b852SSage Weil dentry, duration, ttl); 1114355da1ebSSage Weil 111518fc8abdSAl Viro /* only track leases on regular dentries */ 111618fc8abdSAl Viro if (ceph_snap(dir) != CEPH_NOSNAP) 1117543212b3SYan, Zheng return; 111818fc8abdSAl Viro 1119f5e17aedSJeff Layton if (mask & CEPH_LEASE_PRIMARY_LINK) 1120f5e17aedSJeff Layton di->flags |= CEPH_DENTRY_PRIMARY_LINK; 1121f5e17aedSJeff Layton else 1122f5e17aedSJeff Layton di->flags &= ~CEPH_DENTRY_PRIMARY_LINK; 1123f5e17aedSJeff Layton 112497aeb6bfSYan, Zheng di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen); 1125f5e17aedSJeff Layton if (!(mask & CEPH_LEASE_VALID)) { 112637c4efc1SYan, Zheng __ceph_dentry_dir_lease_touch(di); 1127543212b3SYan, Zheng return; 112837c4efc1SYan, Zheng } 1129355da1ebSSage Weil 113052d60f8eSJeff Layton if (di->lease_gen == atomic_read(&session->s_cap_gen) && 11319b16f03cSMiklos Szeredi time_before(ttl, di->time)) 1132543212b3SYan, Zheng return; /* we already have a newer lease. */ 1133355da1ebSSage Weil 1134481f001fSYan, Zheng if (di->lease_session && di->lease_session != session) { 1135543212b3SYan, Zheng *old_lease_session = di->lease_session; 1136481f001fSYan, Zheng di->lease_session = NULL; 1137481f001fSYan, Zheng } 1138355da1ebSSage Weil 1139355da1ebSSage Weil if (!di->lease_session) 1140355da1ebSSage Weil di->lease_session = ceph_get_mds_session(session); 114152d60f8eSJeff Layton di->lease_gen = atomic_read(&session->s_cap_gen); 1142355da1ebSSage Weil di->lease_seq = le32_to_cpu(lease->seq); 1143355da1ebSSage Weil di->lease_renew_after = half_ttl; 1144355da1ebSSage Weil di->lease_renew_from = 0; 11459b16f03cSMiklos Szeredi di->time = ttl; 114637c4efc1SYan, Zheng 114737c4efc1SYan, Zheng __ceph_dentry_lease_touch(di); 1148543212b3SYan, Zheng } 1149543212b3SYan, Zheng 1150543212b3SYan, Zheng static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry, 1151543212b3SYan, Zheng struct ceph_mds_reply_lease *lease, 1152543212b3SYan, Zheng struct ceph_mds_session *session, 1153543212b3SYan, Zheng unsigned long from_time) 1154543212b3SYan, Zheng { 1155543212b3SYan, Zheng struct ceph_mds_session *old_lease_session = NULL; 1156543212b3SYan, Zheng spin_lock(&dentry->d_lock); 1157543212b3SYan, Zheng __update_dentry_lease(dir, dentry, lease, session, from_time, 1158543212b3SYan, Zheng &old_lease_session); 1159543212b3SYan, Zheng spin_unlock(&dentry->d_lock); 1160543212b3SYan, Zheng ceph_put_mds_session(old_lease_session); 1161543212b3SYan, Zheng } 1162543212b3SYan, Zheng 1163543212b3SYan, Zheng /* 1164543212b3SYan, Zheng * update dentry lease without having parent inode locked 1165543212b3SYan, Zheng */ 1166543212b3SYan, Zheng static void update_dentry_lease_careful(struct dentry *dentry, 1167543212b3SYan, Zheng struct ceph_mds_reply_lease *lease, 1168543212b3SYan, Zheng struct ceph_mds_session *session, 1169543212b3SYan, Zheng unsigned long from_time, 1170543212b3SYan, Zheng char *dname, u32 dname_len, 1171543212b3SYan, Zheng struct ceph_vino *pdvino, 1172543212b3SYan, Zheng struct ceph_vino *ptvino) 1173543212b3SYan, Zheng 1174543212b3SYan, Zheng { 1175543212b3SYan, Zheng struct inode *dir; 1176543212b3SYan, Zheng struct ceph_mds_session *old_lease_session = NULL; 1177543212b3SYan, Zheng 1178543212b3SYan, Zheng spin_lock(&dentry->d_lock); 1179543212b3SYan, Zheng /* make sure dentry's name matches target */ 1180543212b3SYan, Zheng if (dentry->d_name.len != dname_len || 1181543212b3SYan, Zheng memcmp(dentry->d_name.name, dname, dname_len)) 1182543212b3SYan, Zheng goto out_unlock; 1183543212b3SYan, Zheng 1184543212b3SYan, Zheng dir = d_inode(dentry->d_parent); 1185543212b3SYan, Zheng /* make sure parent matches dvino */ 1186543212b3SYan, Zheng if (!ceph_ino_compare(dir, pdvino)) 1187543212b3SYan, Zheng goto out_unlock; 1188543212b3SYan, Zheng 1189543212b3SYan, Zheng /* make sure dentry's inode matches target. NULL ptvino means that 1190543212b3SYan, Zheng * we expect a negative dentry */ 1191543212b3SYan, Zheng if (ptvino) { 1192543212b3SYan, Zheng if (d_really_is_negative(dentry)) 1193543212b3SYan, Zheng goto out_unlock; 1194543212b3SYan, Zheng if (!ceph_ino_compare(d_inode(dentry), ptvino)) 1195543212b3SYan, Zheng goto out_unlock; 1196543212b3SYan, Zheng } else { 1197543212b3SYan, Zheng if (d_really_is_positive(dentry)) 1198543212b3SYan, Zheng goto out_unlock; 1199543212b3SYan, Zheng } 1200543212b3SYan, Zheng 1201543212b3SYan, Zheng __update_dentry_lease(dir, dentry, lease, session, 1202543212b3SYan, Zheng from_time, &old_lease_session); 1203355da1ebSSage Weil out_unlock: 1204355da1ebSSage Weil spin_unlock(&dentry->d_lock); 1205481f001fSYan, Zheng ceph_put_mds_session(old_lease_session); 1206355da1ebSSage Weil } 1207355da1ebSSage Weil 1208355da1ebSSage Weil /* 12091cd3935bSSage Weil * splice a dentry to an inode. 1210810313c5Shongnanli * caller must hold directory i_rwsem for this to be safe. 12111cd3935bSSage Weil */ 12122bf996acSYan, Zheng static int splice_dentry(struct dentry **pdn, struct inode *in) 12131cd3935bSSage Weil { 12142bf996acSYan, Zheng struct dentry *dn = *pdn; 12151cd3935bSSage Weil struct dentry *realdn; 12161cd3935bSSage Weil 12172b0143b5SDavid Howells BUG_ON(d_inode(dn)); 12181cd3935bSSage Weil 12195495c2d0SYan, Zheng if (S_ISDIR(in->i_mode)) { 12205495c2d0SYan, Zheng /* If inode is directory, d_splice_alias() below will remove 12215495c2d0SYan, Zheng * 'realdn' from its origin parent. We need to ensure that 12225495c2d0SYan, Zheng * origin parent's readdir cache will not reference 'realdn' 12235495c2d0SYan, Zheng */ 12245495c2d0SYan, Zheng realdn = d_find_any_alias(in); 12255495c2d0SYan, Zheng if (realdn) { 12265495c2d0SYan, Zheng struct ceph_dentry_info *di = ceph_dentry(realdn); 12275495c2d0SYan, Zheng spin_lock(&realdn->d_lock); 12285495c2d0SYan, Zheng 12295495c2d0SYan, Zheng realdn->d_op->d_prune(realdn); 12305495c2d0SYan, Zheng 12315495c2d0SYan, Zheng di->time = jiffies; 12325495c2d0SYan, Zheng di->lease_shared_gen = 0; 12335495c2d0SYan, Zheng di->offset = 0; 12345495c2d0SYan, Zheng 12355495c2d0SYan, Zheng spin_unlock(&realdn->d_lock); 12365495c2d0SYan, Zheng dput(realdn); 12375495c2d0SYan, Zheng } 12385495c2d0SYan, Zheng } 12395495c2d0SYan, Zheng 12401cd3935bSSage Weil /* dn must be unhashed */ 12411cd3935bSSage Weil if (!d_unhashed(dn)) 12421cd3935bSSage Weil d_drop(dn); 124341d28bcaSAl Viro realdn = d_splice_alias(in, dn); 12441cd3935bSSage Weil if (IS_ERR(realdn)) { 1245d69ed05aSSage Weil pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n", 1246d69ed05aSSage Weil PTR_ERR(realdn), dn, in, ceph_vinop(in)); 12472bf996acSYan, Zheng return PTR_ERR(realdn); 12482bf996acSYan, Zheng } 12492bf996acSYan, Zheng 12502bf996acSYan, Zheng if (realdn) { 12511cd3935bSSage Weil dout("dn %p (%d) spliced with %p (%d) " 12521cd3935bSSage Weil "inode %p ino %llx.%llx\n", 125384d08fa8SAl Viro dn, d_count(dn), 125484d08fa8SAl Viro realdn, d_count(realdn), 12552b0143b5SDavid Howells d_inode(realdn), ceph_vinop(d_inode(realdn))); 12561cd3935bSSage Weil dput(dn); 12572bf996acSYan, Zheng *pdn = realdn; 12581cd3935bSSage Weil } else { 12591cd3935bSSage Weil BUG_ON(!ceph_dentry(dn)); 12601cd3935bSSage Weil dout("dn %p attached to %p ino %llx.%llx\n", 12612b0143b5SDavid Howells dn, d_inode(dn), ceph_vinop(d_inode(dn))); 12621cd3935bSSage Weil } 12632bf996acSYan, Zheng return 0; 12641cd3935bSSage Weil } 12651cd3935bSSage Weil 12661cd3935bSSage Weil /* 1267355da1ebSSage Weil * Incorporate results into the local cache. This is either just 1268355da1ebSSage Weil * one inode, or a directory, dentry, and possibly linked-to inode (e.g., 1269355da1ebSSage Weil * after a lookup). 1270355da1ebSSage Weil * 1271355da1ebSSage Weil * A reply may contain 1272355da1ebSSage Weil * a directory inode along with a dentry. 1273355da1ebSSage Weil * and/or a target inode 1274355da1ebSSage Weil * 1275355da1ebSSage Weil * Called with snap_rwsem (read). 1276355da1ebSSage Weil */ 1277f5a03b08SJeff Layton int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) 1278355da1ebSSage Weil { 1279f5a03b08SJeff Layton struct ceph_mds_session *session = req->r_session; 1280355da1ebSSage Weil struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1281355da1ebSSage Weil struct inode *in = NULL; 1282f5d55f03SJeff Layton struct ceph_vino tvino, dvino; 12833d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc = ceph_sb_to_client(sb); 1284355da1ebSSage Weil int err = 0; 1285355da1ebSSage Weil 1286355da1ebSSage Weil dout("fill_trace %p is_dentry %d is_target %d\n", req, 1287355da1ebSSage Weil rinfo->head->is_dentry, rinfo->head->is_target); 1288355da1ebSSage Weil 1289355da1ebSSage Weil if (!rinfo->head->is_target && !rinfo->head->is_dentry) { 1290355da1ebSSage Weil dout("fill_trace reply is empty!\n"); 12913dd69aabSJeff Layton if (rinfo->head->result == 0 && req->r_parent) 1292167c9e35SSage Weil ceph_invalidate_dir_request(req); 1293355da1ebSSage Weil return 0; 1294355da1ebSSage Weil } 1295355da1ebSSage Weil 1296355da1ebSSage Weil if (rinfo->head->is_dentry) { 12973dd69aabSJeff Layton struct inode *dir = req->r_parent; 12985b1daecdSSage Weil 12996c5e50faSSage Weil if (dir) { 1300966c7160SJeff Layton err = ceph_fill_inode(dir, NULL, &rinfo->diri, 1301966c7160SJeff Layton rinfo->dirfrag, session, -1, 13025b1daecdSSage Weil &req->r_caps_reservation); 13035b1daecdSSage Weil if (err < 0) 130419913b4eSYan, Zheng goto done; 13056c5e50faSSage Weil } else { 13066c5e50faSSage Weil WARN_ON_ONCE(1); 13076c5e50faSSage Weil } 130819913b4eSYan, Zheng 130974c9e6bfSYan, Zheng if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME && 131074c9e6bfSYan, Zheng test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 131174c9e6bfSYan, Zheng !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { 131219913b4eSYan, Zheng struct qstr dname; 131319913b4eSYan, Zheng struct dentry *dn, *parent; 131419913b4eSYan, Zheng 131519913b4eSYan, Zheng BUG_ON(!rinfo->head->is_target); 131619913b4eSYan, Zheng BUG_ON(req->r_dentry); 131719913b4eSYan, Zheng 131819913b4eSYan, Zheng parent = d_find_any_alias(dir); 131919913b4eSYan, Zheng BUG_ON(!parent); 132019913b4eSYan, Zheng 132119913b4eSYan, Zheng dname.name = rinfo->dname; 132219913b4eSYan, Zheng dname.len = rinfo->dname_len; 13238387ff25SLinus Torvalds dname.hash = full_name_hash(parent, dname.name, dname.len); 1324f5d55f03SJeff Layton tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); 1325f5d55f03SJeff Layton tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); 132619913b4eSYan, Zheng retry_lookup: 132719913b4eSYan, Zheng dn = d_lookup(parent, &dname); 132819913b4eSYan, Zheng dout("d_lookup on parent=%p name=%.*s got %p\n", 132919913b4eSYan, Zheng parent, dname.len, dname.name, dn); 133019913b4eSYan, Zheng 133119913b4eSYan, Zheng if (!dn) { 133219913b4eSYan, Zheng dn = d_alloc(parent, &dname); 133319913b4eSYan, Zheng dout("d_alloc %p '%.*s' = %p\n", parent, 133419913b4eSYan, Zheng dname.len, dname.name, dn); 1335d37b1d99SMarkus Elfring if (!dn) { 133619913b4eSYan, Zheng dput(parent); 133719913b4eSYan, Zheng err = -ENOMEM; 133819913b4eSYan, Zheng goto done; 133919913b4eSYan, Zheng } 1340ad5cb123SAl Viro err = 0; 13412b0143b5SDavid Howells } else if (d_really_is_positive(dn) && 1342f5d55f03SJeff Layton (ceph_ino(d_inode(dn)) != tvino.ino || 1343f5d55f03SJeff Layton ceph_snap(d_inode(dn)) != tvino.snap)) { 134419913b4eSYan, Zheng dout(" dn %p points to wrong inode %p\n", 13452b0143b5SDavid Howells dn, d_inode(dn)); 1346933ad2c9SYan, Zheng ceph_dir_clear_ordered(dir); 134719913b4eSYan, Zheng d_delete(dn); 134819913b4eSYan, Zheng dput(dn); 134919913b4eSYan, Zheng goto retry_lookup; 135019913b4eSYan, Zheng } 135119913b4eSYan, Zheng 135219913b4eSYan, Zheng req->r_dentry = dn; 135319913b4eSYan, Zheng dput(parent); 135419913b4eSYan, Zheng } 13555b1daecdSSage Weil } 13565b1daecdSSage Weil 135786b58d13SYan, Zheng if (rinfo->head->is_target) { 1358bca9fc14SJeff Layton /* Should be filled in by handle_reply */ 1359bca9fc14SJeff Layton BUG_ON(!req->r_target_inode); 136086b58d13SYan, Zheng 1361bca9fc14SJeff Layton in = req->r_target_inode; 1362966c7160SJeff Layton err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti, 1363966c7160SJeff Layton NULL, session, 1364bc2de10dSJeff Layton (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && 13653bb48b41SJeff Layton !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && 1366bc2de10dSJeff Layton rinfo->head->result == 0) ? req->r_fmode : -1, 136786b58d13SYan, Zheng &req->r_caps_reservation); 136886b58d13SYan, Zheng if (err < 0) { 1369966c7160SJeff Layton pr_err("ceph_fill_inode badness %p %llx.%llx\n", 137086b58d13SYan, Zheng in, ceph_vinop(in)); 1371bca9fc14SJeff Layton req->r_target_inode = NULL; 1372893e456bSJeff Layton if (in->i_state & I_NEW) 1373893e456bSJeff Layton discard_new_inode(in); 137468cbb805SJeff Layton else 137568cbb805SJeff Layton iput(in); 137686b58d13SYan, Zheng goto done; 137786b58d13SYan, Zheng } 1378893e456bSJeff Layton if (in->i_state & I_NEW) 1379893e456bSJeff Layton unlock_new_inode(in); 138086b58d13SYan, Zheng } 138186b58d13SYan, Zheng 13829358c6d4SSage Weil /* 13839358c6d4SSage Weil * ignore null lease/binding on snapdir ENOENT, or else we 13849358c6d4SSage Weil * will have trouble splicing in the virtual snapdir later 13859358c6d4SSage Weil */ 13863dd69aabSJeff Layton if (rinfo->head->is_dentry && 1387bc2de10dSJeff Layton !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && 13883dd69aabSJeff Layton test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 13899358c6d4SSage Weil (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name, 13903d14c5d2SYehuda Sadeh fsc->mount_options->snapdir_name, 13919358c6d4SSage Weil req->r_dentry->d_name.len))) { 1392355da1ebSSage Weil /* 1393355da1ebSSage Weil * lookup link rename : null -> possibly existing inode 1394355da1ebSSage Weil * mknod symlink mkdir : null -> new inode 1395355da1ebSSage Weil * unlink : linked -> null 1396355da1ebSSage Weil */ 13973dd69aabSJeff Layton struct inode *dir = req->r_parent; 1398355da1ebSSage Weil struct dentry *dn = req->r_dentry; 1399355da1ebSSage Weil bool have_dir_cap, have_lease; 1400355da1ebSSage Weil 1401355da1ebSSage Weil BUG_ON(!dn); 1402355da1ebSSage Weil BUG_ON(!dir); 14032b0143b5SDavid Howells BUG_ON(d_inode(dn->d_parent) != dir); 1404f5d55f03SJeff Layton 1405f5d55f03SJeff Layton dvino.ino = le64_to_cpu(rinfo->diri.in->ino); 1406f5d55f03SJeff Layton dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); 1407f5d55f03SJeff Layton 1408f5d55f03SJeff Layton BUG_ON(ceph_ino(dir) != dvino.ino); 1409f5d55f03SJeff Layton BUG_ON(ceph_snap(dir) != dvino.snap); 1410355da1ebSSage Weil 1411355da1ebSSage Weil /* do we have a lease on the whole dir? */ 1412355da1ebSSage Weil have_dir_cap = 1413355da1ebSSage Weil (le32_to_cpu(rinfo->diri.in->cap.caps) & 1414355da1ebSSage Weil CEPH_CAP_FILE_SHARED); 1415355da1ebSSage Weil 1416355da1ebSSage Weil /* do we have a dn lease? */ 1417355da1ebSSage Weil have_lease = have_dir_cap || 14182f90b852SSage Weil le32_to_cpu(rinfo->dlease->duration_ms); 1419355da1ebSSage Weil if (!have_lease) 1420355da1ebSSage Weil dout("fill_trace no dentry lease or dir cap\n"); 1421355da1ebSSage Weil 1422355da1ebSSage Weil /* rename? */ 1423355da1ebSSage Weil if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) { 14240a8a70f9SYan, Zheng struct inode *olddir = req->r_old_dentry_dir; 14250a8a70f9SYan, Zheng BUG_ON(!olddir); 14260a8a70f9SYan, Zheng 1427a455589fSAl Viro dout(" src %p '%pd' dst %p '%pd'\n", 1428355da1ebSSage Weil req->r_old_dentry, 1429a455589fSAl Viro req->r_old_dentry, 1430a455589fSAl Viro dn, dn); 1431355da1ebSSage Weil dout("fill_trace doing d_move %p -> %p\n", 1432355da1ebSSage Weil req->r_old_dentry, dn); 1433c10f5e12SSage Weil 1434fdd4e158SYan, Zheng /* d_move screws up sibling dentries' offsets */ 1435fdd4e158SYan, Zheng ceph_dir_clear_ordered(dir); 1436fdd4e158SYan, Zheng ceph_dir_clear_ordered(olddir); 1437fdd4e158SYan, Zheng 1438355da1ebSSage Weil d_move(req->r_old_dentry, dn); 1439a455589fSAl Viro dout(" src %p '%pd' dst %p '%pd'\n", 1440355da1ebSSage Weil req->r_old_dentry, 1441a455589fSAl Viro req->r_old_dentry, 1442a455589fSAl Viro dn, dn); 144381a6cf2dSSage Weil 1444c4a29f26SSage Weil /* ensure target dentry is invalidated, despite 1445c4a29f26SSage Weil rehashing bug in vfs_rename_dir */ 144681a6cf2dSSage Weil ceph_invalidate_dentry_lease(dn); 144781a6cf2dSSage Weil 144809adc80cSSage Weil dout("dn %p gets new offset %lld\n", req->r_old_dentry, 14491cd3935bSSage Weil ceph_dentry(req->r_old_dentry)->offset); 145081a6cf2dSSage Weil 14512bf996acSYan, Zheng /* swap r_dentry and r_old_dentry in case that 14522bf996acSYan, Zheng * splice_dentry() gets called later. This is safe 14532bf996acSYan, Zheng * because no other place will use them */ 14542bf996acSYan, Zheng req->r_dentry = req->r_old_dentry; 14552bf996acSYan, Zheng req->r_old_dentry = dn; 14562bf996acSYan, Zheng dn = req->r_dentry; 1457355da1ebSSage Weil } 1458355da1ebSSage Weil 1459355da1ebSSage Weil /* null dentry? */ 1460355da1ebSSage Weil if (!rinfo->head->is_target) { 1461355da1ebSSage Weil dout("fill_trace null dentry\n"); 14622b0143b5SDavid Howells if (d_really_is_positive(dn)) { 1463355da1ebSSage Weil dout("d_delete %p\n", dn); 14645495c2d0SYan, Zheng ceph_dir_clear_ordered(dir); 1465355da1ebSSage Weil d_delete(dn); 146680d025ffSJeff Layton } else if (have_lease) { 146780d025ffSJeff Layton if (d_unhashed(dn)) 1468f8b31710SAl Viro d_add(dn, NULL); 14697ffe4fceSXiubo Li } 14707ffe4fceSXiubo Li 14717ffe4fceSXiubo Li if (!d_unhashed(dn) && have_lease) 1472543212b3SYan, Zheng update_dentry_lease(dir, dn, 1473543212b3SYan, Zheng rinfo->dlease, session, 1474543212b3SYan, Zheng req->r_request_started); 1475355da1ebSSage Weil goto done; 1476355da1ebSSage Weil } 1477355da1ebSSage Weil 1478355da1ebSSage Weil /* attach proper inode */ 14792b0143b5SDavid Howells if (d_really_is_negative(dn)) { 148070db4f36SYan, Zheng ceph_dir_clear_ordered(dir); 148186b58d13SYan, Zheng ihold(in); 14822bf996acSYan, Zheng err = splice_dentry(&req->r_dentry, in); 14832bf996acSYan, Zheng if (err < 0) 1484355da1ebSSage Weil goto done; 14852bf996acSYan, Zheng dn = req->r_dentry; /* may have spliced */ 14862b0143b5SDavid Howells } else if (d_really_is_positive(dn) && d_inode(dn) != in) { 1487355da1ebSSage Weil dout(" %p links to %p %llx.%llx, not %llx.%llx\n", 14882b0143b5SDavid Howells dn, d_inode(dn), ceph_vinop(d_inode(dn)), 148986b58d13SYan, Zheng ceph_vinop(in)); 1490200fd27cSYan, Zheng d_invalidate(dn); 1491355da1ebSSage Weil have_lease = false; 1492355da1ebSSage Weil } 1493355da1ebSSage Weil 1494f5d55f03SJeff Layton if (have_lease) { 1495543212b3SYan, Zheng update_dentry_lease(dir, dn, 1496543212b3SYan, Zheng rinfo->dlease, session, 1497543212b3SYan, Zheng req->r_request_started); 1498f5d55f03SJeff Layton } 1499355da1ebSSage Weil dout(" final dn %p\n", dn); 1500bc2de10dSJeff Layton } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP || 1501bc2de10dSJeff Layton req->r_op == CEPH_MDS_OP_MKSNAP) && 15021f08529cSAl Viro test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1503bc2de10dSJeff Layton !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { 15043dd69aabSJeff Layton struct inode *dir = req->r_parent; 1505355da1ebSSage Weil 1506355da1ebSSage Weil /* fill out a snapdir LOOKUPSNAP dentry */ 15070a8a70f9SYan, Zheng BUG_ON(!dir); 15080a8a70f9SYan, Zheng BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR); 15092bf996acSYan, Zheng BUG_ON(!req->r_dentry); 15102bf996acSYan, Zheng dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry); 151170db4f36SYan, Zheng ceph_dir_clear_ordered(dir); 151286b58d13SYan, Zheng ihold(in); 15132bf996acSYan, Zheng err = splice_dentry(&req->r_dentry, in); 15142bf996acSYan, Zheng if (err < 0) 1515355da1ebSSage Weil goto done; 1516543212b3SYan, Zheng } else if (rinfo->head->is_dentry && req->r_dentry) { 1517543212b3SYan, Zheng /* parent inode is not locked, be carefull */ 1518cdde7c43SJeff Layton struct ceph_vino *ptvino = NULL; 1519cdde7c43SJeff Layton dvino.ino = le64_to_cpu(rinfo->diri.in->ino); 1520cdde7c43SJeff Layton dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); 1521cdde7c43SJeff Layton if (rinfo->head->is_target) { 1522cdde7c43SJeff Layton tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); 1523cdde7c43SJeff Layton tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); 1524cdde7c43SJeff Layton ptvino = &tvino; 1525cdde7c43SJeff Layton } 1526543212b3SYan, Zheng update_dentry_lease_careful(req->r_dentry, rinfo->dlease, 1527543212b3SYan, Zheng session, req->r_request_started, 1528543212b3SYan, Zheng rinfo->dname, rinfo->dname_len, 1529543212b3SYan, Zheng &dvino, ptvino); 1530355da1ebSSage Weil } 1531355da1ebSSage Weil done: 1532355da1ebSSage Weil dout("fill_trace done err=%d\n", err); 1533355da1ebSSage Weil return err; 1534355da1ebSSage Weil } 1535355da1ebSSage Weil 1536355da1ebSSage Weil /* 1537355da1ebSSage Weil * Prepopulate our cache with readdir results, leases, etc. 1538355da1ebSSage Weil */ 153979f9f99aSSage Weil static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req, 154079f9f99aSSage Weil struct ceph_mds_session *session) 154179f9f99aSSage Weil { 154279f9f99aSSage Weil struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 154379f9f99aSSage Weil int i, err = 0; 154479f9f99aSSage Weil 154579f9f99aSSage Weil for (i = 0; i < rinfo->dir_nr; i++) { 15462a5beea3SYan, Zheng struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; 154779f9f99aSSage Weil struct ceph_vino vino; 154879f9f99aSSage Weil struct inode *in; 154979f9f99aSSage Weil int rc; 155079f9f99aSSage Weil 15512a5beea3SYan, Zheng vino.ino = le64_to_cpu(rde->inode.in->ino); 15522a5beea3SYan, Zheng vino.snap = le64_to_cpu(rde->inode.in->snapid); 155379f9f99aSSage Weil 155479f9f99aSSage Weil in = ceph_get_inode(req->r_dentry->d_sb, vino); 155579f9f99aSSage Weil if (IS_ERR(in)) { 155679f9f99aSSage Weil err = PTR_ERR(in); 155779f9f99aSSage Weil dout("new_inode badness got %d\n", err); 155879f9f99aSSage Weil continue; 155979f9f99aSSage Weil } 1560966c7160SJeff Layton rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, 156157c21994SJeff Layton -1, &req->r_caps_reservation); 156279f9f99aSSage Weil if (rc < 0) { 1563966c7160SJeff Layton pr_err("ceph_fill_inode badness on %p got %d\n", 1564966c7160SJeff Layton in, rc); 156579f9f99aSSage Weil err = rc; 1566893e456bSJeff Layton if (in->i_state & I_NEW) { 1567893e456bSJeff Layton ihold(in); 1568893e456bSJeff Layton discard_new_inode(in); 156979f9f99aSSage Weil } 1570893e456bSJeff Layton } else if (in->i_state & I_NEW) { 1571893e456bSJeff Layton unlock_new_inode(in); 1572893e456bSJeff Layton } 1573893e456bSJeff Layton 157423c2c76eSJeff Layton iput(in); 157579f9f99aSSage Weil } 157679f9f99aSSage Weil 157779f9f99aSSage Weil return err; 157879f9f99aSSage Weil } 157979f9f99aSSage Weil 1580fdd4e158SYan, Zheng void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl) 1581fdd4e158SYan, Zheng { 1582fdd4e158SYan, Zheng if (ctl->page) { 1583fdd4e158SYan, Zheng kunmap(ctl->page); 158409cbfeafSKirill A. Shutemov put_page(ctl->page); 1585fdd4e158SYan, Zheng ctl->page = NULL; 1586fdd4e158SYan, Zheng } 1587fdd4e158SYan, Zheng } 1588fdd4e158SYan, Zheng 1589fdd4e158SYan, Zheng static int fill_readdir_cache(struct inode *dir, struct dentry *dn, 1590fdd4e158SYan, Zheng struct ceph_readdir_cache_control *ctl, 1591fdd4e158SYan, Zheng struct ceph_mds_request *req) 1592fdd4e158SYan, Zheng { 1593fdd4e158SYan, Zheng struct ceph_inode_info *ci = ceph_inode(dir); 159409cbfeafSKirill A. Shutemov unsigned nsize = PAGE_SIZE / sizeof(struct dentry*); 1595fdd4e158SYan, Zheng unsigned idx = ctl->index % nsize; 1596fdd4e158SYan, Zheng pgoff_t pgoff = ctl->index / nsize; 1597fdd4e158SYan, Zheng 1598fdd4e158SYan, Zheng if (!ctl->page || pgoff != page_index(ctl->page)) { 1599fdd4e158SYan, Zheng ceph_readdir_cache_release(ctl); 1600af5e5eb5SYan, Zheng if (idx == 0) 1601fdd4e158SYan, Zheng ctl->page = grab_cache_page(&dir->i_data, pgoff); 1602af5e5eb5SYan, Zheng else 1603af5e5eb5SYan, Zheng ctl->page = find_lock_page(&dir->i_data, pgoff); 1604fdd4e158SYan, Zheng if (!ctl->page) { 1605fdd4e158SYan, Zheng ctl->index = -1; 1606af5e5eb5SYan, Zheng return idx == 0 ? -ENOMEM : 0; 1607fdd4e158SYan, Zheng } 1608fdd4e158SYan, Zheng /* reading/filling the cache are serialized by 1609810313c5Shongnanli * i_rwsem, no need to use page lock */ 1610fdd4e158SYan, Zheng unlock_page(ctl->page); 1611fdd4e158SYan, Zheng ctl->dentries = kmap(ctl->page); 1612af5e5eb5SYan, Zheng if (idx == 0) 161309cbfeafSKirill A. Shutemov memset(ctl->dentries, 0, PAGE_SIZE); 1614fdd4e158SYan, Zheng } 1615fdd4e158SYan, Zheng 1616fdd4e158SYan, Zheng if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) && 1617fdd4e158SYan, Zheng req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) { 1618fdd4e158SYan, Zheng dout("readdir cache dn %p idx %d\n", dn, ctl->index); 1619fdd4e158SYan, Zheng ctl->dentries[idx] = dn; 1620fdd4e158SYan, Zheng ctl->index++; 1621fdd4e158SYan, Zheng } else { 1622fdd4e158SYan, Zheng dout("disable readdir cache\n"); 1623fdd4e158SYan, Zheng ctl->index = -1; 1624fdd4e158SYan, Zheng } 1625fdd4e158SYan, Zheng return 0; 1626fdd4e158SYan, Zheng } 1627fdd4e158SYan, Zheng 1628355da1ebSSage Weil int ceph_readdir_prepopulate(struct ceph_mds_request *req, 1629355da1ebSSage Weil struct ceph_mds_session *session) 1630355da1ebSSage Weil { 1631355da1ebSSage Weil struct dentry *parent = req->r_dentry; 1632f3c4ebe6SYan, Zheng struct ceph_inode_info *ci = ceph_inode(d_inode(parent)); 1633355da1ebSSage Weil struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1634355da1ebSSage Weil struct qstr dname; 1635355da1ebSSage Weil struct dentry *dn; 1636355da1ebSSage Weil struct inode *in; 1637315f2408SYan, Zheng int err = 0, skipped = 0, ret, i; 16380f51a983SJeff Layton u32 frag = le32_to_cpu(req->r_args.readdir.frag); 1639f3c4ebe6SYan, Zheng u32 last_hash = 0; 1640f3c4ebe6SYan, Zheng u32 fpos_offset; 1641fdd4e158SYan, Zheng struct ceph_readdir_cache_control cache_ctl = {}; 1642fdd4e158SYan, Zheng 1643bc2de10dSJeff Layton if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) 1644fdd4e158SYan, Zheng return readdir_prepopulate_inodes_only(req, session); 164581c6aea5SYan, Zheng 164679162547SYan, Zheng if (rinfo->hash_order) { 164779162547SYan, Zheng if (req->r_path2) { 1648f3c4ebe6SYan, Zheng last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, 164979162547SYan, Zheng req->r_path2, 165079162547SYan, Zheng strlen(req->r_path2)); 1651f3c4ebe6SYan, Zheng last_hash = ceph_frag_value(last_hash); 165279162547SYan, Zheng } else if (rinfo->offset_hash) { 165379162547SYan, Zheng /* mds understands offset_hash */ 165479162547SYan, Zheng WARN_ON_ONCE(req->r_readdir_offset != 2); 16550f51a983SJeff Layton last_hash = le32_to_cpu(req->r_args.readdir.offset_hash); 165679162547SYan, Zheng } 1657f3c4ebe6SYan, Zheng } 1658f3c4ebe6SYan, Zheng 165981c6aea5SYan, Zheng if (rinfo->dir_dir && 166081c6aea5SYan, Zheng le32_to_cpu(rinfo->dir_dir->frag) != frag) { 166181c6aea5SYan, Zheng dout("readdir_prepopulate got new frag %x -> %x\n", 166281c6aea5SYan, Zheng frag, le32_to_cpu(rinfo->dir_dir->frag)); 166381c6aea5SYan, Zheng frag = le32_to_cpu(rinfo->dir_dir->frag); 1664f3c4ebe6SYan, Zheng if (!rinfo->hash_order) 1665fdd4e158SYan, Zheng req->r_readdir_offset = 2; 166681c6aea5SYan, Zheng } 1667355da1ebSSage Weil 1668355da1ebSSage Weil if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) { 1669355da1ebSSage Weil dout("readdir_prepopulate %d items under SNAPDIR dn %p\n", 1670355da1ebSSage Weil rinfo->dir_nr, parent); 1671355da1ebSSage Weil } else { 1672355da1ebSSage Weil dout("readdir_prepopulate %d items under dn %p\n", 1673355da1ebSSage Weil rinfo->dir_nr, parent); 1674355da1ebSSage Weil if (rinfo->dir_dir) 16752b0143b5SDavid Howells ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir); 1676355da1ebSSage Weil 16778d45b911SYan, Zheng if (ceph_frag_is_leftmost(frag) && 16788d45b911SYan, Zheng req->r_readdir_offset == 2 && 167979162547SYan, Zheng !(rinfo->hash_order && last_hash)) { 16808d45b911SYan, Zheng /* note dir version at start of readdir so we can 16818d45b911SYan, Zheng * tell if any dentries get dropped */ 16828d45b911SYan, Zheng req->r_dir_release_cnt = 16838d45b911SYan, Zheng atomic64_read(&ci->i_release_count); 16848d45b911SYan, Zheng req->r_dir_ordered_cnt = 16858d45b911SYan, Zheng atomic64_read(&ci->i_ordered_count); 1686fdd4e158SYan, Zheng req->r_readdir_cache_idx = 0; 1687fdd4e158SYan, Zheng } 16888d45b911SYan, Zheng } 1689fdd4e158SYan, Zheng 1690fdd4e158SYan, Zheng cache_ctl.index = req->r_readdir_cache_idx; 1691f3c4ebe6SYan, Zheng fpos_offset = req->r_readdir_offset; 1692fdd4e158SYan, Zheng 169386b58d13SYan, Zheng /* FIXME: release caps/leases if error occurs */ 1694355da1ebSSage Weil for (i = 0; i < rinfo->dir_nr; i++) { 16952a5beea3SYan, Zheng struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; 1696543212b3SYan, Zheng struct ceph_vino tvino; 1697355da1ebSSage Weil 16982a5beea3SYan, Zheng dname.name = rde->name; 16992a5beea3SYan, Zheng dname.len = rde->name_len; 17008387ff25SLinus Torvalds dname.hash = full_name_hash(parent, dname.name, dname.len); 1701355da1ebSSage Weil 1702f5d55f03SJeff Layton tvino.ino = le64_to_cpu(rde->inode.in->ino); 1703f5d55f03SJeff Layton tvino.snap = le64_to_cpu(rde->inode.in->snapid); 1704355da1ebSSage Weil 1705f3c4ebe6SYan, Zheng if (rinfo->hash_order) { 1706f3c4ebe6SYan, Zheng u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, 1707f3c4ebe6SYan, Zheng rde->name, rde->name_len); 1708f3c4ebe6SYan, Zheng hash = ceph_frag_value(hash); 1709f3c4ebe6SYan, Zheng if (hash != last_hash) 1710f3c4ebe6SYan, Zheng fpos_offset = 2; 1711f3c4ebe6SYan, Zheng last_hash = hash; 1712f3c4ebe6SYan, Zheng rde->offset = ceph_make_fpos(hash, fpos_offset++, true); 1713f3c4ebe6SYan, Zheng } else { 1714f3c4ebe6SYan, Zheng rde->offset = ceph_make_fpos(frag, fpos_offset++, false); 1715f3c4ebe6SYan, Zheng } 1716355da1ebSSage Weil 1717355da1ebSSage Weil retry_lookup: 1718355da1ebSSage Weil dn = d_lookup(parent, &dname); 1719355da1ebSSage Weil dout("d_lookup on parent=%p name=%.*s got %p\n", 1720355da1ebSSage Weil parent, dname.len, dname.name, dn); 1721355da1ebSSage Weil 1722355da1ebSSage Weil if (!dn) { 1723355da1ebSSage Weil dn = d_alloc(parent, &dname); 1724355da1ebSSage Weil dout("d_alloc %p '%.*s' = %p\n", parent, 1725355da1ebSSage Weil dname.len, dname.name, dn); 1726d37b1d99SMarkus Elfring if (!dn) { 1727355da1ebSSage Weil dout("d_alloc badness\n"); 1728355da1ebSSage Weil err = -ENOMEM; 1729355da1ebSSage Weil goto out; 1730355da1ebSSage Weil } 17312b0143b5SDavid Howells } else if (d_really_is_positive(dn) && 1732f5d55f03SJeff Layton (ceph_ino(d_inode(dn)) != tvino.ino || 1733f5d55f03SJeff Layton ceph_snap(d_inode(dn)) != tvino.snap)) { 17345495c2d0SYan, Zheng struct ceph_dentry_info *di = ceph_dentry(dn); 1735355da1ebSSage Weil dout(" dn %p points to wrong inode %p\n", 17362b0143b5SDavid Howells dn, d_inode(dn)); 17375495c2d0SYan, Zheng 17385495c2d0SYan, Zheng spin_lock(&dn->d_lock); 17395495c2d0SYan, Zheng if (di->offset > 0 && 17405495c2d0SYan, Zheng di->lease_shared_gen == 17415495c2d0SYan, Zheng atomic_read(&ci->i_shared_gen)) { 1742933ad2c9SYan, Zheng __ceph_dir_clear_ordered(ci); 17435495c2d0SYan, Zheng di->offset = 0; 17445495c2d0SYan, Zheng } 17455495c2d0SYan, Zheng spin_unlock(&dn->d_lock); 17465495c2d0SYan, Zheng 1747355da1ebSSage Weil d_delete(dn); 1748355da1ebSSage Weil dput(dn); 1749355da1ebSSage Weil goto retry_lookup; 1750355da1ebSSage Weil } 1751355da1ebSSage Weil 1752355da1ebSSage Weil /* inode */ 17532b0143b5SDavid Howells if (d_really_is_positive(dn)) { 17542b0143b5SDavid Howells in = d_inode(dn); 1755355da1ebSSage Weil } else { 1756f5d55f03SJeff Layton in = ceph_get_inode(parent->d_sb, tvino); 1757ac1f12efSDan Carpenter if (IS_ERR(in)) { 1758355da1ebSSage Weil dout("new_inode badness\n"); 17592744c171SAl Viro d_drop(dn); 1760355da1ebSSage Weil dput(dn); 1761ac1f12efSDan Carpenter err = PTR_ERR(in); 1762355da1ebSSage Weil goto out; 1763355da1ebSSage Weil } 1764355da1ebSSage Weil } 1765355da1ebSSage Weil 1766966c7160SJeff Layton ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, 176757c21994SJeff Layton -1, &req->r_caps_reservation); 1768fdd4e158SYan, Zheng if (ret < 0) { 1769966c7160SJeff Layton pr_err("ceph_fill_inode badness on %p\n", in); 17703e1d0452SYan, Zheng if (d_really_is_negative(dn)) { 1771893e456bSJeff Layton if (in->i_state & I_NEW) { 1772893e456bSJeff Layton ihold(in); 1773893e456bSJeff Layton discard_new_inode(in); 1774893e456bSJeff Layton } 177523c2c76eSJeff Layton iput(in); 17763e1d0452SYan, Zheng } 177786b58d13SYan, Zheng d_drop(dn); 1778fdd4e158SYan, Zheng err = ret; 1779d69ed05aSSage Weil goto next_item; 1780355da1ebSSage Weil } 1781893e456bSJeff Layton if (in->i_state & I_NEW) 1782893e456bSJeff Layton unlock_new_inode(in); 178386b58d13SYan, Zheng 17842b0143b5SDavid Howells if (d_really_is_negative(dn)) { 1785315f2408SYan, Zheng if (ceph_security_xattr_deadlock(in)) { 1786315f2408SYan, Zheng dout(" skip splicing dn %p to inode %p" 1787315f2408SYan, Zheng " (security xattr deadlock)\n", dn, in); 178823c2c76eSJeff Layton iput(in); 1789315f2408SYan, Zheng skipped++; 1790315f2408SYan, Zheng goto next_item; 1791315f2408SYan, Zheng } 1792315f2408SYan, Zheng 17932bf996acSYan, Zheng err = splice_dentry(&dn, in); 17942bf996acSYan, Zheng if (err < 0) 179586b58d13SYan, Zheng goto next_item; 179686b58d13SYan, Zheng } 179786b58d13SYan, Zheng 1798f3c4ebe6SYan, Zheng ceph_dentry(dn)->offset = rde->offset; 179986b58d13SYan, Zheng 1800543212b3SYan, Zheng update_dentry_lease(d_inode(parent), dn, 1801543212b3SYan, Zheng rde->lease, req->r_session, 1802543212b3SYan, Zheng req->r_request_started); 1803fdd4e158SYan, Zheng 1804315f2408SYan, Zheng if (err == 0 && skipped == 0 && cache_ctl.index >= 0) { 1805fdd4e158SYan, Zheng ret = fill_readdir_cache(d_inode(parent), dn, 1806fdd4e158SYan, Zheng &cache_ctl, req); 1807fdd4e158SYan, Zheng if (ret < 0) 1808fdd4e158SYan, Zheng err = ret; 1809fdd4e158SYan, Zheng } 1810d69ed05aSSage Weil next_item: 1811355da1ebSSage Weil dput(dn); 1812355da1ebSSage Weil } 1813355da1ebSSage Weil out: 1814315f2408SYan, Zheng if (err == 0 && skipped == 0) { 1815bc2de10dSJeff Layton set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags); 1816fdd4e158SYan, Zheng req->r_readdir_cache_idx = cache_ctl.index; 1817fdd4e158SYan, Zheng } 1818fdd4e158SYan, Zheng ceph_readdir_cache_release(&cache_ctl); 1819355da1ebSSage Weil dout("readdir_prepopulate done\n"); 1820355da1ebSSage Weil return err; 1821355da1ebSSage Weil } 1822355da1ebSSage Weil 1823efb0ca76SYan, Zheng bool ceph_inode_set_size(struct inode *inode, loff_t size) 1824355da1ebSSage Weil { 1825355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1826efb0ca76SYan, Zheng bool ret; 1827355da1ebSSage Weil 1828be655596SSage Weil spin_lock(&ci->i_ceph_lock); 18292d6795fbSJeff Layton dout("set_size %p %llu -> %llu\n", inode, i_size_read(inode), size); 183099c88e69SYan, Zheng i_size_write(inode, size); 1831400e1286SJeff Layton ceph_fscache_update(inode); 1832224a7542SYan, Zheng inode->i_blocks = calc_inode_blocks(size); 1833355da1ebSSage Weil 1834efb0ca76SYan, Zheng ret = __ceph_should_report_size(ci); 1835355da1ebSSage Weil 1836be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1837400e1286SJeff Layton 1838355da1ebSSage Weil return ret; 1839355da1ebSSage Weil } 1840355da1ebSSage Weil 184164f28c62SJeff Layton void ceph_queue_inode_work(struct inode *inode, int work_bit) 18423c6f6b79SSage Weil { 184364f28c62SJeff Layton struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 18441cf89a8dSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 184564f28c62SJeff Layton set_bit(work_bit, &ci->i_work_mask); 18461cf89a8dSYan, Zheng 184715a2015fSSage Weil ihold(inode); 184864f28c62SJeff Layton if (queue_work(fsc->inode_wq, &ci->i_work)) { 184964f28c62SJeff Layton dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask); 18503c6f6b79SSage Weil } else { 185164f28c62SJeff Layton dout("queue_inode_work %p already queued, mask=%lx\n", 18521cf89a8dSYan, Zheng inode, ci->i_work_mask); 18531cf89a8dSYan, Zheng iput(inode); 18541cf89a8dSYan, Zheng } 18551cf89a8dSYan, Zheng } 18561cf89a8dSYan, Zheng 18571cf89a8dSYan, Zheng static void ceph_do_invalidate_pages(struct inode *inode) 18581cf89a8dSYan, Zheng { 18591cf89a8dSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode); 1860355da1ebSSage Weil u32 orig_gen; 1861355da1ebSSage Weil int check = 0; 1862355da1ebSSage Weil 1863400e1286SJeff Layton ceph_fscache_invalidate(inode, false); 1864400e1286SJeff Layton 1865b0d7c223SYan, Zheng mutex_lock(&ci->i_truncate_mutex); 18666c93df5dSYan, Zheng 18675d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode)) { 18686407fbb9SJeff Layton pr_warn_ratelimited("%s: inode %llx.%llx is shut down\n", 18696407fbb9SJeff Layton __func__, ceph_vinop(inode)); 18706c93df5dSYan, Zheng mapping_set_error(inode->i_mapping, -EIO); 18716c93df5dSYan, Zheng truncate_pagecache(inode, 0); 18726c93df5dSYan, Zheng mutex_unlock(&ci->i_truncate_mutex); 18736c93df5dSYan, Zheng goto out; 18746c93df5dSYan, Zheng } 18756c93df5dSYan, Zheng 1876be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1877355da1ebSSage Weil dout("invalidate_pages %p gen %d revoking %d\n", inode, 1878355da1ebSSage Weil ci->i_rdcache_gen, ci->i_rdcache_revoking); 1879cd045cb4SSage Weil if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { 18809563f88cSYan, Zheng if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) 18819563f88cSYan, Zheng check = 1; 1882be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1883b0d7c223SYan, Zheng mutex_unlock(&ci->i_truncate_mutex); 1884355da1ebSSage Weil goto out; 1885355da1ebSSage Weil } 1886355da1ebSSage Weil orig_gen = ci->i_rdcache_gen; 1887be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1888355da1ebSSage Weil 18899abd4db7SYan, Zheng if (invalidate_inode_pages2(inode->i_mapping) < 0) { 18906407fbb9SJeff Layton pr_err("invalidate_inode_pages2 %llx.%llx failed\n", 18916407fbb9SJeff Layton ceph_vinop(inode)); 18929abd4db7SYan, Zheng } 1893355da1ebSSage Weil 1894be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1895cd045cb4SSage Weil if (orig_gen == ci->i_rdcache_gen && 1896cd045cb4SSage Weil orig_gen == ci->i_rdcache_revoking) { 1897355da1ebSSage Weil dout("invalidate_pages %p gen %d successful\n", inode, 1898355da1ebSSage Weil ci->i_rdcache_gen); 1899cd045cb4SSage Weil ci->i_rdcache_revoking--; 1900355da1ebSSage Weil check = 1; 1901355da1ebSSage Weil } else { 1902cd045cb4SSage Weil dout("invalidate_pages %p gen %d raced, now %d revoking %d\n", 1903cd045cb4SSage Weil inode, orig_gen, ci->i_rdcache_gen, 1904cd045cb4SSage Weil ci->i_rdcache_revoking); 19059563f88cSYan, Zheng if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) 19069563f88cSYan, Zheng check = 1; 1907355da1ebSSage Weil } 1908be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1909b0d7c223SYan, Zheng mutex_unlock(&ci->i_truncate_mutex); 19109563f88cSYan, Zheng out: 1911355da1ebSSage Weil if (check) 1912e4b731ccSXiubo Li ceph_check_caps(ci, 0); 19133c6f6b79SSage Weil } 19143c6f6b79SSage Weil 19153c6f6b79SSage Weil /* 1916355da1ebSSage Weil * Make sure any pending truncation is applied before doing anything 1917355da1ebSSage Weil * that may depend on it. 1918355da1ebSSage Weil */ 1919b415bf4fSYan, Zheng void __ceph_do_pending_vmtruncate(struct inode *inode) 1920355da1ebSSage Weil { 1921355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1922355da1ebSSage Weil u64 to; 1923a85f50b6SYan, Zheng int wrbuffer_refs, finish = 0; 1924355da1ebSSage Weil 1925b0d7c223SYan, Zheng mutex_lock(&ci->i_truncate_mutex); 1926355da1ebSSage Weil retry: 1927be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1928355da1ebSSage Weil if (ci->i_truncate_pending == 0) { 1929355da1ebSSage Weil dout("__do_pending_vmtruncate %p none pending\n", inode); 1930be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1931b0d7c223SYan, Zheng mutex_unlock(&ci->i_truncate_mutex); 1932355da1ebSSage Weil return; 1933355da1ebSSage Weil } 1934355da1ebSSage Weil 1935355da1ebSSage Weil /* 1936355da1ebSSage Weil * make sure any dirty snapped pages are flushed before we 1937355da1ebSSage Weil * possibly truncate them.. so write AND block! 1938355da1ebSSage Weil */ 1939355da1ebSSage Weil if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) { 1940c8fd0d37SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1941355da1ebSSage Weil dout("__do_pending_vmtruncate %p flushing snaps first\n", 1942355da1ebSSage Weil inode); 1943355da1ebSSage Weil filemap_write_and_wait_range(&inode->i_data, 0, 1944355da1ebSSage Weil inode->i_sb->s_maxbytes); 1945355da1ebSSage Weil goto retry; 1946355da1ebSSage Weil } 1947355da1ebSSage Weil 1948b0d7c223SYan, Zheng /* there should be no reader or writer */ 1949b0d7c223SYan, Zheng WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref); 1950b0d7c223SYan, Zheng 1951355da1ebSSage Weil to = ci->i_truncate_size; 1952355da1ebSSage Weil wrbuffer_refs = ci->i_wrbuffer_ref; 1953355da1ebSSage Weil dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode, 1954355da1ebSSage Weil ci->i_truncate_pending, to); 1955be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1956355da1ebSSage Weil 1957400e1286SJeff Layton ceph_fscache_resize(inode, to); 19584e217b5dSYan, Zheng truncate_pagecache(inode, to); 1959355da1ebSSage Weil 1960be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1961a85f50b6SYan, Zheng if (to == ci->i_truncate_size) { 1962a85f50b6SYan, Zheng ci->i_truncate_pending = 0; 1963a85f50b6SYan, Zheng finish = 1; 1964a85f50b6SYan, Zheng } 1965be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1966a85f50b6SYan, Zheng if (!finish) 1967a85f50b6SYan, Zheng goto retry; 1968355da1ebSSage Weil 1969b0d7c223SYan, Zheng mutex_unlock(&ci->i_truncate_mutex); 1970b0d7c223SYan, Zheng 1971355da1ebSSage Weil if (wrbuffer_refs == 0) 1972e4b731ccSXiubo Li ceph_check_caps(ci, 0); 1973a85f50b6SYan, Zheng 197403066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq); 1975355da1ebSSage Weil } 1976355da1ebSSage Weil 19771cf89a8dSYan, Zheng static void ceph_inode_work(struct work_struct *work) 19781cf89a8dSYan, Zheng { 19791cf89a8dSYan, Zheng struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, 19801cf89a8dSYan, Zheng i_work); 1981874c8ca1SDavid Howells struct inode *inode = &ci->netfs.inode; 19821cf89a8dSYan, Zheng 19831cf89a8dSYan, Zheng if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) { 19841cf89a8dSYan, Zheng dout("writeback %p\n", inode); 19851cf89a8dSYan, Zheng filemap_fdatawrite(&inode->i_data); 19861cf89a8dSYan, Zheng } 19871cf89a8dSYan, Zheng if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask)) 19881cf89a8dSYan, Zheng ceph_do_invalidate_pages(inode); 19891cf89a8dSYan, Zheng 19901cf89a8dSYan, Zheng if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask)) 19911cf89a8dSYan, Zheng __ceph_do_pending_vmtruncate(inode); 19921cf89a8dSYan, Zheng 1993a8810cdcSJeff Layton if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask)) 1994e4b731ccSXiubo Li ceph_check_caps(ci, 0); 1995a8810cdcSJeff Layton 1996a8810cdcSJeff Layton if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask)) 1997a8810cdcSJeff Layton ceph_flush_snaps(ci, NULL); 1998a8810cdcSJeff Layton 19991cf89a8dSYan, Zheng iput(inode); 20001cf89a8dSYan, Zheng } 20011cf89a8dSYan, Zheng 2002355da1ebSSage Weil /* 2003355da1ebSSage Weil * symlinks 2004355da1ebSSage Weil */ 2005355da1ebSSage Weil static const struct inode_operations ceph_symlink_iops = { 20066b255391SAl Viro .get_link = simple_get_link, 20070b932672SYan, Zheng .setattr = ceph_setattr, 20080b932672SYan, Zheng .getattr = ceph_getattr, 20090b932672SYan, Zheng .listxattr = ceph_listxattr, 2010355da1ebSSage Weil }; 2011355da1ebSSage Weil 2012a26feccaSAndreas Gruenbacher int __ceph_setattr(struct inode *inode, struct iattr *attr) 2013355da1ebSSage Weil { 2014355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2015c62498d7SJeff Layton unsigned int ia_valid = attr->ia_valid; 2016355da1ebSSage Weil struct ceph_mds_request *req; 2017a26feccaSAndreas Gruenbacher struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 2018f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf; 2019355da1ebSSage Weil int issued; 2020355da1ebSSage Weil int release = 0, dirtied = 0; 2021355da1ebSSage Weil int mask = 0; 2022355da1ebSSage Weil int err = 0; 2023fca65b4aSSage Weil int inode_dirty_flags = 0; 2024604d1b02SYan, Zheng bool lock_snap_rwsem = false; 2025355da1ebSSage Weil 2026f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush(); 2027f66fd9f0SYan, Zheng if (!prealloc_cf) 2028f66fd9f0SYan, Zheng return -ENOMEM; 2029f66fd9f0SYan, Zheng 2030355da1ebSSage Weil req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR, 2031355da1ebSSage Weil USE_AUTH_MDS); 2032f66fd9f0SYan, Zheng if (IS_ERR(req)) { 2033f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 2034355da1ebSSage Weil return PTR_ERR(req); 2035f66fd9f0SYan, Zheng } 2036355da1ebSSage Weil 2037be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2038355da1ebSSage Weil issued = __ceph_caps_issued(ci, NULL); 2039604d1b02SYan, Zheng 2040604d1b02SYan, Zheng if (!ci->i_head_snapc && 2041604d1b02SYan, Zheng (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) { 2042604d1b02SYan, Zheng lock_snap_rwsem = true; 2043604d1b02SYan, Zheng if (!down_read_trylock(&mdsc->snap_rwsem)) { 2044604d1b02SYan, Zheng spin_unlock(&ci->i_ceph_lock); 2045604d1b02SYan, Zheng down_read(&mdsc->snap_rwsem); 2046604d1b02SYan, Zheng spin_lock(&ci->i_ceph_lock); 2047604d1b02SYan, Zheng issued = __ceph_caps_issued(ci, NULL); 2048604d1b02SYan, Zheng } 2049604d1b02SYan, Zheng } 2050604d1b02SYan, Zheng 2051355da1ebSSage Weil dout("setattr %p issued %s\n", inode, ceph_cap_string(issued)); 2052355da1ebSSage Weil 2053355da1ebSSage Weil if (ia_valid & ATTR_UID) { 2054355da1ebSSage Weil dout("setattr %p uid %d -> %d\n", inode, 2055bd2bae6aSEric W. Biederman from_kuid(&init_user_ns, inode->i_uid), 2056bd2bae6aSEric W. Biederman from_kuid(&init_user_ns, attr->ia_uid)); 2057355da1ebSSage Weil if (issued & CEPH_CAP_AUTH_EXCL) { 2058355da1ebSSage Weil inode->i_uid = attr->ia_uid; 2059355da1ebSSage Weil dirtied |= CEPH_CAP_AUTH_EXCL; 2060355da1ebSSage Weil } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2061ab871b90SEric W. Biederman !uid_eq(attr->ia_uid, inode->i_uid)) { 2062ab871b90SEric W. Biederman req->r_args.setattr.uid = cpu_to_le32( 2063ab871b90SEric W. Biederman from_kuid(&init_user_ns, attr->ia_uid)); 2064355da1ebSSage Weil mask |= CEPH_SETATTR_UID; 2065355da1ebSSage Weil release |= CEPH_CAP_AUTH_SHARED; 2066355da1ebSSage Weil } 2067355da1ebSSage Weil } 2068355da1ebSSage Weil if (ia_valid & ATTR_GID) { 2069355da1ebSSage Weil dout("setattr %p gid %d -> %d\n", inode, 2070bd2bae6aSEric W. Biederman from_kgid(&init_user_ns, inode->i_gid), 2071bd2bae6aSEric W. Biederman from_kgid(&init_user_ns, attr->ia_gid)); 2072355da1ebSSage Weil if (issued & CEPH_CAP_AUTH_EXCL) { 2073355da1ebSSage Weil inode->i_gid = attr->ia_gid; 2074355da1ebSSage Weil dirtied |= CEPH_CAP_AUTH_EXCL; 2075355da1ebSSage Weil } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2076ab871b90SEric W. Biederman !gid_eq(attr->ia_gid, inode->i_gid)) { 2077ab871b90SEric W. Biederman req->r_args.setattr.gid = cpu_to_le32( 2078ab871b90SEric W. Biederman from_kgid(&init_user_ns, attr->ia_gid)); 2079355da1ebSSage Weil mask |= CEPH_SETATTR_GID; 2080355da1ebSSage Weil release |= CEPH_CAP_AUTH_SHARED; 2081355da1ebSSage Weil } 2082355da1ebSSage Weil } 2083355da1ebSSage Weil if (ia_valid & ATTR_MODE) { 2084355da1ebSSage Weil dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode, 2085355da1ebSSage Weil attr->ia_mode); 2086355da1ebSSage Weil if (issued & CEPH_CAP_AUTH_EXCL) { 2087355da1ebSSage Weil inode->i_mode = attr->ia_mode; 2088355da1ebSSage Weil dirtied |= CEPH_CAP_AUTH_EXCL; 2089355da1ebSSage Weil } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2090355da1ebSSage Weil attr->ia_mode != inode->i_mode) { 20917221fe4cSGuangliang Zhao inode->i_mode = attr->ia_mode; 2092355da1ebSSage Weil req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode); 2093355da1ebSSage Weil mask |= CEPH_SETATTR_MODE; 2094355da1ebSSage Weil release |= CEPH_CAP_AUTH_SHARED; 2095355da1ebSSage Weil } 2096355da1ebSSage Weil } 2097355da1ebSSage Weil 2098355da1ebSSage Weil if (ia_valid & ATTR_ATIME) { 209913442b03SDeepa Dinamani dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode, 21009bbeab41SArnd Bergmann inode->i_atime.tv_sec, inode->i_atime.tv_nsec, 21019bbeab41SArnd Bergmann attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec); 2102355da1ebSSage Weil if (issued & CEPH_CAP_FILE_EXCL) { 2103355da1ebSSage Weil ci->i_time_warp_seq++; 2104355da1ebSSage Weil inode->i_atime = attr->ia_atime; 2105355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_EXCL; 2106355da1ebSSage Weil } else if ((issued & CEPH_CAP_FILE_WR) && 210795582b00SDeepa Dinamani timespec64_compare(&inode->i_atime, 2108355da1ebSSage Weil &attr->ia_atime) < 0) { 2109355da1ebSSage Weil inode->i_atime = attr->ia_atime; 2110355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_WR; 2111355da1ebSSage Weil } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 211295582b00SDeepa Dinamani !timespec64_equal(&inode->i_atime, &attr->ia_atime)) { 21139bbeab41SArnd Bergmann ceph_encode_timespec64(&req->r_args.setattr.atime, 21149bbeab41SArnd Bergmann &attr->ia_atime); 2115355da1ebSSage Weil mask |= CEPH_SETATTR_ATIME; 2116be70489eSYan, Zheng release |= CEPH_CAP_FILE_SHARED | 2117be70489eSYan, Zheng CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2118355da1ebSSage Weil } 2119355da1ebSSage Weil } 2120c62498d7SJeff Layton if (ia_valid & ATTR_SIZE) { 21212d6795fbSJeff Layton loff_t isize = i_size_read(inode); 21222d6795fbSJeff Layton 21232d6795fbSJeff Layton dout("setattr %p size %lld -> %lld\n", inode, isize, attr->ia_size); 2124e90334e8SXiubo Li if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) { 2125e90334e8SXiubo Li if (attr->ia_size > isize) { 2126c62498d7SJeff Layton i_size_write(inode, attr->ia_size); 2127c62498d7SJeff Layton inode->i_blocks = calc_inode_blocks(attr->ia_size); 2128c62498d7SJeff Layton ci->i_reported_size = attr->ia_size; 2129c62498d7SJeff Layton dirtied |= CEPH_CAP_FILE_EXCL; 2130c62498d7SJeff Layton ia_valid |= ATTR_MTIME; 2131e90334e8SXiubo Li } 2132c62498d7SJeff Layton } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 21332d6795fbSJeff Layton attr->ia_size != isize) { 2134c62498d7SJeff Layton req->r_args.setattr.size = cpu_to_le64(attr->ia_size); 21352d6795fbSJeff Layton req->r_args.setattr.old_size = cpu_to_le64(isize); 2136c62498d7SJeff Layton mask |= CEPH_SETATTR_SIZE; 2137c62498d7SJeff Layton release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | 2138c62498d7SJeff Layton CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2139c62498d7SJeff Layton } 2140c62498d7SJeff Layton } 2141355da1ebSSage Weil if (ia_valid & ATTR_MTIME) { 214213442b03SDeepa Dinamani dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode, 21439bbeab41SArnd Bergmann inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, 21449bbeab41SArnd Bergmann attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec); 2145355da1ebSSage Weil if (issued & CEPH_CAP_FILE_EXCL) { 2146355da1ebSSage Weil ci->i_time_warp_seq++; 2147355da1ebSSage Weil inode->i_mtime = attr->ia_mtime; 2148355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_EXCL; 2149355da1ebSSage Weil } else if ((issued & CEPH_CAP_FILE_WR) && 215095582b00SDeepa Dinamani timespec64_compare(&inode->i_mtime, 2151355da1ebSSage Weil &attr->ia_mtime) < 0) { 2152355da1ebSSage Weil inode->i_mtime = attr->ia_mtime; 2153355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_WR; 2154355da1ebSSage Weil } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 215595582b00SDeepa Dinamani !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) { 21569bbeab41SArnd Bergmann ceph_encode_timespec64(&req->r_args.setattr.mtime, 21579bbeab41SArnd Bergmann &attr->ia_mtime); 2158355da1ebSSage Weil mask |= CEPH_SETATTR_MTIME; 2159be70489eSYan, Zheng release |= CEPH_CAP_FILE_SHARED | 2160be70489eSYan, Zheng CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2161355da1ebSSage Weil } 2162355da1ebSSage Weil } 2163355da1ebSSage Weil 2164355da1ebSSage Weil /* these do nothing */ 2165355da1ebSSage Weil if (ia_valid & ATTR_CTIME) { 2166355da1ebSSage Weil bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME| 2167355da1ebSSage Weil ATTR_MODE|ATTR_UID|ATTR_GID)) == 0; 216813442b03SDeepa Dinamani dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode, 21699bbeab41SArnd Bergmann inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, 21709bbeab41SArnd Bergmann attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, 2171355da1ebSSage Weil only ? "ctime only" : "ignored"); 2172355da1ebSSage Weil if (only) { 2173355da1ebSSage Weil /* 2174355da1ebSSage Weil * if kernel wants to dirty ctime but nothing else, 2175355da1ebSSage Weil * we need to choose a cap to dirty under, or do 2176355da1ebSSage Weil * a almost-no-op setattr 2177355da1ebSSage Weil */ 2178355da1ebSSage Weil if (issued & CEPH_CAP_AUTH_EXCL) 2179355da1ebSSage Weil dirtied |= CEPH_CAP_AUTH_EXCL; 2180355da1ebSSage Weil else if (issued & CEPH_CAP_FILE_EXCL) 2181355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_EXCL; 2182355da1ebSSage Weil else if (issued & CEPH_CAP_XATTR_EXCL) 2183355da1ebSSage Weil dirtied |= CEPH_CAP_XATTR_EXCL; 2184355da1ebSSage Weil else 2185355da1ebSSage Weil mask |= CEPH_SETATTR_CTIME; 2186355da1ebSSage Weil } 2187355da1ebSSage Weil } 2188355da1ebSSage Weil if (ia_valid & ATTR_FILE) 2189355da1ebSSage Weil dout("setattr %p ATTR_FILE ... hrm!\n", inode); 2190355da1ebSSage Weil 2191355da1ebSSage Weil if (dirtied) { 2192f66fd9f0SYan, Zheng inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied, 2193f66fd9f0SYan, Zheng &prealloc_cf); 21944ca2fea6SYan, Zheng inode->i_ctime = attr->ia_ctime; 2195b4b924c7SJeff Layton inode_inc_iversion_raw(inode); 2196355da1ebSSage Weil } 2197355da1ebSSage Weil 2198355da1ebSSage Weil release &= issued; 2199be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2200604d1b02SYan, Zheng if (lock_snap_rwsem) 2201604d1b02SYan, Zheng up_read(&mdsc->snap_rwsem); 2202355da1ebSSage Weil 2203fca65b4aSSage Weil if (inode_dirty_flags) 2204fca65b4aSSage Weil __mark_inode_dirty(inode, inode_dirty_flags); 2205fca65b4aSSage Weil 2206355da1ebSSage Weil if (mask) { 220770b666c3SSage Weil req->r_inode = inode; 220870b666c3SSage Weil ihold(inode); 2209355da1ebSSage Weil req->r_inode_drop = release; 2210355da1ebSSage Weil req->r_args.setattr.mask = cpu_to_le32(mask); 2211355da1ebSSage Weil req->r_num_caps = 1; 22120ed1e90aSArnd Bergmann req->r_stamp = attr->ia_ctime; 2213752c8bdcSSage Weil err = ceph_mdsc_do_request(mdsc, NULL, req); 2214355da1ebSSage Weil } 2215355da1ebSSage Weil dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err, 2216355da1ebSSage Weil ceph_cap_string(dirtied), mask); 2217355da1ebSSage Weil 2218355da1ebSSage Weil ceph_mdsc_put_request(req); 22198179a101SYan, Zheng ceph_free_cap_flush(prealloc_cf); 22208179a101SYan, Zheng 22218179a101SYan, Zheng if (err >= 0 && (mask & CEPH_SETATTR_SIZE)) 2222b415bf4fSYan, Zheng __ceph_do_pending_vmtruncate(inode); 22238179a101SYan, Zheng 2224355da1ebSSage Weil return err; 2225355da1ebSSage Weil } 2226355da1ebSSage Weil 2227355da1ebSSage Weil /* 2228a26feccaSAndreas Gruenbacher * setattr 2229a26feccaSAndreas Gruenbacher */ 2230c1632a0fSChristian Brauner int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 2231549c7297SChristian Brauner struct iattr *attr) 2232a26feccaSAndreas Gruenbacher { 2233fd5472edSJan Kara struct inode *inode = d_inode(dentry); 223436a4c72dSChengguang Xu struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 2235fd5472edSJan Kara int err; 2236fd5472edSJan Kara 2237fd5472edSJan Kara if (ceph_snap(inode) != CEPH_NOSNAP) 2238fd5472edSJan Kara return -EROFS; 2239fd5472edSJan Kara 22405d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode)) 22415d6451b1SJeff Layton return -ESTALE; 22425d6451b1SJeff Layton 2243c1632a0fSChristian Brauner err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 2244fd5472edSJan Kara if (err != 0) 2245fd5472edSJan Kara return err; 2246fd5472edSJan Kara 22472b83845fSLuis Henriques if ((attr->ia_valid & ATTR_SIZE) && 22482d6795fbSJeff Layton attr->ia_size > max(i_size_read(inode), fsc->max_file_size)) 224936a4c72dSChengguang Xu return -EFBIG; 225036a4c72dSChengguang Xu 225136a4c72dSChengguang Xu if ((attr->ia_valid & ATTR_SIZE) && 22522b83845fSLuis Henriques ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size)) 22532b83845fSLuis Henriques return -EDQUOT; 22542b83845fSLuis Henriques 22558179a101SYan, Zheng err = __ceph_setattr(inode, attr); 22568179a101SYan, Zheng 22578179a101SYan, Zheng if (err >= 0 && (attr->ia_valid & ATTR_MODE)) 225813e83a49SChristian Brauner err = posix_acl_chmod(&nop_mnt_idmap, dentry, attr->ia_mode); 22598179a101SYan, Zheng 22608179a101SYan, Zheng return err; 2261a26feccaSAndreas Gruenbacher } 2262a26feccaSAndreas Gruenbacher 22635eed80fbSXiubo Li int ceph_try_to_choose_auth_mds(struct inode *inode, int mask) 22645eed80fbSXiubo Li { 22655eed80fbSXiubo Li int issued = ceph_caps_issued(ceph_inode(inode)); 22665eed80fbSXiubo Li 22675eed80fbSXiubo Li /* 22685eed80fbSXiubo Li * If any 'x' caps is issued we can just choose the auth MDS 22695eed80fbSXiubo Li * instead of the random replica MDSes. Because only when the 22705eed80fbSXiubo Li * Locker is in LOCK_EXEC state will the loner client could 22715eed80fbSXiubo Li * get the 'x' caps. And if we send the getattr requests to 22725eed80fbSXiubo Li * any replica MDS it must auth pin and tries to rdlock from 22735eed80fbSXiubo Li * the auth MDS, and then the auth MDS need to do the Locker 22745eed80fbSXiubo Li * state transition to LOCK_SYNC. And after that the lock state 22755eed80fbSXiubo Li * will change back. 22765eed80fbSXiubo Li * 22775eed80fbSXiubo Li * This cost much when doing the Locker state transition and 22785eed80fbSXiubo Li * usually will need to revoke caps from clients. 22798266c4d7SXiubo Li * 22808266c4d7SXiubo Li * And for the 'Xs' caps for getxattr we will also choose the 22818266c4d7SXiubo Li * auth MDS, because the MDS side code is buggy due to setxattr 22828266c4d7SXiubo Li * won't notify the replica MDSes when the values changed and 22838266c4d7SXiubo Li * the replica MDS will return the old values. Though we will 22848266c4d7SXiubo Li * fix it in MDS code, but this still makes sense for old ceph. 22855eed80fbSXiubo Li */ 22865eed80fbSXiubo Li if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL)) 22878266c4d7SXiubo Li || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR))) 22885eed80fbSXiubo Li return USE_AUTH_MDS; 22895eed80fbSXiubo Li else 22905eed80fbSXiubo Li return USE_ANY_MDS; 22915eed80fbSXiubo Li } 22925eed80fbSXiubo Li 2293a26feccaSAndreas Gruenbacher /* 2294355da1ebSSage Weil * Verify that we have a lease on the given mask. If not, 2295355da1ebSSage Weil * do a getattr against an mds. 2296355da1ebSSage Weil */ 229701deead0SYan, Zheng int __ceph_do_getattr(struct inode *inode, struct page *locked_page, 229801deead0SYan, Zheng int mask, bool force) 2299355da1ebSSage Weil { 23003d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 23013d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc; 2302355da1ebSSage Weil struct ceph_mds_request *req; 230349a9f4f6SYan, Zheng int mode; 2304355da1ebSSage Weil int err; 2305355da1ebSSage Weil 2306355da1ebSSage Weil if (ceph_snap(inode) == CEPH_SNAPDIR) { 2307355da1ebSSage Weil dout("do_getattr inode %p SNAPDIR\n", inode); 2308355da1ebSSage Weil return 0; 2309355da1ebSSage Weil } 2310355da1ebSSage Weil 231101deead0SYan, Zheng dout("do_getattr inode %p mask %s mode 0%o\n", 231201deead0SYan, Zheng inode, ceph_cap_string(mask), inode->i_mode); 23131af16d54SXiubo Li if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1)) 2314355da1ebSSage Weil return 0; 2315355da1ebSSage Weil 23165eed80fbSXiubo Li mode = ceph_try_to_choose_auth_mds(inode, mask); 231749a9f4f6SYan, Zheng req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode); 2318355da1ebSSage Weil if (IS_ERR(req)) 2319355da1ebSSage Weil return PTR_ERR(req); 232070b666c3SSage Weil req->r_inode = inode; 232170b666c3SSage Weil ihold(inode); 2322355da1ebSSage Weil req->r_num_caps = 1; 2323355da1ebSSage Weil req->r_args.getattr.mask = cpu_to_le32(mask); 232401deead0SYan, Zheng req->r_locked_page = locked_page; 2325355da1ebSSage Weil err = ceph_mdsc_do_request(mdsc, NULL, req); 232601deead0SYan, Zheng if (locked_page && err == 0) { 232701deead0SYan, Zheng u64 inline_version = req->r_reply_info.targeti.inline_version; 232801deead0SYan, Zheng if (inline_version == 0) { 232901deead0SYan, Zheng /* the reply is supposed to contain inline data */ 233001deead0SYan, Zheng err = -EINVAL; 233148490776SXiubo Li } else if (inline_version == CEPH_INLINE_NONE || 233248490776SXiubo Li inline_version == 1) { 233301deead0SYan, Zheng err = -ENODATA; 233401deead0SYan, Zheng } else { 233501deead0SYan, Zheng err = req->r_reply_info.targeti.inline_len; 233601deead0SYan, Zheng } 233701deead0SYan, Zheng } 2338355da1ebSSage Weil ceph_mdsc_put_request(req); 2339355da1ebSSage Weil dout("do_getattr result=%d\n", err); 2340355da1ebSSage Weil return err; 2341355da1ebSSage Weil } 2342355da1ebSSage Weil 23436ddf5f16SMilind Changire int ceph_do_getvxattr(struct inode *inode, const char *name, void *value, 23446ddf5f16SMilind Changire size_t size) 23456ddf5f16SMilind Changire { 23466ddf5f16SMilind Changire struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 23476ddf5f16SMilind Changire struct ceph_mds_client *mdsc = fsc->mdsc; 23486ddf5f16SMilind Changire struct ceph_mds_request *req; 23496ddf5f16SMilind Changire int mode = USE_AUTH_MDS; 23506ddf5f16SMilind Changire int err; 23516ddf5f16SMilind Changire char *xattr_value; 23526ddf5f16SMilind Changire size_t xattr_value_len; 23536ddf5f16SMilind Changire 23546ddf5f16SMilind Changire req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode); 23556ddf5f16SMilind Changire if (IS_ERR(req)) { 23566ddf5f16SMilind Changire err = -ENOMEM; 23576ddf5f16SMilind Changire goto out; 23586ddf5f16SMilind Changire } 23596ddf5f16SMilind Changire 23606eb06c46SXiubo Li req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR; 23616ddf5f16SMilind Changire req->r_path2 = kstrdup(name, GFP_NOFS); 23626ddf5f16SMilind Changire if (!req->r_path2) { 23636ddf5f16SMilind Changire err = -ENOMEM; 23646ddf5f16SMilind Changire goto put; 23656ddf5f16SMilind Changire } 23666ddf5f16SMilind Changire 23676ddf5f16SMilind Changire ihold(inode); 23686ddf5f16SMilind Changire req->r_inode = inode; 23696ddf5f16SMilind Changire err = ceph_mdsc_do_request(mdsc, NULL, req); 23706ddf5f16SMilind Changire if (err < 0) 23716ddf5f16SMilind Changire goto put; 23726ddf5f16SMilind Changire 23736ddf5f16SMilind Changire xattr_value = req->r_reply_info.xattr_info.xattr_value; 23746ddf5f16SMilind Changire xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len; 23756ddf5f16SMilind Changire 23766ddf5f16SMilind Changire dout("do_getvxattr xattr_value_len:%zu, size:%zu\n", xattr_value_len, size); 23776ddf5f16SMilind Changire 23786ddf5f16SMilind Changire err = (int)xattr_value_len; 23796ddf5f16SMilind Changire if (size == 0) 23806ddf5f16SMilind Changire goto put; 23816ddf5f16SMilind Changire 23826ddf5f16SMilind Changire if (xattr_value_len > size) { 23836ddf5f16SMilind Changire err = -ERANGE; 23846ddf5f16SMilind Changire goto put; 23856ddf5f16SMilind Changire } 23866ddf5f16SMilind Changire 23876ddf5f16SMilind Changire memcpy(value, xattr_value, xattr_value_len); 23886ddf5f16SMilind Changire put: 23896ddf5f16SMilind Changire ceph_mdsc_put_request(req); 23906ddf5f16SMilind Changire out: 23916ddf5f16SMilind Changire dout("do_getvxattr result=%d\n", err); 23926ddf5f16SMilind Changire return err; 23936ddf5f16SMilind Changire } 23946ddf5f16SMilind Changire 2395355da1ebSSage Weil 2396355da1ebSSage Weil /* 2397355da1ebSSage Weil * Check inode permissions. We verify we have a valid value for 2398355da1ebSSage Weil * the AUTH cap, then call the generic handler. 2399355da1ebSSage Weil */ 2400*4609e1f1SChristian Brauner int ceph_permission(struct mnt_idmap *idmap, struct inode *inode, 2401549c7297SChristian Brauner int mask) 2402355da1ebSSage Weil { 2403b74c79e9SNick Piggin int err; 2404b74c79e9SNick Piggin 240510556cb2SAl Viro if (mask & MAY_NOT_BLOCK) 2406b74c79e9SNick Piggin return -ECHILD; 2407b74c79e9SNick Piggin 2408508b32d8SYan, Zheng err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false); 2409355da1ebSSage Weil 2410355da1ebSSage Weil if (!err) 2411*4609e1f1SChristian Brauner err = generic_permission(&nop_mnt_idmap, inode, mask); 2412355da1ebSSage Weil return err; 2413355da1ebSSage Weil } 2414355da1ebSSage Weil 2415428bb68aSJeff Layton /* Craft a mask of needed caps given a set of requested statx attrs. */ 241604fabb11SJeff Layton static int statx_to_caps(u32 want, umode_t mode) 2417428bb68aSJeff Layton { 2418428bb68aSJeff Layton int mask = 0; 2419428bb68aSJeff Layton 242058981784SJeff Layton if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME)) 2421428bb68aSJeff Layton mask |= CEPH_CAP_AUTH_SHARED; 2422428bb68aSJeff Layton 242304fabb11SJeff Layton if (want & (STATX_NLINK|STATX_CTIME)) { 242404fabb11SJeff Layton /* 242504fabb11SJeff Layton * The link count for directories depends on inode->i_subdirs, 242604fabb11SJeff Layton * and that is only updated when Fs caps are held. 242704fabb11SJeff Layton */ 242804fabb11SJeff Layton if (S_ISDIR(mode)) 242904fabb11SJeff Layton mask |= CEPH_CAP_FILE_SHARED; 243004fabb11SJeff Layton else 2431428bb68aSJeff Layton mask |= CEPH_CAP_LINK_SHARED; 243204fabb11SJeff Layton } 2433428bb68aSJeff Layton 2434428bb68aSJeff Layton if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE| 2435428bb68aSJeff Layton STATX_BLOCKS)) 2436428bb68aSJeff Layton mask |= CEPH_CAP_FILE_SHARED; 2437428bb68aSJeff Layton 2438428bb68aSJeff Layton if (want & (STATX_CTIME)) 2439428bb68aSJeff Layton mask |= CEPH_CAP_XATTR_SHARED; 2440428bb68aSJeff Layton 2441428bb68aSJeff Layton return mask; 2442428bb68aSJeff Layton } 2443428bb68aSJeff Layton 2444355da1ebSSage Weil /* 2445428bb68aSJeff Layton * Get all the attributes. If we have sufficient caps for the requested attrs, 2446428bb68aSJeff Layton * then we can avoid talking to the MDS at all. 2447355da1ebSSage Weil */ 2448b74d24f7SChristian Brauner int ceph_getattr(struct mnt_idmap *idmap, const struct path *path, 2449549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int flags) 2450355da1ebSSage Weil { 2451a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 2452aa87052dSXiubo Li struct super_block *sb = inode->i_sb; 2453232d4b01SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 245458981784SJeff Layton u32 valid_mask = STATX_BASIC_STATS; 2455428bb68aSJeff Layton int err = 0; 2456355da1ebSSage Weil 24575d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode)) 24585d6451b1SJeff Layton return -ESTALE; 24595d6451b1SJeff Layton 2460428bb68aSJeff Layton /* Skip the getattr altogether if we're asked not to sync */ 2461261998c3SXiubo Li if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) { 246204fabb11SJeff Layton err = ceph_do_getattr(inode, 246304fabb11SJeff Layton statx_to_caps(request_mask, inode->i_mode), 2464428bb68aSJeff Layton flags & AT_STATX_FORCE_SYNC); 2465428bb68aSJeff Layton if (err) 2466428bb68aSJeff Layton return err; 2467428bb68aSJeff Layton } 2468428bb68aSJeff Layton 2469b74d24f7SChristian Brauner generic_fillattr(&nop_mnt_idmap, inode, stat); 2470ebce3eb2SJeff Layton stat->ino = ceph_present_inode(inode); 247158981784SJeff Layton 247258981784SJeff Layton /* 247358981784SJeff Layton * btime on newly-allocated inodes is 0, so if this is still set to 247458981784SJeff Layton * that, then assume that it's not valid. 247558981784SJeff Layton */ 247658981784SJeff Layton if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) { 247758981784SJeff Layton stat->btime = ci->i_btime; 247858981784SJeff Layton valid_mask |= STATX_BTIME; 247958981784SJeff Layton } 248058981784SJeff Layton 248175c9627eSYan, Zheng if (ceph_snap(inode) == CEPH_NOSNAP) 2482aa87052dSXiubo Li stat->dev = sb->s_dev; 2483355da1ebSSage Weil else 248475c9627eSYan, Zheng stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0; 248575c9627eSYan, Zheng 2486232d4b01SSage Weil if (S_ISDIR(inode->i_mode)) { 2487aa87052dSXiubo Li if (ceph_test_mount_opt(ceph_sb_to_client(sb), RBYTES)) { 2488232d4b01SSage Weil stat->size = ci->i_rbytes; 2489aa87052dSXiubo Li } else if (ceph_snap(inode) == CEPH_SNAPDIR) { 2490aa87052dSXiubo Li struct ceph_inode_info *pci; 2491aa87052dSXiubo Li struct ceph_snap_realm *realm; 2492aa87052dSXiubo Li struct inode *parent; 2493aa87052dSXiubo Li 2494aa87052dSXiubo Li parent = ceph_lookup_inode(sb, ceph_ino(inode)); 2495f86a4866SDan Carpenter if (IS_ERR(parent)) 2496aa87052dSXiubo Li return PTR_ERR(parent); 2497aa87052dSXiubo Li 2498aa87052dSXiubo Li pci = ceph_inode(parent); 2499aa87052dSXiubo Li spin_lock(&pci->i_ceph_lock); 2500aa87052dSXiubo Li realm = pci->i_snap_realm; 2501aa87052dSXiubo Li if (realm) 2502aa87052dSXiubo Li stat->size = realm->num_snaps; 25031c1266bbSYehuda Sadeh else 2504aa87052dSXiubo Li stat->size = 0; 2505aa87052dSXiubo Li spin_unlock(&pci->i_ceph_lock); 2506aa87052dSXiubo Li iput(parent); 2507aa87052dSXiubo Li } else { 25081c1266bbSYehuda Sadeh stat->size = ci->i_files + ci->i_subdirs; 2509aa87052dSXiubo Li } 2510232d4b01SSage Weil stat->blocks = 0; 2511355da1ebSSage Weil stat->blksize = 65536; 25128c6286f1SLuis Henriques /* 25138c6286f1SLuis Henriques * Some applications rely on the number of st_nlink 25148c6286f1SLuis Henriques * value on directories to be either 0 (if unlinked) 25158c6286f1SLuis Henriques * or 2 + number of subdirectories. 25168c6286f1SLuis Henriques */ 25178c6286f1SLuis Henriques if (stat->nlink == 1) 25188c6286f1SLuis Henriques /* '.' + '..' + subdirs */ 25198c6286f1SLuis Henriques stat->nlink = 1 + 1 + ci->i_subdirs; 2520355da1ebSSage Weil } 2521428bb68aSJeff Layton 252258981784SJeff Layton stat->result_mask = request_mask & valid_mask; 2523355da1ebSSage Weil return err; 2524355da1ebSSage Weil } 25255d6451b1SJeff Layton 25265d6451b1SJeff Layton void ceph_inode_shutdown(struct inode *inode) 25275d6451b1SJeff Layton { 25285d6451b1SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode); 25295d6451b1SJeff Layton struct rb_node *p; 25305d6451b1SJeff Layton int iputs = 0; 25315d6451b1SJeff Layton bool invalidate = false; 25325d6451b1SJeff Layton 25335d6451b1SJeff Layton spin_lock(&ci->i_ceph_lock); 25345d6451b1SJeff Layton ci->i_ceph_flags |= CEPH_I_SHUTDOWN; 25355d6451b1SJeff Layton p = rb_first(&ci->i_caps); 25365d6451b1SJeff Layton while (p) { 25375d6451b1SJeff Layton struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); 25385d6451b1SJeff Layton 25395d6451b1SJeff Layton p = rb_next(p); 25405d6451b1SJeff Layton iputs += ceph_purge_inode_cap(inode, cap, &invalidate); 25415d6451b1SJeff Layton } 25425d6451b1SJeff Layton spin_unlock(&ci->i_ceph_lock); 25435d6451b1SJeff Layton 25445d6451b1SJeff Layton if (invalidate) 25455d6451b1SJeff Layton ceph_queue_invalidate(inode); 25465d6451b1SJeff Layton while (iputs--) 25475d6451b1SJeff Layton iput(inode); 25485d6451b1SJeff Layton } 2549