Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 3 | |
| 4 | #include <linux/module.h> |
| 5 | #include <linux/fs.h> |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 6 | #include <linux/slab.h> |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/uaccess.h> |
| 9 | #include <linux/kernel.h> |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 10 | #include <linux/writeback.h> |
| 11 | #include <linux/vmalloc.h> |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 12 | #include <linux/xattr.h> |
Linus Torvalds | 4db658e | 2014-01-28 18:06:18 -0800 | [diff] [blame] | 13 | #include <linux/posix_acl.h> |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 14 | #include <linux/random.h> |
Yan, Zheng | a407846 | 2016-05-03 20:55:50 +0800 | [diff] [blame] | 15 | #include <linux/sort.h> |
Jeff Layton | a35ead3 | 2019-06-06 07:29:23 -0400 | [diff] [blame] | 16 | #include <linux/iversion.h> |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 17 | |
| 18 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 19 | #include "mds_client.h" |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 20 | #include "cache.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 21 | #include <linux/ceph/decode.h> |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Ceph inode operations |
| 25 | * |
| 26 | * Implement basic inode helpers (get, alloc) and inode ops (getattr, |
| 27 | * setattr, etc.), xattr helpers, and helpers for assimilating |
| 28 | * metadata returned by the MDS into our cache. |
| 29 | * |
| 30 | * Also define helpers for doing asynchronous writeback, invalidation, |
| 31 | * and truncation for the benefit of those who can't afford to block |
| 32 | * (typically because they are in the message handler path). |
| 33 | */ |
| 34 | |
| 35 | static const struct inode_operations ceph_symlink_iops; |
| 36 | |
Yan, Zheng | 1cf89a8 | 2019-05-18 11:18:44 +0800 | [diff] [blame] | 37 | static void ceph_inode_work(struct work_struct *work); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * find or create an inode, given the ceph ino number |
| 41 | */ |
Yehuda Sadeh | ad1fee9 | 2011-01-21 16:44:03 -0800 | [diff] [blame] | 42 | static int ceph_set_ino_cb(struct inode *inode, void *data) |
| 43 | { |
Jeff Layton | ebce3eb | 2020-08-18 08:03:48 -0400 | [diff] [blame] | 44 | struct ceph_inode_info *ci = ceph_inode(inode); |
Xiubo Li | 1dd8d47 | 2020-09-03 09:01:40 -0400 | [diff] [blame] | 45 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); |
Jeff Layton | ebce3eb | 2020-08-18 08:03:48 -0400 | [diff] [blame] | 46 | |
| 47 | ci->i_vino = *(struct ceph_vino *)data; |
| 48 | inode->i_ino = ceph_vino_to_ino_t(ci->i_vino); |
Jeff Layton | a35ead3 | 2019-06-06 07:29:23 -0400 | [diff] [blame] | 49 | inode_set_iversion_raw(inode, 0); |
Xiubo Li | 1dd8d47 | 2020-09-03 09:01:40 -0400 | [diff] [blame] | 50 | percpu_counter_inc(&mdsc->metric.total_inodes); |
| 51 | |
Yehuda Sadeh | ad1fee9 | 2011-01-21 16:44:03 -0800 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 55 | struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino) |
| 56 | { |
| 57 | struct inode *inode; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 58 | |
Jeff Layton | d4f6b31 | 2021-04-01 13:55:11 -0400 | [diff] [blame] | 59 | if (ceph_vino_is_reserved(vino)) |
| 60 | return ERR_PTR(-EREMOTEIO); |
| 61 | |
Jeff Layton | ebce3eb | 2020-08-18 08:03:48 -0400 | [diff] [blame] | 62 | inode = iget5_locked(sb, (unsigned long)vino.ino, ceph_ino_compare, |
| 63 | ceph_set_ino_cb, &vino); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 64 | if (!inode) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 65 | return ERR_PTR(-ENOMEM); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 66 | |
Jeff Layton | ebce3eb | 2020-08-18 08:03:48 -0400 | [diff] [blame] | 67 | dout("get_inode on %llu=%llx.%llx got %p new %d\n", ceph_present_inode(inode), |
| 68 | ceph_vinop(inode), inode, !!(inode->i_state & I_NEW)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 69 | return inode; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * get/constuct snapdir inode for a given directory |
| 74 | */ |
| 75 | struct inode *ceph_get_snapdir(struct inode *parent) |
| 76 | { |
| 77 | struct ceph_vino vino = { |
| 78 | .ino = ceph_ino(parent), |
| 79 | .snap = CEPH_SNAPDIR, |
| 80 | }; |
| 81 | struct inode *inode = ceph_get_inode(parent->i_sb, vino); |
Sage Weil | b377ff1 | 2009-11-11 15:22:37 -0800 | [diff] [blame] | 82 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 83 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 84 | if (IS_ERR(inode)) |
Julia Lawall | 7e34bc5 | 2010-05-22 12:01:14 +0200 | [diff] [blame] | 85 | return inode; |
Jeff Layton | 3e10a15 | 2021-02-25 15:04:15 -0500 | [diff] [blame] | 86 | |
| 87 | if (!S_ISDIR(parent->i_mode)) { |
| 88 | pr_warn_once("bad snapdir parent type (mode=0%o)\n", |
| 89 | parent->i_mode); |
| 90 | return ERR_PTR(-ENOTDIR); |
| 91 | } |
| 92 | |
| 93 | if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) { |
| 94 | pr_warn_once("bad snapdir inode type (mode=0%o)\n", |
| 95 | inode->i_mode); |
| 96 | return ERR_PTR(-ENOTDIR); |
| 97 | } |
| 98 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 99 | inode->i_mode = parent->i_mode; |
| 100 | inode->i_uid = parent->i_uid; |
| 101 | inode->i_gid = parent->i_gid; |
Luis Henriques | ef91572 | 2020-03-11 11:05:38 +0000 | [diff] [blame] | 102 | inode->i_mtime = parent->i_mtime; |
| 103 | inode->i_ctime = parent->i_ctime; |
| 104 | inode->i_atime = parent->i_atime; |
Sage Weil | b377ff1 | 2009-11-11 15:22:37 -0800 | [diff] [blame] | 105 | ci->i_rbytes = 0; |
Luis Henriques | ef91572 | 2020-03-11 11:05:38 +0000 | [diff] [blame] | 106 | ci->i_btime = ceph_inode(parent)->i_btime; |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 107 | |
Jeff Layton | d3c51ae | 2021-03-01 07:38:01 -0500 | [diff] [blame] | 108 | if (inode->i_state & I_NEW) { |
| 109 | inode->i_op = &ceph_snapdir_iops; |
| 110 | inode->i_fop = &ceph_snapdir_fops; |
| 111 | ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */ |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 112 | unlock_new_inode(inode); |
Jeff Layton | d3c51ae | 2021-03-01 07:38:01 -0500 | [diff] [blame] | 113 | } |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 114 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 115 | return inode; |
| 116 | } |
| 117 | |
| 118 | const struct inode_operations ceph_file_iops = { |
| 119 | .permission = ceph_permission, |
| 120 | .setattr = ceph_setattr, |
| 121 | .getattr = ceph_getattr, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 122 | .listxattr = ceph_listxattr, |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 123 | .get_acl = ceph_get_acl, |
Sage Weil | 72466d0 | 2014-01-29 06:22:25 -0800 | [diff] [blame] | 124 | .set_acl = ceph_set_acl, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | |
| 128 | /* |
| 129 | * We use a 'frag tree' to keep track of the MDS's directory fragments |
| 130 | * for a given inode (usually there is just a single fragment). We |
| 131 | * need to know when a child frag is delegated to a new MDS, or when |
| 132 | * it is flagged as replicated, so we can direct our requests |
| 133 | * accordingly. |
| 134 | */ |
| 135 | |
| 136 | /* |
| 137 | * find/create a frag in the tree |
| 138 | */ |
| 139 | static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci, |
| 140 | u32 f) |
| 141 | { |
| 142 | struct rb_node **p; |
| 143 | struct rb_node *parent = NULL; |
| 144 | struct ceph_inode_frag *frag; |
| 145 | int c; |
| 146 | |
| 147 | p = &ci->i_fragtree.rb_node; |
| 148 | while (*p) { |
| 149 | parent = *p; |
| 150 | frag = rb_entry(parent, struct ceph_inode_frag, node); |
| 151 | c = ceph_frag_compare(f, frag->frag); |
| 152 | if (c < 0) |
| 153 | p = &(*p)->rb_left; |
| 154 | else if (c > 0) |
| 155 | p = &(*p)->rb_right; |
| 156 | else |
| 157 | return frag; |
| 158 | } |
| 159 | |
| 160 | frag = kmalloc(sizeof(*frag), GFP_NOFS); |
Markus Elfring | 5130880 | 2017-08-20 20:00:09 +0200 | [diff] [blame] | 161 | if (!frag) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 162 | return ERR_PTR(-ENOMEM); |
Markus Elfring | 5130880 | 2017-08-20 20:00:09 +0200 | [diff] [blame] | 163 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 164 | frag->frag = f; |
| 165 | frag->split_by = 0; |
| 166 | frag->mds = -1; |
| 167 | frag->ndist = 0; |
| 168 | |
| 169 | rb_link_node(&frag->node, parent, p); |
| 170 | rb_insert_color(&frag->node, &ci->i_fragtree); |
| 171 | |
| 172 | dout("get_or_create_frag added %llx.%llx frag %x\n", |
| 173 | ceph_vinop(&ci->vfs_inode), f); |
| 174 | return frag; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * find a specific frag @f |
| 179 | */ |
| 180 | struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f) |
| 181 | { |
| 182 | struct rb_node *n = ci->i_fragtree.rb_node; |
| 183 | |
| 184 | while (n) { |
| 185 | struct ceph_inode_frag *frag = |
| 186 | rb_entry(n, struct ceph_inode_frag, node); |
| 187 | int c = ceph_frag_compare(f, frag->frag); |
| 188 | if (c < 0) |
| 189 | n = n->rb_left; |
| 190 | else if (c > 0) |
| 191 | n = n->rb_right; |
| 192 | else |
| 193 | return frag; |
| 194 | } |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Choose frag containing the given value @v. If @pfrag is |
| 200 | * specified, copy the frag delegation info to the caller if |
| 201 | * it is present. |
| 202 | */ |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 203 | static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v, |
| 204 | struct ceph_inode_frag *pfrag, int *found) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 205 | { |
| 206 | u32 t = ceph_frag_make(0, 0); |
| 207 | struct ceph_inode_frag *frag; |
| 208 | unsigned nway, i; |
| 209 | u32 n; |
| 210 | |
| 211 | if (found) |
| 212 | *found = 0; |
| 213 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 214 | while (1) { |
| 215 | WARN_ON(!ceph_frag_contains_value(t, v)); |
| 216 | frag = __ceph_find_frag(ci, t); |
| 217 | if (!frag) |
| 218 | break; /* t is a leaf */ |
| 219 | if (frag->split_by == 0) { |
| 220 | if (pfrag) |
| 221 | memcpy(pfrag, frag, sizeof(*pfrag)); |
| 222 | if (found) |
| 223 | *found = 1; |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | /* choose child */ |
| 228 | nway = 1 << frag->split_by; |
| 229 | dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t, |
| 230 | frag->split_by, nway); |
| 231 | for (i = 0; i < nway; i++) { |
| 232 | n = ceph_frag_make_child(t, frag->split_by, i); |
| 233 | if (ceph_frag_contains_value(n, v)) { |
| 234 | t = n; |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | BUG_ON(i == nway); |
| 239 | } |
| 240 | dout("choose_frag(%x) = %x\n", v, t); |
| 241 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 242 | return t; |
| 243 | } |
| 244 | |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 245 | u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v, |
| 246 | struct ceph_inode_frag *pfrag, int *found) |
| 247 | { |
| 248 | u32 ret; |
| 249 | mutex_lock(&ci->i_fragtree_mutex); |
| 250 | ret = __ceph_choose_frag(ci, v, pfrag, found); |
| 251 | mutex_unlock(&ci->i_fragtree_mutex); |
| 252 | return ret; |
| 253 | } |
| 254 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 255 | /* |
| 256 | * Process dirfrag (delegation) info from the mds. Include leaf |
| 257 | * fragment in tree ONLY if ndist > 0. Otherwise, only |
| 258 | * branches/splits are included in i_fragtree) |
| 259 | */ |
| 260 | static int ceph_fill_dirfrag(struct inode *inode, |
| 261 | struct ceph_mds_reply_dirfrag *dirinfo) |
| 262 | { |
| 263 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 264 | struct ceph_inode_frag *frag; |
| 265 | u32 id = le32_to_cpu(dirinfo->frag); |
| 266 | int mds = le32_to_cpu(dirinfo->auth); |
| 267 | int ndist = le32_to_cpu(dirinfo->ndist); |
Yan, Zheng | 8d08503 | 2014-04-18 22:01:38 +0800 | [diff] [blame] | 268 | int diri_auth = -1; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 269 | int i; |
| 270 | int err = 0; |
| 271 | |
Yan, Zheng | 8d08503 | 2014-04-18 22:01:38 +0800 | [diff] [blame] | 272 | spin_lock(&ci->i_ceph_lock); |
| 273 | if (ci->i_auth_cap) |
| 274 | diri_auth = ci->i_auth_cap->mds; |
| 275 | spin_unlock(&ci->i_ceph_lock); |
| 276 | |
Yan, Zheng | 4217211 | 2016-05-03 22:33:20 +0800 | [diff] [blame] | 277 | if (mds == -1) /* CDIR_AUTH_PARENT */ |
| 278 | mds = diri_auth; |
| 279 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 280 | mutex_lock(&ci->i_fragtree_mutex); |
Yan, Zheng | 8d08503 | 2014-04-18 22:01:38 +0800 | [diff] [blame] | 281 | if (ndist == 0 && mds == diri_auth) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 282 | /* no delegation info needed. */ |
| 283 | frag = __ceph_find_frag(ci, id); |
| 284 | if (!frag) |
| 285 | goto out; |
| 286 | if (frag->split_by == 0) { |
| 287 | /* tree leaf, remove */ |
| 288 | dout("fill_dirfrag removed %llx.%llx frag %x" |
| 289 | " (no ref)\n", ceph_vinop(inode), id); |
| 290 | rb_erase(&frag->node, &ci->i_fragtree); |
| 291 | kfree(frag); |
| 292 | } else { |
| 293 | /* tree branch, keep and clear */ |
| 294 | dout("fill_dirfrag cleared %llx.%llx frag %x" |
| 295 | " referral\n", ceph_vinop(inode), id); |
| 296 | frag->mds = -1; |
| 297 | frag->ndist = 0; |
| 298 | } |
| 299 | goto out; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /* find/add this frag to store mds delegation info */ |
| 304 | frag = __get_or_create_frag(ci, id); |
| 305 | if (IS_ERR(frag)) { |
| 306 | /* this is not the end of the world; we can continue |
| 307 | with bad/inaccurate delegation info */ |
| 308 | pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n", |
| 309 | ceph_vinop(inode), le32_to_cpu(dirinfo->frag)); |
| 310 | err = -ENOMEM; |
| 311 | goto out; |
| 312 | } |
| 313 | |
| 314 | frag->mds = mds; |
| 315 | frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP); |
| 316 | for (i = 0; i < frag->ndist; i++) |
| 317 | frag->dist[i] = le32_to_cpu(dirinfo->dist[i]); |
| 318 | dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n", |
| 319 | ceph_vinop(inode), frag->frag, frag->ndist); |
| 320 | |
| 321 | out: |
| 322 | mutex_unlock(&ci->i_fragtree_mutex); |
| 323 | return err; |
| 324 | } |
| 325 | |
Yan, Zheng | a407846 | 2016-05-03 20:55:50 +0800 | [diff] [blame] | 326 | static int frag_tree_split_cmp(const void *l, const void *r) |
| 327 | { |
| 328 | struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l; |
| 329 | struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r; |
Jeff Layton | fe2ed42 | 2017-01-12 14:42:39 -0500 | [diff] [blame] | 330 | return ceph_frag_compare(le32_to_cpu(ls->frag), |
| 331 | le32_to_cpu(rs->frag)); |
Yan, Zheng | a407846 | 2016-05-03 20:55:50 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Yan, Zheng | a4b7431 | 2016-05-04 11:05:10 +0800 | [diff] [blame] | 334 | static bool is_frag_child(u32 f, struct ceph_inode_frag *frag) |
| 335 | { |
| 336 | if (!frag) |
| 337 | return f == ceph_frag_make(0, 0); |
| 338 | if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by) |
| 339 | return false; |
| 340 | return ceph_frag_contains_value(frag->frag, ceph_frag_value(f)); |
| 341 | } |
| 342 | |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 343 | static int ceph_fill_fragtree(struct inode *inode, |
| 344 | struct ceph_frag_tree_head *fragtree, |
| 345 | struct ceph_mds_reply_dirfrag *dirinfo) |
| 346 | { |
| 347 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | a4b7431 | 2016-05-04 11:05:10 +0800 | [diff] [blame] | 348 | struct ceph_inode_frag *frag, *prev_frag = NULL; |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 349 | struct rb_node *rb_node; |
Yan, Zheng | 1b1bc16 | 2016-05-04 11:40:30 +0800 | [diff] [blame] | 350 | unsigned i, split_by, nsplits; |
| 351 | u32 id; |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 352 | bool update = false; |
| 353 | |
| 354 | mutex_lock(&ci->i_fragtree_mutex); |
| 355 | nsplits = le32_to_cpu(fragtree->nsplits); |
Yan, Zheng | 1b1bc16 | 2016-05-04 11:40:30 +0800 | [diff] [blame] | 356 | if (nsplits != ci->i_fragtree_nsplits) { |
| 357 | update = true; |
| 358 | } else if (nsplits) { |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 359 | i = prandom_u32() % nsplits; |
| 360 | id = le32_to_cpu(fragtree->splits[i].frag); |
| 361 | if (!__ceph_find_frag(ci, id)) |
| 362 | update = true; |
| 363 | } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) { |
| 364 | rb_node = rb_first(&ci->i_fragtree); |
| 365 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); |
| 366 | if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node)) |
| 367 | update = true; |
| 368 | } |
| 369 | if (!update && dirinfo) { |
| 370 | id = le32_to_cpu(dirinfo->frag); |
| 371 | if (id != __ceph_choose_frag(ci, id, NULL, NULL)) |
| 372 | update = true; |
| 373 | } |
| 374 | if (!update) |
| 375 | goto out_unlock; |
| 376 | |
Yan, Zheng | a407846 | 2016-05-03 20:55:50 +0800 | [diff] [blame] | 377 | if (nsplits > 1) { |
| 378 | sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]), |
| 379 | frag_tree_split_cmp, NULL); |
| 380 | } |
| 381 | |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 382 | dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode)); |
| 383 | rb_node = rb_first(&ci->i_fragtree); |
| 384 | for (i = 0; i < nsplits; i++) { |
| 385 | id = le32_to_cpu(fragtree->splits[i].frag); |
Yan, Zheng | 1b1bc16 | 2016-05-04 11:40:30 +0800 | [diff] [blame] | 386 | split_by = le32_to_cpu(fragtree->splits[i].by); |
| 387 | if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) { |
| 388 | pr_err("fill_fragtree %llx.%llx invalid split %d/%u, " |
| 389 | "frag %x split by %d\n", ceph_vinop(inode), |
| 390 | i, nsplits, id, split_by); |
| 391 | continue; |
| 392 | } |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 393 | frag = NULL; |
| 394 | while (rb_node) { |
| 395 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); |
| 396 | if (ceph_frag_compare(frag->frag, id) >= 0) { |
| 397 | if (frag->frag != id) |
| 398 | frag = NULL; |
| 399 | else |
| 400 | rb_node = rb_next(rb_node); |
| 401 | break; |
| 402 | } |
| 403 | rb_node = rb_next(rb_node); |
Yan, Zheng | a4b7431 | 2016-05-04 11:05:10 +0800 | [diff] [blame] | 404 | /* delete stale split/leaf node */ |
| 405 | if (frag->split_by > 0 || |
| 406 | !is_frag_child(frag->frag, prev_frag)) { |
| 407 | rb_erase(&frag->node, &ci->i_fragtree); |
Yan, Zheng | 1b1bc16 | 2016-05-04 11:40:30 +0800 | [diff] [blame] | 408 | if (frag->split_by > 0) |
| 409 | ci->i_fragtree_nsplits--; |
Yan, Zheng | a4b7431 | 2016-05-04 11:05:10 +0800 | [diff] [blame] | 410 | kfree(frag); |
| 411 | } |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 412 | frag = NULL; |
| 413 | } |
| 414 | if (!frag) { |
| 415 | frag = __get_or_create_frag(ci, id); |
| 416 | if (IS_ERR(frag)) |
| 417 | continue; |
| 418 | } |
Yan, Zheng | 1b1bc16 | 2016-05-04 11:40:30 +0800 | [diff] [blame] | 419 | if (frag->split_by == 0) |
| 420 | ci->i_fragtree_nsplits++; |
| 421 | frag->split_by = split_by; |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 422 | dout(" frag %x split by %d\n", frag->frag, frag->split_by); |
Yan, Zheng | a4b7431 | 2016-05-04 11:05:10 +0800 | [diff] [blame] | 423 | prev_frag = frag; |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 424 | } |
| 425 | while (rb_node) { |
| 426 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); |
| 427 | rb_node = rb_next(rb_node); |
Yan, Zheng | a4b7431 | 2016-05-04 11:05:10 +0800 | [diff] [blame] | 428 | /* delete stale split/leaf node */ |
| 429 | if (frag->split_by > 0 || |
| 430 | !is_frag_child(frag->frag, prev_frag)) { |
| 431 | rb_erase(&frag->node, &ci->i_fragtree); |
Yan, Zheng | 1b1bc16 | 2016-05-04 11:40:30 +0800 | [diff] [blame] | 432 | if (frag->split_by > 0) |
| 433 | ci->i_fragtree_nsplits--; |
Yan, Zheng | a4b7431 | 2016-05-04 11:05:10 +0800 | [diff] [blame] | 434 | kfree(frag); |
| 435 | } |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 436 | } |
| 437 | out_unlock: |
| 438 | mutex_unlock(&ci->i_fragtree_mutex); |
| 439 | return 0; |
| 440 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * initialize a newly allocated inode. |
| 444 | */ |
| 445 | struct inode *ceph_alloc_inode(struct super_block *sb) |
| 446 | { |
| 447 | struct ceph_inode_info *ci; |
| 448 | int i; |
| 449 | |
| 450 | ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS); |
| 451 | if (!ci) |
| 452 | return NULL; |
| 453 | |
| 454 | dout("alloc_inode %p\n", &ci->vfs_inode); |
| 455 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 456 | spin_lock_init(&ci->i_ceph_lock); |
| 457 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 458 | ci->i_version = 0; |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 459 | ci->i_inline_version = 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 460 | ci->i_time_warp_seq = 0; |
| 461 | ci->i_ceph_flags = 0; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 462 | atomic64_set(&ci->i_ordered_count, 1); |
| 463 | atomic64_set(&ci->i_release_count, 1); |
| 464 | atomic64_set(&ci->i_complete_seq[0], 0); |
| 465 | atomic64_set(&ci->i_complete_seq[1], 0); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 466 | ci->i_symlink = NULL; |
| 467 | |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 468 | ci->i_max_bytes = 0; |
| 469 | ci->i_max_files = 0; |
| 470 | |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 471 | memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout)); |
Jeff Layton | 785892f | 2020-01-02 07:11:38 -0500 | [diff] [blame] | 472 | memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout)); |
Yan, Zheng | 30c156d | 2016-02-14 11:24:31 +0800 | [diff] [blame] | 473 | RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL); |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 474 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 475 | ci->i_fragtree = RB_ROOT; |
| 476 | mutex_init(&ci->i_fragtree_mutex); |
| 477 | |
| 478 | ci->i_xattrs.blob = NULL; |
| 479 | ci->i_xattrs.prealloc_blob = NULL; |
| 480 | ci->i_xattrs.dirty = false; |
| 481 | ci->i_xattrs.index = RB_ROOT; |
| 482 | ci->i_xattrs.count = 0; |
| 483 | ci->i_xattrs.names_size = 0; |
| 484 | ci->i_xattrs.vals_size = 0; |
| 485 | ci->i_xattrs.version = 0; |
| 486 | ci->i_xattrs.index_version = 0; |
| 487 | |
| 488 | ci->i_caps = RB_ROOT; |
| 489 | ci->i_auth_cap = NULL; |
| 490 | ci->i_dirty_caps = 0; |
| 491 | ci->i_flushing_caps = 0; |
| 492 | INIT_LIST_HEAD(&ci->i_dirty_item); |
| 493 | INIT_LIST_HEAD(&ci->i_flushing_item); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 494 | ci->i_prealloc_cap_flush = NULL; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 495 | INIT_LIST_HEAD(&ci->i_cap_flush_list); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 496 | init_waitqueue_head(&ci->i_cap_wq); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 497 | ci->i_hold_caps_max = 0; |
| 498 | INIT_LIST_HEAD(&ci->i_cap_delay_list); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 499 | INIT_LIST_HEAD(&ci->i_cap_snaps); |
| 500 | ci->i_head_snapc = NULL; |
| 501 | ci->i_snap_caps = 0; |
| 502 | |
Yan, Zheng | 719a251 | 2020-03-05 20:21:00 +0800 | [diff] [blame] | 503 | ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ; |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 504 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 505 | ci->i_nr_by_mode[i] = 0; |
| 506 | |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 507 | mutex_init(&ci->i_truncate_mutex); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 508 | ci->i_truncate_seq = 0; |
| 509 | ci->i_truncate_size = 0; |
| 510 | ci->i_truncate_pending = 0; |
| 511 | |
| 512 | ci->i_max_size = 0; |
| 513 | ci->i_reported_size = 0; |
| 514 | ci->i_wanted_max_size = 0; |
| 515 | ci->i_requested_max_size = 0; |
| 516 | |
| 517 | ci->i_pin_ref = 0; |
| 518 | ci->i_rd_ref = 0; |
| 519 | ci->i_rdcache_ref = 0; |
| 520 | ci->i_wr_ref = 0; |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 521 | ci->i_wb_ref = 0; |
Jeff Layton | f85122a | 2019-04-02 08:04:30 -0400 | [diff] [blame] | 522 | ci->i_fx_ref = 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 523 | ci->i_wrbuffer_ref = 0; |
| 524 | ci->i_wrbuffer_ref_head = 0; |
Yan, Zheng | 89aa593 | 2017-09-08 15:23:18 +0800 | [diff] [blame] | 525 | atomic_set(&ci->i_filelock_ref, 0); |
Yan, Zheng | 1e9c2eb | 2019-01-28 20:43:55 +0800 | [diff] [blame] | 526 | atomic_set(&ci->i_shared_gen, 1); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 527 | ci->i_rdcache_gen = 0; |
| 528 | ci->i_rdcache_revoking = 0; |
| 529 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 530 | INIT_LIST_HEAD(&ci->i_unsafe_dirops); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 531 | INIT_LIST_HEAD(&ci->i_unsafe_iops); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 532 | spin_lock_init(&ci->i_unsafe_lock); |
| 533 | |
| 534 | ci->i_snap_realm = NULL; |
| 535 | INIT_LIST_HEAD(&ci->i_snap_realm_item); |
| 536 | INIT_LIST_HEAD(&ci->i_snap_flush_item); |
| 537 | |
Yan, Zheng | 1cf89a8 | 2019-05-18 11:18:44 +0800 | [diff] [blame] | 538 | INIT_WORK(&ci->i_work, ceph_inode_work); |
| 539 | ci->i_work_mask = 0; |
Jeff Layton | 245ce99 | 2019-05-29 11:19:42 -0400 | [diff] [blame] | 540 | memset(&ci->i_btime, '\0', sizeof(ci->i_btime)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 541 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 542 | ceph_fscache_inode_init(ci); |
| 543 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 544 | return &ci->vfs_inode; |
| 545 | } |
| 546 | |
Al Viro | cfa6d41 | 2019-04-10 15:18:50 -0400 | [diff] [blame] | 547 | void ceph_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 548 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 549 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 550 | |
Al Viro | daf5cc2 | 2019-03-26 01:38:58 +0000 | [diff] [blame] | 551 | kfree(ci->i_symlink); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 552 | kmem_cache_free(ceph_inode_cachep, ci); |
| 553 | } |
| 554 | |
Yan, Zheng | 87bc5b8 | 2019-06-02 09:45:38 +0800 | [diff] [blame] | 555 | void ceph_evict_inode(struct inode *inode) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 556 | { |
| 557 | struct ceph_inode_info *ci = ceph_inode(inode); |
Xiubo Li | 1dd8d47 | 2020-09-03 09:01:40 -0400 | [diff] [blame] | 558 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 559 | struct ceph_inode_frag *frag; |
| 560 | struct rb_node *n; |
| 561 | |
Yan, Zheng | 87bc5b8 | 2019-06-02 09:45:38 +0800 | [diff] [blame] | 562 | dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode)); |
| 563 | |
Xiubo Li | 1dd8d47 | 2020-09-03 09:01:40 -0400 | [diff] [blame] | 564 | percpu_counter_dec(&mdsc->metric.total_inodes); |
| 565 | |
Yan, Zheng | 87bc5b8 | 2019-06-02 09:45:38 +0800 | [diff] [blame] | 566 | truncate_inode_pages_final(&inode->i_data); |
Jeff Layton | 400e128 | 2021-12-07 08:44:50 -0500 | [diff] [blame] | 567 | if (inode->i_state & I_PINNING_FSCACHE_WB) |
| 568 | ceph_fscache_unuse_cookie(inode, true); |
Yan, Zheng | 87bc5b8 | 2019-06-02 09:45:38 +0800 | [diff] [blame] | 569 | clear_inode(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 570 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 571 | ceph_fscache_unregister_inode_cookie(ci); |
| 572 | |
Yan, Zheng | d6e4781 | 2019-05-23 11:01:37 +0800 | [diff] [blame] | 573 | __ceph_remove_caps(ci); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 574 | |
Luis Henriques | d557c48 | 2018-01-12 17:19:29 +0000 | [diff] [blame] | 575 | if (__ceph_has_any_quota(ci)) |
| 576 | ceph_adjust_quota_realms_count(inode, false); |
| 577 | |
Sage Weil | 8b218b8 | 2010-03-09 12:59:08 -0800 | [diff] [blame] | 578 | /* |
| 579 | * we may still have a snap_realm reference if there are stray |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 580 | * caps in i_snap_caps. |
Sage Weil | 8b218b8 | 2010-03-09 12:59:08 -0800 | [diff] [blame] | 581 | */ |
| 582 | if (ci->i_snap_realm) { |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 583 | if (ceph_snap(inode) == CEPH_NOSNAP) { |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 584 | dout(" dropping residual ref to snap realm %p\n", |
Jeff Layton | 0ba92e1 | 2021-08-02 11:01:26 -0400 | [diff] [blame] | 585 | ci->i_snap_realm); |
| 586 | ceph_change_snap_realm(inode, NULL); |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 587 | } else { |
| 588 | ceph_put_snapid_map(mdsc, ci->i_snapid_map); |
| 589 | ci->i_snap_realm = NULL; |
| 590 | } |
Sage Weil | 8b218b8 | 2010-03-09 12:59:08 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 593 | while ((n = rb_first(&ci->i_fragtree)) != NULL) { |
| 594 | frag = rb_entry(n, struct ceph_inode_frag, node); |
| 595 | rb_erase(n, &ci->i_fragtree); |
| 596 | kfree(frag); |
| 597 | } |
Yan, Zheng | 1b1bc16 | 2016-05-04 11:40:30 +0800 | [diff] [blame] | 598 | ci->i_fragtree_nsplits = 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 599 | |
| 600 | __ceph_destroy_xattrs(ci); |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame] | 601 | if (ci->i_xattrs.blob) |
| 602 | ceph_buffer_put(ci->i_xattrs.blob); |
| 603 | if (ci->i_xattrs.prealloc_blob) |
| 604 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 605 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 606 | ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns)); |
Jeff Layton | 785892f | 2020-01-02 07:11:38 -0500 | [diff] [blame] | 607 | ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Yan, Zheng | 224a7542 | 2016-05-05 16:40:17 +0800 | [diff] [blame] | 610 | static inline blkcnt_t calc_inode_blocks(u64 size) |
| 611 | { |
| 612 | return (size + (1<<9) - 1) >> 9; |
| 613 | } |
| 614 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 615 | /* |
| 616 | * Helpers to fill in size, ctime, mtime, and atime. We have to be |
| 617 | * careful because either the client or MDS may have more up to date |
| 618 | * info, depending on which capabilities are held, and whether |
| 619 | * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime |
| 620 | * and size are monotonically increasing, except when utimes() or |
| 621 | * truncate() increments the corresponding _seq values.) |
| 622 | */ |
| 623 | int ceph_fill_file_size(struct inode *inode, int issued, |
| 624 | u32 truncate_seq, u64 truncate_size, u64 size) |
| 625 | { |
| 626 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 627 | int queue_trunc = 0; |
Jeff Layton | 2d6795f | 2021-04-09 15:58:35 -0400 | [diff] [blame] | 628 | loff_t isize = i_size_read(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 629 | |
| 630 | if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 || |
Jeff Layton | 2d6795f | 2021-04-09 15:58:35 -0400 | [diff] [blame] | 631 | (truncate_seq == ci->i_truncate_seq && size > isize)) { |
| 632 | dout("size %lld -> %llu\n", isize, size); |
Yan, Zheng | a3d714c | 2016-02-26 17:16:32 +0800 | [diff] [blame] | 633 | if (size > 0 && S_ISDIR(inode->i_mode)) { |
| 634 | pr_err("fill_file_size non-zero size for directory\n"); |
| 635 | size = 0; |
| 636 | } |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 637 | i_size_write(inode, size); |
Yan, Zheng | 224a7542 | 2016-05-05 16:40:17 +0800 | [diff] [blame] | 638 | inode->i_blocks = calc_inode_blocks(size); |
Jeff Layton | 400e128 | 2021-12-07 08:44:50 -0500 | [diff] [blame] | 639 | /* |
| 640 | * If we're expanding, then we should be able to just update |
| 641 | * the existing cookie. |
| 642 | */ |
| 643 | if (size > isize) |
| 644 | ceph_fscache_update(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 645 | ci->i_reported_size = size; |
| 646 | if (truncate_seq != ci->i_truncate_seq) { |
| 647 | dout("truncate_seq %u -> %u\n", |
| 648 | ci->i_truncate_seq, truncate_seq); |
| 649 | ci->i_truncate_seq = truncate_seq; |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 650 | |
| 651 | /* the MDS should have revoked these caps */ |
| 652 | WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL | |
| 653 | CEPH_CAP_FILE_RD | |
| 654 | CEPH_CAP_FILE_WR | |
| 655 | CEPH_CAP_FILE_LAZYIO)); |
Yehuda Sadeh | 3d497d8 | 2010-02-09 11:08:40 -0800 | [diff] [blame] | 656 | /* |
| 657 | * If we hold relevant caps, or in the case where we're |
| 658 | * not the only client referencing this file and we |
| 659 | * don't hold those caps, then we need to check whether |
| 660 | * the file is either opened or mmaped |
| 661 | */ |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 662 | if ((issued & (CEPH_CAP_FILE_CACHE| |
| 663 | CEPH_CAP_FILE_BUFFER)) || |
Yehuda Sadeh | 3d497d8 | 2010-02-09 11:08:40 -0800 | [diff] [blame] | 664 | mapping_mapped(inode->i_mapping) || |
Yan, Zheng | 719a251 | 2020-03-05 20:21:00 +0800 | [diff] [blame] | 665 | __ceph_is_file_opened(ci)) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 666 | ci->i_truncate_pending++; |
| 667 | queue_trunc = 1; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 && |
| 672 | ci->i_truncate_size != truncate_size) { |
| 673 | dout("truncate_size %lld -> %llu\n", ci->i_truncate_size, |
| 674 | truncate_size); |
| 675 | ci->i_truncate_size = truncate_size; |
| 676 | } |
| 677 | return queue_trunc; |
| 678 | } |
| 679 | |
| 680 | void ceph_fill_file_time(struct inode *inode, int issued, |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 681 | u64 time_warp_seq, struct timespec64 *ctime, |
| 682 | struct timespec64 *mtime, struct timespec64 *atime) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 683 | { |
| 684 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 685 | int warn = 0; |
| 686 | |
| 687 | if (issued & (CEPH_CAP_FILE_EXCL| |
| 688 | CEPH_CAP_FILE_WR| |
Sage Weil | d8672d6 | 2010-11-08 09:24:34 -0800 | [diff] [blame] | 689 | CEPH_CAP_FILE_BUFFER| |
| 690 | CEPH_CAP_AUTH_EXCL| |
| 691 | CEPH_CAP_XATTR_EXCL)) { |
Yan, Zheng | ffdeec7 | 2018-03-26 16:46:39 +0800 | [diff] [blame] | 692 | if (ci->i_version == 0 || |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 693 | timespec64_compare(ctime, &inode->i_ctime) > 0) { |
Deepa Dinamani | 13442b0 | 2018-04-23 19:26:50 -0700 | [diff] [blame] | 694 | dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n", |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 695 | inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, |
| 696 | ctime->tv_sec, ctime->tv_nsec); |
| 697 | inode->i_ctime = *ctime; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 698 | } |
Yan, Zheng | ffdeec7 | 2018-03-26 16:46:39 +0800 | [diff] [blame] | 699 | if (ci->i_version == 0 || |
| 700 | ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 701 | /* the MDS did a utimes() */ |
Deepa Dinamani | 13442b0 | 2018-04-23 19:26:50 -0700 | [diff] [blame] | 702 | dout("mtime %lld.%09ld -> %lld.%09ld " |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 703 | "tw %d -> %d\n", |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 704 | inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, |
| 705 | mtime->tv_sec, mtime->tv_nsec, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 706 | ci->i_time_warp_seq, (int)time_warp_seq); |
| 707 | |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 708 | inode->i_mtime = *mtime; |
| 709 | inode->i_atime = *atime; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 710 | ci->i_time_warp_seq = time_warp_seq; |
| 711 | } else if (time_warp_seq == ci->i_time_warp_seq) { |
| 712 | /* nobody did utimes(); take the max */ |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 713 | if (timespec64_compare(mtime, &inode->i_mtime) > 0) { |
Deepa Dinamani | 13442b0 | 2018-04-23 19:26:50 -0700 | [diff] [blame] | 714 | dout("mtime %lld.%09ld -> %lld.%09ld inc\n", |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 715 | inode->i_mtime.tv_sec, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 716 | inode->i_mtime.tv_nsec, |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 717 | mtime->tv_sec, mtime->tv_nsec); |
| 718 | inode->i_mtime = *mtime; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 719 | } |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 720 | if (timespec64_compare(atime, &inode->i_atime) > 0) { |
Deepa Dinamani | 13442b0 | 2018-04-23 19:26:50 -0700 | [diff] [blame] | 721 | dout("atime %lld.%09ld -> %lld.%09ld inc\n", |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 722 | inode->i_atime.tv_sec, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 723 | inode->i_atime.tv_nsec, |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 724 | atime->tv_sec, atime->tv_nsec); |
| 725 | inode->i_atime = *atime; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 726 | } |
| 727 | } else if (issued & CEPH_CAP_FILE_EXCL) { |
| 728 | /* we did a utimes(); ignore mds values */ |
| 729 | } else { |
| 730 | warn = 1; |
| 731 | } |
| 732 | } else { |
Sage Weil | d8672d6 | 2010-11-08 09:24:34 -0800 | [diff] [blame] | 733 | /* we have no write|excl caps; whatever the MDS says is true */ |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 734 | if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) { |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 735 | inode->i_ctime = *ctime; |
| 736 | inode->i_mtime = *mtime; |
| 737 | inode->i_atime = *atime; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 738 | ci->i_time_warp_seq = time_warp_seq; |
| 739 | } else { |
| 740 | warn = 1; |
| 741 | } |
| 742 | } |
| 743 | if (warn) /* time_warp_seq shouldn't go backwards */ |
| 744 | dout("%p mds time_warp_seq %llu < %u\n", |
| 745 | inode, time_warp_seq, ci->i_time_warp_seq); |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Populate an inode based on info from mds. May be called on new or |
| 750 | * existing inodes. |
| 751 | */ |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 752 | int ceph_fill_inode(struct inode *inode, struct page *locked_page, |
| 753 | struct ceph_mds_reply_info_in *iinfo, |
| 754 | struct ceph_mds_reply_dirfrag *dirinfo, |
| 755 | struct ceph_mds_session *session, int cap_fmode, |
| 756 | struct ceph_cap_reservation *caps_reservation) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 757 | { |
Xiubo Li | 2678da8 | 2020-09-03 09:01:39 -0400 | [diff] [blame] | 758 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 759 | struct ceph_mds_reply_inode *info = iinfo->in; |
| 760 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 2af54a7 | 2018-04-27 11:14:39 +0800 | [diff] [blame] | 761 | int issued, new_issued, info_caps; |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 762 | struct timespec64 mtime, atime, ctime; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 763 | struct ceph_buffer *xattr_blob = NULL; |
Luis Henriques | af8a85a | 2019-07-19 15:32:22 +0100 | [diff] [blame] | 764 | struct ceph_buffer *old_blob = NULL; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 765 | struct ceph_string *pool_ns = NULL; |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 766 | struct ceph_cap *new_cap = NULL; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 767 | int err = 0; |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 768 | bool wake = false; |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 769 | bool queue_trunc = false; |
| 770 | bool new_version = false; |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 771 | bool fill_inline = false; |
Jeff Layton | ed94f87 | 2021-02-25 15:04:16 -0500 | [diff] [blame] | 772 | umode_t mode = le32_to_cpu(info->mode); |
| 773 | dev_t rdev = le32_to_cpu(info->rdev); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 774 | |
Jeff Layton | 27171ae | 2021-06-01 09:40:25 -0400 | [diff] [blame] | 775 | lockdep_assert_held(&mdsc->snap_rwsem); |
| 776 | |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 777 | dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 778 | inode, ceph_vinop(inode), le64_to_cpu(info->version), |
| 779 | ci->i_version); |
| 780 | |
Jeff Layton | ed94f87 | 2021-02-25 15:04:16 -0500 | [diff] [blame] | 781 | /* Once I_NEW is cleared, we can't change type or dev numbers */ |
| 782 | if (inode->i_state & I_NEW) { |
| 783 | inode->i_mode = mode; |
| 784 | } else { |
| 785 | if (inode_wrong_type(inode, mode)) { |
| 786 | pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n", |
| 787 | ceph_vinop(inode), inode->i_mode, mode); |
| 788 | return -ESTALE; |
| 789 | } |
| 790 | |
| 791 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) { |
| 792 | pr_warn_once("dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n", |
| 793 | ceph_vinop(inode), MAJOR(inode->i_rdev), |
| 794 | MINOR(inode->i_rdev), MAJOR(rdev), |
| 795 | MINOR(rdev)); |
| 796 | return -ESTALE; |
| 797 | } |
| 798 | } |
| 799 | |
Yan, Zheng | 2af54a7 | 2018-04-27 11:14:39 +0800 | [diff] [blame] | 800 | info_caps = le32_to_cpu(info->cap.caps); |
| 801 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 802 | /* prealloc new cap struct */ |
Jeff Layton | 9a6bed4 | 2019-12-05 08:41:25 -0500 | [diff] [blame] | 803 | if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) { |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 804 | new_cap = ceph_get_cap(mdsc, caps_reservation); |
Jeff Layton | 9a6bed4 | 2019-12-05 08:41:25 -0500 | [diff] [blame] | 805 | if (!new_cap) |
| 806 | return -ENOMEM; |
| 807 | } |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 808 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 809 | /* |
| 810 | * prealloc xattr data, if it looks like we'll need it. only |
| 811 | * if len > 4 (meaning there are actually xattrs; the first 4 |
| 812 | * bytes are the xattr count). |
| 813 | */ |
| 814 | if (iinfo->xattr_len > 4) { |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame] | 815 | xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 816 | if (!xattr_blob) |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 817 | pr_err("%s ENOMEM xattr blob %d bytes\n", __func__, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 818 | iinfo->xattr_len); |
| 819 | } |
| 820 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 821 | if (iinfo->pool_ns_len > 0) |
| 822 | pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data, |
| 823 | iinfo->pool_ns_len); |
| 824 | |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 825 | if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map) |
| 826 | ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode)); |
| 827 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 828 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * provided version will be odd if inode value is projected, |
Sage Weil | 8bd59e0 | 2010-11-08 09:23:12 -0800 | [diff] [blame] | 832 | * even if stable. skip the update if we have newer stable |
| 833 | * info (ours>=theirs, e.g. due to racing mds replies), unless |
| 834 | * we are getting projected (unstable) info (in which case the |
| 835 | * version is odd, and we want ours>theirs). |
| 836 | * us them |
| 837 | * 2 2 skip |
| 838 | * 3 2 skip |
| 839 | * 3 3 update |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 840 | */ |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 841 | if (ci->i_version == 0 || |
| 842 | ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && |
| 843 | le64_to_cpu(info->version) > (ci->i_version & ~1))) |
| 844 | new_version = true; |
| 845 | |
Jeff Layton | a35ead3 | 2019-06-06 07:29:23 -0400 | [diff] [blame] | 846 | /* Update change_attribute */ |
| 847 | inode_set_max_iversion_raw(inode, iinfo->change_attr); |
| 848 | |
Yan, Zheng | 2af54a7 | 2018-04-27 11:14:39 +0800 | [diff] [blame] | 849 | __ceph_caps_issued(ci, &issued); |
| 850 | issued |= __ceph_caps_dirty(ci); |
| 851 | new_issued = ~issued & info_caps; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 852 | |
Luis Henriques | 7506703 | 2019-07-23 16:50:20 +0100 | [diff] [blame] | 853 | /* directories have fl_stripe_unit set to zero */ |
| 854 | if (le32_to_cpu(info->layout.fl_stripe_unit)) |
| 855 | inode->i_blkbits = |
| 856 | fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1; |
| 857 | else |
| 858 | inode->i_blkbits = CEPH_BLOCK_SHIFT; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 859 | |
Luis Henriques | d557c48 | 2018-01-12 17:19:29 +0000 | [diff] [blame] | 860 | __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files); |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 861 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 862 | if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) && |
| 863 | (issued & CEPH_CAP_AUTH_EXCL) == 0) { |
Jeff Layton | ed94f87 | 2021-02-25 15:04:16 -0500 | [diff] [blame] | 864 | inode->i_mode = mode; |
Eric W. Biederman | ab871b9 | 2013-01-31 03:40:12 -0800 | [diff] [blame] | 865 | inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid)); |
| 866 | inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 867 | dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, |
Eric W. Biederman | bd2bae6 | 2013-01-31 04:05:39 -0800 | [diff] [blame] | 868 | from_kuid(&init_user_ns, inode->i_uid), |
| 869 | from_kgid(&init_user_ns, inode->i_gid)); |
Jeff Layton | 245ce99 | 2019-05-29 11:19:42 -0400 | [diff] [blame] | 870 | ceph_decode_timespec64(&ci->i_btime, &iinfo->btime); |
David Disseldorp | 193e7b3 | 2019-04-18 14:15:46 +0200 | [diff] [blame] | 871 | ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 874 | if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) && |
| 875 | (issued & CEPH_CAP_LINK_EXCL) == 0) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 876 | set_nlink(inode, le32_to_cpu(info->nlink)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 877 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 878 | if (new_version || (new_issued & CEPH_CAP_ANY_RD)) { |
| 879 | /* be careful with mtime, atime, size */ |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 880 | ceph_decode_timespec64(&atime, &info->atime); |
| 881 | ceph_decode_timespec64(&mtime, &info->mtime); |
| 882 | ceph_decode_timespec64(&ctime, &info->ctime); |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 883 | ceph_fill_file_time(inode, issued, |
| 884 | le32_to_cpu(info->time_warp_seq), |
| 885 | &ctime, &mtime, &atime); |
| 886 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 887 | |
Yan, Zheng | 2af54a7 | 2018-04-27 11:14:39 +0800 | [diff] [blame] | 888 | if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) { |
| 889 | ci->i_files = le64_to_cpu(info->files); |
| 890 | ci->i_subdirs = le64_to_cpu(info->subdirs); |
| 891 | } |
| 892 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 893 | if (new_version || |
| 894 | (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) { |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 895 | s64 old_pool = ci->i_layout.pool_id; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 896 | struct ceph_string *old_ns; |
| 897 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 898 | ceph_file_layout_from_legacy(&ci->i_layout, &info->layout); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 899 | old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, |
| 900 | lockdep_is_held(&ci->i_ceph_lock)); |
| 901 | rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns); |
| 902 | |
| 903 | if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns) |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 904 | ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 905 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 906 | pool_ns = old_ns; |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 907 | |
| 908 | queue_trunc = ceph_fill_file_size(inode, issued, |
| 909 | le32_to_cpu(info->truncate_seq), |
| 910 | le64_to_cpu(info->truncate_size), |
| 911 | le64_to_cpu(info->size)); |
| 912 | /* only update max_size on auth cap */ |
| 913 | if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && |
| 914 | ci->i_max_size != le64_to_cpu(info->max_size)) { |
| 915 | dout("max_size %lld -> %llu\n", ci->i_max_size, |
| 916 | le64_to_cpu(info->max_size)); |
| 917 | ci->i_max_size = le64_to_cpu(info->max_size); |
| 918 | } |
| 919 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 920 | |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 921 | /* layout and rstat are not tracked by capability, update them if |
| 922 | * the inode info is from auth mds */ |
| 923 | if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) { |
| 924 | if (S_ISDIR(inode->i_mode)) { |
| 925 | ci->i_dir_layout = iinfo->dir_layout; |
| 926 | ci->i_rbytes = le64_to_cpu(info->rbytes); |
| 927 | ci->i_rfiles = le64_to_cpu(info->rfiles); |
| 928 | ci->i_rsubdirs = le64_to_cpu(info->rsubdirs); |
Yan, Zheng | 0879687 | 2019-01-09 11:07:02 +0800 | [diff] [blame] | 929 | ci->i_dir_pin = iinfo->dir_pin; |
Yanhu Cao | e7f7295 | 2020-08-28 09:28:44 +0800 | [diff] [blame] | 930 | ci->i_rsnaps = iinfo->rsnaps; |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 931 | ceph_decode_timespec64(&ci->i_rctime, &info->rctime); |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 935 | /* xattrs */ |
| 936 | /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */ |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 937 | if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) && |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 938 | le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) { |
| 939 | if (ci->i_xattrs.blob) |
Luis Henriques | af8a85a | 2019-07-19 15:32:22 +0100 | [diff] [blame] | 940 | old_blob = ci->i_xattrs.blob; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 941 | ci->i_xattrs.blob = xattr_blob; |
| 942 | if (xattr_blob) |
| 943 | memcpy(ci->i_xattrs.blob->vec.iov_base, |
| 944 | iinfo->xattr_data, iinfo->xattr_len); |
| 945 | ci->i_xattrs.version = le64_to_cpu(info->xattr_version); |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 946 | ceph_forget_all_cached_acls(inode); |
Yan, Zheng | ac6713c | 2019-05-26 16:27:56 +0800 | [diff] [blame] | 947 | ceph_security_invalidate_secctx(inode); |
Sage Weil | a6424e4 | 2010-04-29 09:28:11 -0700 | [diff] [blame] | 948 | xattr_blob = NULL; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Yan, Zheng | ffdeec7 | 2018-03-26 16:46:39 +0800 | [diff] [blame] | 951 | /* finally update i_version */ |
Yan, Zheng | aae1a44 | 2018-05-26 16:54:39 +0800 | [diff] [blame] | 952 | if (le64_to_cpu(info->version) > ci->i_version) |
| 953 | ci->i_version = le64_to_cpu(info->version); |
Yan, Zheng | ffdeec7 | 2018-03-26 16:46:39 +0800 | [diff] [blame] | 954 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 955 | inode->i_mapping->a_ops = &ceph_aops; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 956 | |
| 957 | switch (inode->i_mode & S_IFMT) { |
| 958 | case S_IFIFO: |
| 959 | case S_IFBLK: |
| 960 | case S_IFCHR: |
| 961 | case S_IFSOCK: |
Yan, Zheng | 5ba72e6 | 2018-12-18 11:03:38 +0800 | [diff] [blame] | 962 | inode->i_blkbits = PAGE_SHIFT; |
Jeff Layton | ed94f87 | 2021-02-25 15:04:16 -0500 | [diff] [blame] | 963 | init_special_inode(inode, inode->i_mode, rdev); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 964 | inode->i_op = &ceph_file_iops; |
| 965 | break; |
| 966 | case S_IFREG: |
| 967 | inode->i_op = &ceph_file_iops; |
| 968 | inode->i_fop = &ceph_file_fops; |
| 969 | break; |
| 970 | case S_IFLNK: |
| 971 | inode->i_op = &ceph_symlink_iops; |
| 972 | if (!ci->i_symlink) { |
Xi Wang | 810339ec | 2012-02-03 09:55:36 -0500 | [diff] [blame] | 973 | u32 symlen = iinfo->symlink_len; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 974 | char *sym; |
| 975 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 976 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 977 | |
Yan, Zheng | 224a7542 | 2016-05-05 16:40:17 +0800 | [diff] [blame] | 978 | if (symlen != i_size_read(inode)) { |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 979 | pr_err("%s %llx.%llx BAD symlink " |
| 980 | "size %lld\n", __func__, |
| 981 | ceph_vinop(inode), |
Yan, Zheng | 224a7542 | 2016-05-05 16:40:17 +0800 | [diff] [blame] | 982 | i_size_read(inode)); |
| 983 | i_size_write(inode, symlen); |
| 984 | inode->i_blocks = calc_inode_blocks(symlen); |
| 985 | } |
Xi Wang | 810339ec | 2012-02-03 09:55:36 -0500 | [diff] [blame] | 986 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 987 | err = -ENOMEM; |
Xi Wang | 810339ec | 2012-02-03 09:55:36 -0500 | [diff] [blame] | 988 | sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 989 | if (!sym) |
| 990 | goto out; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 991 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 992 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 993 | if (!ci->i_symlink) |
| 994 | ci->i_symlink = sym; |
| 995 | else |
| 996 | kfree(sym); /* lost a race */ |
| 997 | } |
Al Viro | ac194dc | 2015-05-02 10:50:05 -0400 | [diff] [blame] | 998 | inode->i_link = ci->i_symlink; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 999 | break; |
| 1000 | case S_IFDIR: |
| 1001 | inode->i_op = &ceph_dir_iops; |
| 1002 | inode->i_fop = &ceph_dir_fops; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1003 | break; |
| 1004 | default: |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 1005 | pr_err("%s %llx.%llx BAD mode 0%o\n", __func__, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1006 | ceph_vinop(inode), inode->i_mode); |
| 1007 | } |
| 1008 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1009 | /* were we issued a capability? */ |
Yan, Zheng | 2af54a7 | 2018-04-27 11:14:39 +0800 | [diff] [blame] | 1010 | if (info_caps) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1011 | if (ceph_snap(inode) == CEPH_NOSNAP) { |
| 1012 | ceph_add_cap(inode, session, |
| 1013 | le64_to_cpu(info->cap.cap_id), |
Yan, Zheng | 135e671 | 2020-03-05 20:21:02 +0800 | [diff] [blame] | 1014 | info_caps, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1015 | le32_to_cpu(info->cap.wanted), |
| 1016 | le32_to_cpu(info->cap.seq), |
| 1017 | le32_to_cpu(info->cap.mseq), |
| 1018 | le64_to_cpu(info->cap.realm), |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 1019 | info->cap.flags, &new_cap); |
Yan, Zheng | 2f92b3d | 2015-01-19 13:12:24 +0800 | [diff] [blame] | 1020 | |
| 1021 | /* set dir completion flag? */ |
| 1022 | if (S_ISDIR(inode->i_mode) && |
| 1023 | ci->i_files == 0 && ci->i_subdirs == 0 && |
Yan, Zheng | 2af54a7 | 2018-04-27 11:14:39 +0800 | [diff] [blame] | 1024 | (info_caps & CEPH_CAP_FILE_SHARED) && |
Yan, Zheng | 2f92b3d | 2015-01-19 13:12:24 +0800 | [diff] [blame] | 1025 | (issued & CEPH_CAP_FILE_EXCL) == 0 && |
| 1026 | !__ceph_dir_is_complete(ci)) { |
| 1027 | dout(" marking %p complete (empty)\n", inode); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1028 | i_size_write(inode, 0); |
Yan, Zheng | 2f92b3d | 2015-01-19 13:12:24 +0800 | [diff] [blame] | 1029 | __ceph_dir_set_complete(ci, |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1030 | atomic64_read(&ci->i_release_count), |
| 1031 | atomic64_read(&ci->i_ordered_count)); |
Yan, Zheng | 2f92b3d | 2015-01-19 13:12:24 +0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 1034 | wake = true; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1035 | } else { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1036 | dout(" %p got snap_caps %s\n", inode, |
Yan, Zheng | 2af54a7 | 2018-04-27 11:14:39 +0800 | [diff] [blame] | 1037 | ceph_cap_string(info_caps)); |
| 1038 | ci->i_snap_caps |= info_caps; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1041 | |
| 1042 | if (iinfo->inline_version > 0 && |
| 1043 | iinfo->inline_version >= ci->i_inline_version) { |
| 1044 | int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; |
| 1045 | ci->i_inline_version = iinfo->inline_version; |
| 1046 | if (ci->i_inline_version != CEPH_INLINE_NONE && |
Yan, Zheng | 2af54a7 | 2018-04-27 11:14:39 +0800 | [diff] [blame] | 1047 | (locked_page || (info_caps & cache_caps))) |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1048 | fill_inline = true; |
| 1049 | } |
| 1050 | |
Yan, Zheng | 719a251 | 2020-03-05 20:21:00 +0800 | [diff] [blame] | 1051 | if (cap_fmode >= 0) { |
| 1052 | if (!info_caps) |
| 1053 | pr_warn("mds issued no caps on %llx.%llx\n", |
| 1054 | ceph_vinop(inode)); |
| 1055 | __ceph_touch_fmode(ci, mdsc, cap_fmode); |
| 1056 | } |
| 1057 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1058 | spin_unlock(&ci->i_ceph_lock); |
| 1059 | |
Jeff Layton | 400e128 | 2021-12-07 08:44:50 -0500 | [diff] [blame] | 1060 | ceph_fscache_register_inode_cookie(inode); |
| 1061 | |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1062 | if (fill_inline) |
Yan, Zheng | 01deead | 2014-11-14 21:56:29 +0800 | [diff] [blame] | 1063 | ceph_fill_inline_data(inode, locked_page, |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 1064 | iinfo->inline_data, iinfo->inline_len); |
| 1065 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 1066 | if (wake) |
| 1067 | wake_up_all(&ci->i_cap_wq); |
| 1068 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1069 | /* queue truncate if we saw i_size decrease */ |
| 1070 | if (queue_trunc) |
| 1071 | ceph_queue_vmtruncate(inode); |
| 1072 | |
| 1073 | /* populate frag tree */ |
Yan, Zheng | 3e7fbe9 | 2014-04-18 16:00:30 +0800 | [diff] [blame] | 1074 | if (S_ISDIR(inode->i_mode)) |
| 1075 | ceph_fill_fragtree(inode, &info->fragtree, dirinfo); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1076 | |
| 1077 | /* update delegation info? */ |
| 1078 | if (dirinfo) |
| 1079 | ceph_fill_dirfrag(inode, dirinfo); |
| 1080 | |
| 1081 | err = 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1082 | out: |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 1083 | if (new_cap) |
| 1084 | ceph_put_cap(mdsc, new_cap); |
Luis Henriques | af8a85a | 2019-07-19 15:32:22 +0100 | [diff] [blame] | 1085 | ceph_buffer_put(old_blob); |
| 1086 | ceph_buffer_put(xattr_blob); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 1087 | ceph_put_string(pool_ns); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1088 | return err; |
| 1089 | } |
| 1090 | |
| 1091 | /* |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1092 | * caller should hold session s_mutex and dentry->d_lock. |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1093 | */ |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1094 | static void __update_dentry_lease(struct inode *dir, struct dentry *dentry, |
| 1095 | struct ceph_mds_reply_lease *lease, |
| 1096 | struct ceph_mds_session *session, |
| 1097 | unsigned long from_time, |
| 1098 | struct ceph_mds_session **old_lease_session) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1099 | { |
| 1100 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
Jeff Layton | f5e17ae | 2020-02-18 14:12:32 -0500 | [diff] [blame] | 1101 | unsigned mask = le16_to_cpu(lease->mask); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1102 | long unsigned duration = le32_to_cpu(lease->duration_ms); |
| 1103 | long unsigned ttl = from_time + (duration * HZ) / 1000; |
| 1104 | long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1105 | |
Sage Weil | 2f90b85 | 2011-07-26 11:28:25 -0700 | [diff] [blame] | 1106 | dout("update_dentry_lease %p duration %lu ms ttl %lu\n", |
| 1107 | dentry, duration, ttl); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1108 | |
Al Viro | 18fc8ab | 2016-10-28 21:52:50 -0400 | [diff] [blame] | 1109 | /* only track leases on regular dentries */ |
| 1110 | if (ceph_snap(dir) != CEPH_NOSNAP) |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1111 | return; |
Al Viro | 18fc8ab | 2016-10-28 21:52:50 -0400 | [diff] [blame] | 1112 | |
Jeff Layton | f5e17ae | 2020-02-18 14:12:32 -0500 | [diff] [blame] | 1113 | if (mask & CEPH_LEASE_PRIMARY_LINK) |
| 1114 | di->flags |= CEPH_DENTRY_PRIMARY_LINK; |
| 1115 | else |
| 1116 | di->flags &= ~CEPH_DENTRY_PRIMARY_LINK; |
| 1117 | |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 1118 | di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen); |
Jeff Layton | f5e17ae | 2020-02-18 14:12:32 -0500 | [diff] [blame] | 1119 | if (!(mask & CEPH_LEASE_VALID)) { |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 1120 | __ceph_dentry_dir_lease_touch(di); |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1121 | return; |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 1122 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1123 | |
Jeff Layton | 52d60f8 | 2021-06-04 12:03:09 -0400 | [diff] [blame] | 1124 | if (di->lease_gen == atomic_read(&session->s_cap_gen) && |
Miklos Szeredi | 9b16f03c | 2016-06-22 16:35:04 +0200 | [diff] [blame] | 1125 | time_before(ttl, di->time)) |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1126 | return; /* we already have a newer lease. */ |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1127 | |
Yan, Zheng | 481f001 | 2017-07-03 09:09:10 +0800 | [diff] [blame] | 1128 | if (di->lease_session && di->lease_session != session) { |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1129 | *old_lease_session = di->lease_session; |
Yan, Zheng | 481f001 | 2017-07-03 09:09:10 +0800 | [diff] [blame] | 1130 | di->lease_session = NULL; |
| 1131 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1132 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1133 | if (!di->lease_session) |
| 1134 | di->lease_session = ceph_get_mds_session(session); |
Jeff Layton | 52d60f8 | 2021-06-04 12:03:09 -0400 | [diff] [blame] | 1135 | di->lease_gen = atomic_read(&session->s_cap_gen); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1136 | di->lease_seq = le32_to_cpu(lease->seq); |
| 1137 | di->lease_renew_after = half_ttl; |
| 1138 | di->lease_renew_from = 0; |
Miklos Szeredi | 9b16f03c | 2016-06-22 16:35:04 +0200 | [diff] [blame] | 1139 | di->time = ttl; |
Yan, Zheng | 37c4efc | 2019-01-31 16:55:51 +0800 | [diff] [blame] | 1140 | |
| 1141 | __ceph_dentry_lease_touch(di); |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry, |
| 1145 | struct ceph_mds_reply_lease *lease, |
| 1146 | struct ceph_mds_session *session, |
| 1147 | unsigned long from_time) |
| 1148 | { |
| 1149 | struct ceph_mds_session *old_lease_session = NULL; |
| 1150 | spin_lock(&dentry->d_lock); |
| 1151 | __update_dentry_lease(dir, dentry, lease, session, from_time, |
| 1152 | &old_lease_session); |
| 1153 | spin_unlock(&dentry->d_lock); |
Jeff Layton | 7e65624 | 2021-06-09 14:09:52 -0400 | [diff] [blame] | 1154 | ceph_put_mds_session(old_lease_session); |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * update dentry lease without having parent inode locked |
| 1159 | */ |
| 1160 | static void update_dentry_lease_careful(struct dentry *dentry, |
| 1161 | struct ceph_mds_reply_lease *lease, |
| 1162 | struct ceph_mds_session *session, |
| 1163 | unsigned long from_time, |
| 1164 | char *dname, u32 dname_len, |
| 1165 | struct ceph_vino *pdvino, |
| 1166 | struct ceph_vino *ptvino) |
| 1167 | |
| 1168 | { |
| 1169 | struct inode *dir; |
| 1170 | struct ceph_mds_session *old_lease_session = NULL; |
| 1171 | |
| 1172 | spin_lock(&dentry->d_lock); |
| 1173 | /* make sure dentry's name matches target */ |
| 1174 | if (dentry->d_name.len != dname_len || |
| 1175 | memcmp(dentry->d_name.name, dname, dname_len)) |
| 1176 | goto out_unlock; |
| 1177 | |
| 1178 | dir = d_inode(dentry->d_parent); |
| 1179 | /* make sure parent matches dvino */ |
| 1180 | if (!ceph_ino_compare(dir, pdvino)) |
| 1181 | goto out_unlock; |
| 1182 | |
| 1183 | /* make sure dentry's inode matches target. NULL ptvino means that |
| 1184 | * we expect a negative dentry */ |
| 1185 | if (ptvino) { |
| 1186 | if (d_really_is_negative(dentry)) |
| 1187 | goto out_unlock; |
| 1188 | if (!ceph_ino_compare(d_inode(dentry), ptvino)) |
| 1189 | goto out_unlock; |
| 1190 | } else { |
| 1191 | if (d_really_is_positive(dentry)) |
| 1192 | goto out_unlock; |
| 1193 | } |
| 1194 | |
| 1195 | __update_dentry_lease(dir, dentry, lease, session, |
| 1196 | from_time, &old_lease_session); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1197 | out_unlock: |
| 1198 | spin_unlock(&dentry->d_lock); |
Jeff Layton | 7e65624 | 2021-06-09 14:09:52 -0400 | [diff] [blame] | 1199 | ceph_put_mds_session(old_lease_session); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /* |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1203 | * splice a dentry to an inode. |
| 1204 | * caller must hold directory i_mutex for this to be safe. |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1205 | */ |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1206 | static int splice_dentry(struct dentry **pdn, struct inode *in) |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1207 | { |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1208 | struct dentry *dn = *pdn; |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1209 | struct dentry *realdn; |
| 1210 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1211 | BUG_ON(d_inode(dn)); |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1212 | |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 1213 | if (S_ISDIR(in->i_mode)) { |
| 1214 | /* If inode is directory, d_splice_alias() below will remove |
| 1215 | * 'realdn' from its origin parent. We need to ensure that |
| 1216 | * origin parent's readdir cache will not reference 'realdn' |
| 1217 | */ |
| 1218 | realdn = d_find_any_alias(in); |
| 1219 | if (realdn) { |
| 1220 | struct ceph_dentry_info *di = ceph_dentry(realdn); |
| 1221 | spin_lock(&realdn->d_lock); |
| 1222 | |
| 1223 | realdn->d_op->d_prune(realdn); |
| 1224 | |
| 1225 | di->time = jiffies; |
| 1226 | di->lease_shared_gen = 0; |
| 1227 | di->offset = 0; |
| 1228 | |
| 1229 | spin_unlock(&realdn->d_lock); |
| 1230 | dput(realdn); |
| 1231 | } |
| 1232 | } |
| 1233 | |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1234 | /* dn must be unhashed */ |
| 1235 | if (!d_unhashed(dn)) |
| 1236 | d_drop(dn); |
Al Viro | 41d28bc | 2014-10-12 22:24:21 -0400 | [diff] [blame] | 1237 | realdn = d_splice_alias(in, dn); |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1238 | if (IS_ERR(realdn)) { |
Sage Weil | d69ed05 | 2010-06-21 10:38:14 -0700 | [diff] [blame] | 1239 | pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n", |
| 1240 | PTR_ERR(realdn), dn, in, ceph_vinop(in)); |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1241 | return PTR_ERR(realdn); |
| 1242 | } |
| 1243 | |
| 1244 | if (realdn) { |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1245 | dout("dn %p (%d) spliced with %p (%d) " |
| 1246 | "inode %p ino %llx.%llx\n", |
Al Viro | 84d08fa | 2013-07-05 18:59:33 +0400 | [diff] [blame] | 1247 | dn, d_count(dn), |
| 1248 | realdn, d_count(realdn), |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1249 | d_inode(realdn), ceph_vinop(d_inode(realdn))); |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1250 | dput(dn); |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1251 | *pdn = realdn; |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1252 | } else { |
| 1253 | BUG_ON(!ceph_dentry(dn)); |
| 1254 | dout("dn %p attached to %p ino %llx.%llx\n", |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1255 | dn, d_inode(dn), ceph_vinop(d_inode(dn))); |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1256 | } |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1257 | return 0; |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /* |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1261 | * Incorporate results into the local cache. This is either just |
| 1262 | * one inode, or a directory, dentry, and possibly linked-to inode (e.g., |
| 1263 | * after a lookup). |
| 1264 | * |
| 1265 | * A reply may contain |
| 1266 | * a directory inode along with a dentry. |
| 1267 | * and/or a target inode |
| 1268 | * |
| 1269 | * Called with snap_rwsem (read). |
| 1270 | */ |
Jeff Layton | f5a03b0 | 2017-01-31 11:06:13 -0500 | [diff] [blame] | 1271 | int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1272 | { |
Jeff Layton | f5a03b0 | 2017-01-31 11:06:13 -0500 | [diff] [blame] | 1273 | struct ceph_mds_session *session = req->r_session; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1274 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
| 1275 | struct inode *in = NULL; |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1276 | struct ceph_vino tvino, dvino; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1277 | struct ceph_fs_client *fsc = ceph_sb_to_client(sb); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1278 | int err = 0; |
| 1279 | |
| 1280 | dout("fill_trace %p is_dentry %d is_target %d\n", req, |
| 1281 | rinfo->head->is_dentry, rinfo->head->is_target); |
| 1282 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1283 | if (!rinfo->head->is_target && !rinfo->head->is_dentry) { |
| 1284 | dout("fill_trace reply is empty!\n"); |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 1285 | if (rinfo->head->result == 0 && req->r_parent) |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 1286 | ceph_invalidate_dir_request(req); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | if (rinfo->head->is_dentry) { |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 1291 | struct inode *dir = req->r_parent; |
Sage Weil | 5b1daec | 2010-01-25 11:33:08 -0800 | [diff] [blame] | 1292 | |
Sage Weil | 6c5e50f | 2012-08-21 15:55:25 -0700 | [diff] [blame] | 1293 | if (dir) { |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 1294 | err = ceph_fill_inode(dir, NULL, &rinfo->diri, |
| 1295 | rinfo->dirfrag, session, -1, |
| 1296 | &req->r_caps_reservation); |
Sage Weil | 6c5e50f | 2012-08-21 15:55:25 -0700 | [diff] [blame] | 1297 | if (err < 0) |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 1298 | goto done; |
Sage Weil | 6c5e50f | 2012-08-21 15:55:25 -0700 | [diff] [blame] | 1299 | } else { |
| 1300 | WARN_ON_ONCE(1); |
| 1301 | } |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 1302 | |
Yan, Zheng | 74c9e6b | 2018-09-28 11:34:42 +0800 | [diff] [blame] | 1303 | if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME && |
| 1304 | test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && |
| 1305 | !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 1306 | struct qstr dname; |
| 1307 | struct dentry *dn, *parent; |
| 1308 | |
| 1309 | BUG_ON(!rinfo->head->is_target); |
| 1310 | BUG_ON(req->r_dentry); |
| 1311 | |
| 1312 | parent = d_find_any_alias(dir); |
| 1313 | BUG_ON(!parent); |
| 1314 | |
| 1315 | dname.name = rinfo->dname; |
| 1316 | dname.len = rinfo->dname_len; |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1317 | dname.hash = full_name_hash(parent, dname.name, dname.len); |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1318 | tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); |
| 1319 | tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 1320 | retry_lookup: |
| 1321 | dn = d_lookup(parent, &dname); |
| 1322 | dout("d_lookup on parent=%p name=%.*s got %p\n", |
| 1323 | parent, dname.len, dname.name, dn); |
| 1324 | |
| 1325 | if (!dn) { |
| 1326 | dn = d_alloc(parent, &dname); |
| 1327 | dout("d_alloc %p '%.*s' = %p\n", parent, |
| 1328 | dname.len, dname.name, dn); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1329 | if (!dn) { |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 1330 | dput(parent); |
| 1331 | err = -ENOMEM; |
| 1332 | goto done; |
| 1333 | } |
Al Viro | ad5cb12 | 2016-10-28 22:05:13 -0400 | [diff] [blame] | 1334 | err = 0; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1335 | } else if (d_really_is_positive(dn) && |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1336 | (ceph_ino(d_inode(dn)) != tvino.ino || |
| 1337 | ceph_snap(d_inode(dn)) != tvino.snap)) { |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 1338 | dout(" dn %p points to wrong inode %p\n", |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1339 | dn, d_inode(dn)); |
Yan, Zheng | 933ad2c9 | 2017-10-10 17:06:25 +0800 | [diff] [blame] | 1340 | ceph_dir_clear_ordered(dir); |
Yan, Zheng | 19913b4 | 2014-03-06 16:40:32 +0800 | [diff] [blame] | 1341 | d_delete(dn); |
| 1342 | dput(dn); |
| 1343 | goto retry_lookup; |
| 1344 | } |
| 1345 | |
| 1346 | req->r_dentry = dn; |
| 1347 | dput(parent); |
| 1348 | } |
Sage Weil | 5b1daec | 2010-01-25 11:33:08 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1351 | if (rinfo->head->is_target) { |
Jeff Layton | bca9fc1 | 2020-11-12 10:03:38 -0500 | [diff] [blame] | 1352 | /* Should be filled in by handle_reply */ |
| 1353 | BUG_ON(!req->r_target_inode); |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1354 | |
Jeff Layton | bca9fc1 | 2020-11-12 10:03:38 -0500 | [diff] [blame] | 1355 | in = req->r_target_inode; |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 1356 | err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti, |
| 1357 | NULL, session, |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 1358 | (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && |
Jeff Layton | 3bb48b4 | 2019-12-02 13:47:57 -0500 | [diff] [blame] | 1359 | !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && |
Jeff Layton | 57c2199 | 2019-12-04 15:28:17 -0500 | [diff] [blame] | 1360 | rinfo->head->result == 0) ? req->r_fmode : -1, |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1361 | &req->r_caps_reservation); |
| 1362 | if (err < 0) { |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 1363 | pr_err("ceph_fill_inode badness %p %llx.%llx\n", |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1364 | in, ceph_vinop(in)); |
Jeff Layton | bca9fc1 | 2020-11-12 10:03:38 -0500 | [diff] [blame] | 1365 | req->r_target_inode = NULL; |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 1366 | if (in->i_state & I_NEW) |
| 1367 | discard_new_inode(in); |
Jeff Layton | 68cbb80 | 2020-11-12 09:37:59 -0500 | [diff] [blame] | 1368 | else |
| 1369 | iput(in); |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1370 | goto done; |
| 1371 | } |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 1372 | if (in->i_state & I_NEW) |
| 1373 | unlock_new_inode(in); |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1374 | } |
| 1375 | |
Sage Weil | 9358c6d | 2010-03-30 13:54:41 -0700 | [diff] [blame] | 1376 | /* |
| 1377 | * ignore null lease/binding on snapdir ENOENT, or else we |
| 1378 | * will have trouble splicing in the virtual snapdir later |
| 1379 | */ |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 1380 | if (rinfo->head->is_dentry && |
| 1381 | !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && |
| 1382 | test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && |
Sage Weil | 9358c6d | 2010-03-30 13:54:41 -0700 | [diff] [blame] | 1383 | (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name, |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1384 | fsc->mount_options->snapdir_name, |
Sage Weil | 9358c6d | 2010-03-30 13:54:41 -0700 | [diff] [blame] | 1385 | req->r_dentry->d_name.len))) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1386 | /* |
| 1387 | * lookup link rename : null -> possibly existing inode |
| 1388 | * mknod symlink mkdir : null -> new inode |
| 1389 | * unlink : linked -> null |
| 1390 | */ |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 1391 | struct inode *dir = req->r_parent; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1392 | struct dentry *dn = req->r_dentry; |
| 1393 | bool have_dir_cap, have_lease; |
| 1394 | |
| 1395 | BUG_ON(!dn); |
| 1396 | BUG_ON(!dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1397 | BUG_ON(d_inode(dn->d_parent) != dir); |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1398 | |
| 1399 | dvino.ino = le64_to_cpu(rinfo->diri.in->ino); |
| 1400 | dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); |
| 1401 | |
| 1402 | BUG_ON(ceph_ino(dir) != dvino.ino); |
| 1403 | BUG_ON(ceph_snap(dir) != dvino.snap); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1404 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1405 | /* do we have a lease on the whole dir? */ |
| 1406 | have_dir_cap = |
| 1407 | (le32_to_cpu(rinfo->diri.in->cap.caps) & |
| 1408 | CEPH_CAP_FILE_SHARED); |
| 1409 | |
| 1410 | /* do we have a dn lease? */ |
| 1411 | have_lease = have_dir_cap || |
Sage Weil | 2f90b85 | 2011-07-26 11:28:25 -0700 | [diff] [blame] | 1412 | le32_to_cpu(rinfo->dlease->duration_ms); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1413 | if (!have_lease) |
| 1414 | dout("fill_trace no dentry lease or dir cap\n"); |
| 1415 | |
| 1416 | /* rename? */ |
| 1417 | if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) { |
Yan, Zheng | 0a8a70f | 2014-04-14 13:13:02 +0800 | [diff] [blame] | 1418 | struct inode *olddir = req->r_old_dentry_dir; |
| 1419 | BUG_ON(!olddir); |
| 1420 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1421 | dout(" src %p '%pd' dst %p '%pd'\n", |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1422 | req->r_old_dentry, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1423 | req->r_old_dentry, |
| 1424 | dn, dn); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1425 | dout("fill_trace doing d_move %p -> %p\n", |
| 1426 | req->r_old_dentry, dn); |
Sage Weil | c10f5e1 | 2010-04-16 12:56:11 -0700 | [diff] [blame] | 1427 | |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1428 | /* d_move screws up sibling dentries' offsets */ |
| 1429 | ceph_dir_clear_ordered(dir); |
| 1430 | ceph_dir_clear_ordered(olddir); |
| 1431 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1432 | d_move(req->r_old_dentry, dn); |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1433 | dout(" src %p '%pd' dst %p '%pd'\n", |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1434 | req->r_old_dentry, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1435 | req->r_old_dentry, |
| 1436 | dn, dn); |
Sage Weil | 81a6cf2 | 2010-05-14 09:35:38 -0700 | [diff] [blame] | 1437 | |
Sage Weil | c4a29f2 | 2009-12-21 11:42:18 -0800 | [diff] [blame] | 1438 | /* ensure target dentry is invalidated, despite |
| 1439 | rehashing bug in vfs_rename_dir */ |
Sage Weil | 81a6cf2 | 2010-05-14 09:35:38 -0700 | [diff] [blame] | 1440 | ceph_invalidate_dentry_lease(dn); |
| 1441 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 1442 | dout("dn %p gets new offset %lld\n", req->r_old_dentry, |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 1443 | ceph_dentry(req->r_old_dentry)->offset); |
Sage Weil | 81a6cf2 | 2010-05-14 09:35:38 -0700 | [diff] [blame] | 1444 | |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1445 | /* swap r_dentry and r_old_dentry in case that |
| 1446 | * splice_dentry() gets called later. This is safe |
| 1447 | * because no other place will use them */ |
| 1448 | req->r_dentry = req->r_old_dentry; |
| 1449 | req->r_old_dentry = dn; |
| 1450 | dn = req->r_dentry; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /* null dentry? */ |
| 1454 | if (!rinfo->head->is_target) { |
| 1455 | dout("fill_trace null dentry\n"); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1456 | if (d_really_is_positive(dn)) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1457 | dout("d_delete %p\n", dn); |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 1458 | ceph_dir_clear_ordered(dir); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1459 | d_delete(dn); |
Jeff Layton | 80d025f | 2017-01-26 16:14:18 -0500 | [diff] [blame] | 1460 | } else if (have_lease) { |
| 1461 | if (d_unhashed(dn)) |
Al Viro | f8b3171 | 2016-03-07 15:11:43 -0500 | [diff] [blame] | 1462 | d_add(dn, NULL); |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1463 | update_dentry_lease(dir, dn, |
| 1464 | rinfo->dlease, session, |
| 1465 | req->r_request_started); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1466 | } |
| 1467 | goto done; |
| 1468 | } |
| 1469 | |
| 1470 | /* attach proper inode */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1471 | if (d_really_is_negative(dn)) { |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 1472 | ceph_dir_clear_ordered(dir); |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1473 | ihold(in); |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1474 | err = splice_dentry(&req->r_dentry, in); |
| 1475 | if (err < 0) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1476 | goto done; |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1477 | dn = req->r_dentry; /* may have spliced */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1478 | } else if (d_really_is_positive(dn) && d_inode(dn) != in) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1479 | dout(" %p links to %p %llx.%llx, not %llx.%llx\n", |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1480 | dn, d_inode(dn), ceph_vinop(d_inode(dn)), |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1481 | ceph_vinop(in)); |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1482 | d_invalidate(dn); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1483 | have_lease = false; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1486 | if (have_lease) { |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1487 | update_dentry_lease(dir, dn, |
| 1488 | rinfo->dlease, session, |
| 1489 | req->r_request_started); |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1490 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1491 | dout(" final dn %p\n", dn); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 1492 | } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP || |
| 1493 | req->r_op == CEPH_MDS_OP_MKSNAP) && |
Al Viro | 1f08529 | 2019-10-29 13:53:29 +0000 | [diff] [blame] | 1494 | test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 1495 | !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 1496 | struct inode *dir = req->r_parent; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1497 | |
| 1498 | /* fill out a snapdir LOOKUPSNAP dentry */ |
Yan, Zheng | 0a8a70f | 2014-04-14 13:13:02 +0800 | [diff] [blame] | 1499 | BUG_ON(!dir); |
| 1500 | BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR); |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1501 | BUG_ON(!req->r_dentry); |
| 1502 | dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry); |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 1503 | ceph_dir_clear_ordered(dir); |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1504 | ihold(in); |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1505 | err = splice_dentry(&req->r_dentry, in); |
| 1506 | if (err < 0) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1507 | goto done; |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1508 | } else if (rinfo->head->is_dentry && req->r_dentry) { |
| 1509 | /* parent inode is not locked, be carefull */ |
Jeff Layton | cdde7c4 | 2017-01-27 13:07:10 -0500 | [diff] [blame] | 1510 | struct ceph_vino *ptvino = NULL; |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1511 | dvino.ino = le64_to_cpu(rinfo->diri.in->ino); |
| 1512 | dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); |
| 1513 | if (rinfo->head->is_target) { |
| 1514 | tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); |
| 1515 | tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); |
| 1516 | ptvino = &tvino; |
Jeff Layton | cdde7c4 | 2017-01-27 13:07:10 -0500 | [diff] [blame] | 1517 | } |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1518 | update_dentry_lease_careful(req->r_dentry, rinfo->dlease, |
| 1519 | session, req->r_request_started, |
| 1520 | rinfo->dname, rinfo->dname_len, |
| 1521 | &dvino, ptvino); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1522 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1523 | done: |
| 1524 | dout("fill_trace done err=%d\n", err); |
| 1525 | return err; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * Prepopulate our cache with readdir results, leases, etc. |
| 1530 | */ |
Sage Weil | 79f9f99 | 2013-01-29 02:55:31 -0500 | [diff] [blame] | 1531 | static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req, |
| 1532 | struct ceph_mds_session *session) |
| 1533 | { |
| 1534 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
| 1535 | int i, err = 0; |
| 1536 | |
| 1537 | for (i = 0; i < rinfo->dir_nr; i++) { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 1538 | struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; |
Sage Weil | 79f9f99 | 2013-01-29 02:55:31 -0500 | [diff] [blame] | 1539 | struct ceph_vino vino; |
| 1540 | struct inode *in; |
| 1541 | int rc; |
| 1542 | |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 1543 | vino.ino = le64_to_cpu(rde->inode.in->ino); |
| 1544 | vino.snap = le64_to_cpu(rde->inode.in->snapid); |
Sage Weil | 79f9f99 | 2013-01-29 02:55:31 -0500 | [diff] [blame] | 1545 | |
| 1546 | in = ceph_get_inode(req->r_dentry->d_sb, vino); |
| 1547 | if (IS_ERR(in)) { |
| 1548 | err = PTR_ERR(in); |
| 1549 | dout("new_inode badness got %d\n", err); |
| 1550 | continue; |
| 1551 | } |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 1552 | rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, |
| 1553 | -1, &req->r_caps_reservation); |
Sage Weil | 79f9f99 | 2013-01-29 02:55:31 -0500 | [diff] [blame] | 1554 | if (rc < 0) { |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 1555 | pr_err("ceph_fill_inode badness on %p got %d\n", |
| 1556 | in, rc); |
Sage Weil | 79f9f99 | 2013-01-29 02:55:31 -0500 | [diff] [blame] | 1557 | err = rc; |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 1558 | if (in->i_state & I_NEW) { |
| 1559 | ihold(in); |
| 1560 | discard_new_inode(in); |
| 1561 | } |
| 1562 | } else if (in->i_state & I_NEW) { |
| 1563 | unlock_new_inode(in); |
Sage Weil | 79f9f99 | 2013-01-29 02:55:31 -0500 | [diff] [blame] | 1564 | } |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 1565 | |
Jeff Layton | 23c2c76 | 2021-06-04 12:03:09 -0400 | [diff] [blame] | 1566 | iput(in); |
Sage Weil | 79f9f99 | 2013-01-29 02:55:31 -0500 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | return err; |
| 1570 | } |
| 1571 | |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1572 | void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl) |
| 1573 | { |
| 1574 | if (ctl->page) { |
| 1575 | kunmap(ctl->page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1576 | put_page(ctl->page); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1577 | ctl->page = NULL; |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | static int fill_readdir_cache(struct inode *dir, struct dentry *dn, |
| 1582 | struct ceph_readdir_cache_control *ctl, |
| 1583 | struct ceph_mds_request *req) |
| 1584 | { |
| 1585 | struct ceph_inode_info *ci = ceph_inode(dir); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1586 | unsigned nsize = PAGE_SIZE / sizeof(struct dentry*); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1587 | unsigned idx = ctl->index % nsize; |
| 1588 | pgoff_t pgoff = ctl->index / nsize; |
| 1589 | |
| 1590 | if (!ctl->page || pgoff != page_index(ctl->page)) { |
| 1591 | ceph_readdir_cache_release(ctl); |
Yan, Zheng | af5e5eb | 2016-02-26 16:27:13 +0800 | [diff] [blame] | 1592 | if (idx == 0) |
| 1593 | ctl->page = grab_cache_page(&dir->i_data, pgoff); |
| 1594 | else |
| 1595 | ctl->page = find_lock_page(&dir->i_data, pgoff); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1596 | if (!ctl->page) { |
| 1597 | ctl->index = -1; |
Yan, Zheng | af5e5eb | 2016-02-26 16:27:13 +0800 | [diff] [blame] | 1598 | return idx == 0 ? -ENOMEM : 0; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1599 | } |
| 1600 | /* reading/filling the cache are serialized by |
| 1601 | * i_mutex, no need to use page lock */ |
| 1602 | unlock_page(ctl->page); |
| 1603 | ctl->dentries = kmap(ctl->page); |
Yan, Zheng | af5e5eb | 2016-02-26 16:27:13 +0800 | [diff] [blame] | 1604 | if (idx == 0) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1605 | memset(ctl->dentries, 0, PAGE_SIZE); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) && |
| 1609 | req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) { |
| 1610 | dout("readdir cache dn %p idx %d\n", dn, ctl->index); |
| 1611 | ctl->dentries[idx] = dn; |
| 1612 | ctl->index++; |
| 1613 | } else { |
| 1614 | dout("disable readdir cache\n"); |
| 1615 | ctl->index = -1; |
| 1616 | } |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1620 | int ceph_readdir_prepopulate(struct ceph_mds_request *req, |
| 1621 | struct ceph_mds_session *session) |
| 1622 | { |
| 1623 | struct dentry *parent = req->r_dentry; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 1624 | struct ceph_inode_info *ci = ceph_inode(d_inode(parent)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1625 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
| 1626 | struct qstr dname; |
| 1627 | struct dentry *dn; |
| 1628 | struct inode *in; |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1629 | int err = 0, skipped = 0, ret, i; |
Jeff Layton | 0f51a98 | 2020-12-09 10:27:01 -0500 | [diff] [blame] | 1630 | u32 frag = le32_to_cpu(req->r_args.readdir.frag); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 1631 | u32 last_hash = 0; |
| 1632 | u32 fpos_offset; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1633 | struct ceph_readdir_cache_control cache_ctl = {}; |
| 1634 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 1635 | if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1636 | return readdir_prepopulate_inodes_only(req, session); |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 1637 | |
Yan, Zheng | 7916254 | 2017-04-05 12:54:05 -0400 | [diff] [blame] | 1638 | if (rinfo->hash_order) { |
| 1639 | if (req->r_path2) { |
| 1640 | last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, |
| 1641 | req->r_path2, |
| 1642 | strlen(req->r_path2)); |
| 1643 | last_hash = ceph_frag_value(last_hash); |
| 1644 | } else if (rinfo->offset_hash) { |
| 1645 | /* mds understands offset_hash */ |
| 1646 | WARN_ON_ONCE(req->r_readdir_offset != 2); |
Jeff Layton | 0f51a98 | 2020-12-09 10:27:01 -0500 | [diff] [blame] | 1647 | last_hash = le32_to_cpu(req->r_args.readdir.offset_hash); |
Yan, Zheng | 7916254 | 2017-04-05 12:54:05 -0400 | [diff] [blame] | 1648 | } |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 1649 | } |
| 1650 | |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 1651 | if (rinfo->dir_dir && |
| 1652 | le32_to_cpu(rinfo->dir_dir->frag) != frag) { |
| 1653 | dout("readdir_prepopulate got new frag %x -> %x\n", |
| 1654 | frag, le32_to_cpu(rinfo->dir_dir->frag)); |
| 1655 | frag = le32_to_cpu(rinfo->dir_dir->frag); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 1656 | if (!rinfo->hash_order) |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1657 | req->r_readdir_offset = 2; |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 1658 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1659 | |
| 1660 | if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1661 | dout("readdir_prepopulate %d items under SNAPDIR dn %p\n", |
| 1662 | rinfo->dir_nr, parent); |
| 1663 | } else { |
| 1664 | dout("readdir_prepopulate %d items under dn %p\n", |
| 1665 | rinfo->dir_nr, parent); |
| 1666 | if (rinfo->dir_dir) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1667 | ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1668 | |
Yan, Zheng | 8d45b91 | 2017-07-26 12:07:51 +0800 | [diff] [blame] | 1669 | if (ceph_frag_is_leftmost(frag) && |
| 1670 | req->r_readdir_offset == 2 && |
| 1671 | !(rinfo->hash_order && last_hash)) { |
| 1672 | /* note dir version at start of readdir so we can |
| 1673 | * tell if any dentries get dropped */ |
| 1674 | req->r_dir_release_cnt = |
| 1675 | atomic64_read(&ci->i_release_count); |
| 1676 | req->r_dir_ordered_cnt = |
| 1677 | atomic64_read(&ci->i_ordered_count); |
| 1678 | req->r_readdir_cache_idx = 0; |
| 1679 | } |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | cache_ctl.index = req->r_readdir_cache_idx; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 1683 | fpos_offset = req->r_readdir_offset; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1684 | |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1685 | /* FIXME: release caps/leases if error occurs */ |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1686 | for (i = 0; i < rinfo->dir_nr; i++) { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 1687 | struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1688 | struct ceph_vino tvino; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1689 | |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 1690 | dname.name = rde->name; |
| 1691 | dname.len = rde->name_len; |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1692 | dname.hash = full_name_hash(parent, dname.name, dname.len); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1693 | |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1694 | tvino.ino = le64_to_cpu(rde->inode.in->ino); |
| 1695 | tvino.snap = le64_to_cpu(rde->inode.in->snapid); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1696 | |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 1697 | if (rinfo->hash_order) { |
| 1698 | u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, |
| 1699 | rde->name, rde->name_len); |
| 1700 | hash = ceph_frag_value(hash); |
| 1701 | if (hash != last_hash) |
| 1702 | fpos_offset = 2; |
| 1703 | last_hash = hash; |
| 1704 | rde->offset = ceph_make_fpos(hash, fpos_offset++, true); |
| 1705 | } else { |
| 1706 | rde->offset = ceph_make_fpos(frag, fpos_offset++, false); |
| 1707 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1708 | |
| 1709 | retry_lookup: |
| 1710 | dn = d_lookup(parent, &dname); |
| 1711 | dout("d_lookup on parent=%p name=%.*s got %p\n", |
| 1712 | parent, dname.len, dname.name, dn); |
| 1713 | |
| 1714 | if (!dn) { |
| 1715 | dn = d_alloc(parent, &dname); |
| 1716 | dout("d_alloc %p '%.*s' = %p\n", parent, |
| 1717 | dname.len, dname.name, dn); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1718 | if (!dn) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1719 | dout("d_alloc badness\n"); |
| 1720 | err = -ENOMEM; |
| 1721 | goto out; |
| 1722 | } |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1723 | } else if (d_really_is_positive(dn) && |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1724 | (ceph_ino(d_inode(dn)) != tvino.ino || |
| 1725 | ceph_snap(d_inode(dn)) != tvino.snap)) { |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 1726 | struct ceph_dentry_info *di = ceph_dentry(dn); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1727 | dout(" dn %p points to wrong inode %p\n", |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1728 | dn, d_inode(dn)); |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 1729 | |
| 1730 | spin_lock(&dn->d_lock); |
| 1731 | if (di->offset > 0 && |
| 1732 | di->lease_shared_gen == |
| 1733 | atomic_read(&ci->i_shared_gen)) { |
| 1734 | __ceph_dir_clear_ordered(ci); |
| 1735 | di->offset = 0; |
| 1736 | } |
| 1737 | spin_unlock(&dn->d_lock); |
| 1738 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1739 | d_delete(dn); |
| 1740 | dput(dn); |
| 1741 | goto retry_lookup; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1744 | /* inode */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1745 | if (d_really_is_positive(dn)) { |
| 1746 | in = d_inode(dn); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1747 | } else { |
Jeff Layton | f5d55f0 | 2017-01-27 09:13:57 -0500 | [diff] [blame] | 1748 | in = ceph_get_inode(parent->d_sb, tvino); |
Dan Carpenter | ac1f12e | 2010-08-25 09:11:35 +0200 | [diff] [blame] | 1749 | if (IS_ERR(in)) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1750 | dout("new_inode badness\n"); |
Al Viro | 2744c17 | 2012-09-26 21:41:05 -0400 | [diff] [blame] | 1751 | d_drop(dn); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1752 | dput(dn); |
Dan Carpenter | ac1f12e | 2010-08-25 09:11:35 +0200 | [diff] [blame] | 1753 | err = PTR_ERR(in); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1754 | goto out; |
| 1755 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 1758 | ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, |
| 1759 | -1, &req->r_caps_reservation); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1760 | if (ret < 0) { |
Jeff Layton | 966c716 | 2019-12-05 09:09:25 -0500 | [diff] [blame] | 1761 | pr_err("ceph_fill_inode badness on %p\n", in); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 1762 | if (d_really_is_negative(dn)) { |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 1763 | if (in->i_state & I_NEW) { |
| 1764 | ihold(in); |
| 1765 | discard_new_inode(in); |
| 1766 | } |
Jeff Layton | 23c2c76 | 2021-06-04 12:03:09 -0400 | [diff] [blame] | 1767 | iput(in); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 1768 | } |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1769 | d_drop(dn); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1770 | err = ret; |
Sage Weil | d69ed05 | 2010-06-21 10:38:14 -0700 | [diff] [blame] | 1771 | goto next_item; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1772 | } |
Jeff Layton | 893e456 | 2019-12-11 15:21:24 -0500 | [diff] [blame] | 1773 | if (in->i_state & I_NEW) |
| 1774 | unlock_new_inode(in); |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1775 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1776 | if (d_really_is_negative(dn)) { |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1777 | if (ceph_security_xattr_deadlock(in)) { |
| 1778 | dout(" skip splicing dn %p to inode %p" |
| 1779 | " (security xattr deadlock)\n", dn, in); |
Jeff Layton | 23c2c76 | 2021-06-04 12:03:09 -0400 | [diff] [blame] | 1780 | iput(in); |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1781 | skipped++; |
| 1782 | goto next_item; |
| 1783 | } |
| 1784 | |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1785 | err = splice_dentry(&dn, in); |
| 1786 | if (err < 0) |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1787 | goto next_item; |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1788 | } |
| 1789 | |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 1790 | ceph_dentry(dn)->offset = rde->offset; |
Yan, Zheng | 86b58d1 | 2013-12-05 12:38:59 +0800 | [diff] [blame] | 1791 | |
Yan, Zheng | 543212b | 2019-05-22 21:49:44 +0800 | [diff] [blame] | 1792 | update_dentry_lease(d_inode(parent), dn, |
| 1793 | rde->lease, req->r_session, |
| 1794 | req->r_request_started); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1795 | |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1796 | if (err == 0 && skipped == 0 && cache_ctl.index >= 0) { |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1797 | ret = fill_readdir_cache(d_inode(parent), dn, |
| 1798 | &cache_ctl, req); |
| 1799 | if (ret < 0) |
| 1800 | err = ret; |
| 1801 | } |
Sage Weil | d69ed05 | 2010-06-21 10:38:14 -0700 | [diff] [blame] | 1802 | next_item: |
Yan, Zheng | 2bf996a | 2018-10-25 17:30:30 +0800 | [diff] [blame] | 1803 | dput(dn); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1804 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1805 | out: |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 1806 | if (err == 0 && skipped == 0) { |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 1807 | set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1808 | req->r_readdir_cache_idx = cache_ctl.index; |
| 1809 | } |
| 1810 | ceph_readdir_cache_release(&cache_ctl); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1811 | dout("readdir_prepopulate done\n"); |
| 1812 | return err; |
| 1813 | } |
| 1814 | |
Yan, Zheng | efb0ca7 | 2017-05-22 12:03:32 +0800 | [diff] [blame] | 1815 | bool ceph_inode_set_size(struct inode *inode, loff_t size) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1816 | { |
| 1817 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | efb0ca7 | 2017-05-22 12:03:32 +0800 | [diff] [blame] | 1818 | bool ret; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1819 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1820 | spin_lock(&ci->i_ceph_lock); |
Jeff Layton | 2d6795f | 2021-04-09 15:58:35 -0400 | [diff] [blame] | 1821 | dout("set_size %p %llu -> %llu\n", inode, i_size_read(inode), size); |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 1822 | i_size_write(inode, size); |
Jeff Layton | 400e128 | 2021-12-07 08:44:50 -0500 | [diff] [blame] | 1823 | ceph_fscache_update(inode); |
Yan, Zheng | 224a7542 | 2016-05-05 16:40:17 +0800 | [diff] [blame] | 1824 | inode->i_blocks = calc_inode_blocks(size); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1825 | |
Yan, Zheng | efb0ca7 | 2017-05-22 12:03:32 +0800 | [diff] [blame] | 1826 | ret = __ceph_should_report_size(ci); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1827 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1828 | spin_unlock(&ci->i_ceph_lock); |
Jeff Layton | 400e128 | 2021-12-07 08:44:50 -0500 | [diff] [blame] | 1829 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1830 | return ret; |
| 1831 | } |
| 1832 | |
Jeff Layton | 64f28c6 | 2020-10-09 14:24:34 -0400 | [diff] [blame] | 1833 | void ceph_queue_inode_work(struct inode *inode, int work_bit) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 1834 | { |
Jeff Layton | 64f28c6 | 2020-10-09 14:24:34 -0400 | [diff] [blame] | 1835 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Yan, Zheng | 1cf89a8 | 2019-05-18 11:18:44 +0800 | [diff] [blame] | 1836 | struct ceph_inode_info *ci = ceph_inode(inode); |
Jeff Layton | 64f28c6 | 2020-10-09 14:24:34 -0400 | [diff] [blame] | 1837 | set_bit(work_bit, &ci->i_work_mask); |
Yan, Zheng | 1cf89a8 | 2019-05-18 11:18:44 +0800 | [diff] [blame] | 1838 | |
Sage Weil | 15a2015 | 2011-11-05 22:06:31 -0700 | [diff] [blame] | 1839 | ihold(inode); |
Jeff Layton | 64f28c6 | 2020-10-09 14:24:34 -0400 | [diff] [blame] | 1840 | if (queue_work(fsc->inode_wq, &ci->i_work)) { |
| 1841 | dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask); |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 1842 | } else { |
Jeff Layton | 64f28c6 | 2020-10-09 14:24:34 -0400 | [diff] [blame] | 1843 | dout("queue_inode_work %p already queued, mask=%lx\n", |
Yan, Zheng | 1cf89a8 | 2019-05-18 11:18:44 +0800 | [diff] [blame] | 1844 | inode, ci->i_work_mask); |
| 1845 | iput(inode); |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | static void ceph_do_invalidate_pages(struct inode *inode) |
| 1850 | { |
| 1851 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1852 | u32 orig_gen; |
| 1853 | int check = 0; |
| 1854 | |
Jeff Layton | 400e128 | 2021-12-07 08:44:50 -0500 | [diff] [blame] | 1855 | ceph_fscache_invalidate(inode, false); |
| 1856 | |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 1857 | mutex_lock(&ci->i_truncate_mutex); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1858 | |
Jeff Layton | 5d6451b | 2021-08-31 13:39:13 -0400 | [diff] [blame] | 1859 | if (ceph_inode_is_shutdown(inode)) { |
Jeff Layton | 6407fbb | 2021-09-02 09:12:35 -0400 | [diff] [blame] | 1860 | pr_warn_ratelimited("%s: inode %llx.%llx is shut down\n", |
| 1861 | __func__, ceph_vinop(inode)); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1862 | mapping_set_error(inode->i_mapping, -EIO); |
| 1863 | truncate_pagecache(inode, 0); |
| 1864 | mutex_unlock(&ci->i_truncate_mutex); |
| 1865 | goto out; |
| 1866 | } |
| 1867 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1868 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1869 | dout("invalidate_pages %p gen %d revoking %d\n", inode, |
| 1870 | ci->i_rdcache_gen, ci->i_rdcache_revoking); |
Sage Weil | cd045cb | 2010-11-04 11:05:05 -0700 | [diff] [blame] | 1871 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { |
Yan, Zheng | 9563f88 | 2013-11-22 13:50:45 +0800 | [diff] [blame] | 1872 | if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) |
| 1873 | check = 1; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1874 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 1875 | mutex_unlock(&ci->i_truncate_mutex); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1876 | goto out; |
| 1877 | } |
| 1878 | orig_gen = ci->i_rdcache_gen; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1879 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1880 | |
Jeff Layton | 400e128 | 2021-12-07 08:44:50 -0500 | [diff] [blame] | 1881 | ceph_fscache_invalidate(inode, false); |
Yan, Zheng | 9abd4db | 2016-05-18 20:58:26 +0800 | [diff] [blame] | 1882 | if (invalidate_inode_pages2(inode->i_mapping) < 0) { |
Jeff Layton | 6407fbb | 2021-09-02 09:12:35 -0400 | [diff] [blame] | 1883 | pr_err("invalidate_inode_pages2 %llx.%llx failed\n", |
| 1884 | ceph_vinop(inode)); |
Yan, Zheng | 9abd4db | 2016-05-18 20:58:26 +0800 | [diff] [blame] | 1885 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1886 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1887 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | cd045cb | 2010-11-04 11:05:05 -0700 | [diff] [blame] | 1888 | if (orig_gen == ci->i_rdcache_gen && |
| 1889 | orig_gen == ci->i_rdcache_revoking) { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1890 | dout("invalidate_pages %p gen %d successful\n", inode, |
| 1891 | ci->i_rdcache_gen); |
Sage Weil | cd045cb | 2010-11-04 11:05:05 -0700 | [diff] [blame] | 1892 | ci->i_rdcache_revoking--; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1893 | check = 1; |
| 1894 | } else { |
Sage Weil | cd045cb | 2010-11-04 11:05:05 -0700 | [diff] [blame] | 1895 | dout("invalidate_pages %p gen %d raced, now %d revoking %d\n", |
| 1896 | inode, orig_gen, ci->i_rdcache_gen, |
| 1897 | ci->i_rdcache_revoking); |
Yan, Zheng | 9563f88 | 2013-11-22 13:50:45 +0800 | [diff] [blame] | 1898 | if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) |
| 1899 | check = 1; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1900 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1901 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 1902 | mutex_unlock(&ci->i_truncate_mutex); |
Yan, Zheng | 9563f88 | 2013-11-22 13:50:45 +0800 | [diff] [blame] | 1903 | out: |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1904 | if (check) |
| 1905 | ceph_check_caps(ci, 0, NULL); |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /* |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1909 | * Make sure any pending truncation is applied before doing anything |
| 1910 | * that may depend on it. |
| 1911 | */ |
Yan, Zheng | b415bf4 | 2013-07-02 12:40:19 +0800 | [diff] [blame] | 1912 | void __ceph_do_pending_vmtruncate(struct inode *inode) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1913 | { |
| 1914 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1915 | u64 to; |
Yan, Zheng | a85f50b | 2012-11-19 10:49:08 +0800 | [diff] [blame] | 1916 | int wrbuffer_refs, finish = 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1917 | |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 1918 | mutex_lock(&ci->i_truncate_mutex); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1919 | retry: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1920 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1921 | if (ci->i_truncate_pending == 0) { |
| 1922 | dout("__do_pending_vmtruncate %p none pending\n", inode); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1923 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 1924 | mutex_unlock(&ci->i_truncate_mutex); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1925 | return; |
| 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * make sure any dirty snapped pages are flushed before we |
| 1930 | * possibly truncate them.. so write AND block! |
| 1931 | */ |
| 1932 | if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) { |
Yan, Zheng | c8fd0d37 | 2017-08-28 15:41:28 +0800 | [diff] [blame] | 1933 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1934 | dout("__do_pending_vmtruncate %p flushing snaps first\n", |
| 1935 | inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1936 | filemap_write_and_wait_range(&inode->i_data, 0, |
| 1937 | inode->i_sb->s_maxbytes); |
| 1938 | goto retry; |
| 1939 | } |
| 1940 | |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 1941 | /* there should be no reader or writer */ |
| 1942 | WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref); |
| 1943 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1944 | to = ci->i_truncate_size; |
| 1945 | wrbuffer_refs = ci->i_wrbuffer_ref; |
| 1946 | dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode, |
| 1947 | ci->i_truncate_pending, to); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1948 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1949 | |
Jeff Layton | 400e128 | 2021-12-07 08:44:50 -0500 | [diff] [blame] | 1950 | ceph_fscache_resize(inode, to); |
Yan, Zheng | 4e217b5 | 2014-06-08 05:08:56 +0800 | [diff] [blame] | 1951 | truncate_pagecache(inode, to); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1952 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1953 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | a85f50b | 2012-11-19 10:49:08 +0800 | [diff] [blame] | 1954 | if (to == ci->i_truncate_size) { |
| 1955 | ci->i_truncate_pending = 0; |
| 1956 | finish = 1; |
| 1957 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1958 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | a85f50b | 2012-11-19 10:49:08 +0800 | [diff] [blame] | 1959 | if (!finish) |
| 1960 | goto retry; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1961 | |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 1962 | mutex_unlock(&ci->i_truncate_mutex); |
| 1963 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1964 | if (wrbuffer_refs == 0) |
Yan, Zheng | bf73c62 | 2020-03-05 20:21:04 +0800 | [diff] [blame] | 1965 | ceph_check_caps(ci, 0, NULL); |
Yan, Zheng | a85f50b | 2012-11-19 10:49:08 +0800 | [diff] [blame] | 1966 | |
| 1967 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
Yan, Zheng | 1cf89a8 | 2019-05-18 11:18:44 +0800 | [diff] [blame] | 1970 | static void ceph_inode_work(struct work_struct *work) |
| 1971 | { |
| 1972 | struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, |
| 1973 | i_work); |
| 1974 | struct inode *inode = &ci->vfs_inode; |
| 1975 | |
| 1976 | if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) { |
| 1977 | dout("writeback %p\n", inode); |
| 1978 | filemap_fdatawrite(&inode->i_data); |
| 1979 | } |
| 1980 | if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask)) |
| 1981 | ceph_do_invalidate_pages(inode); |
| 1982 | |
| 1983 | if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask)) |
| 1984 | __ceph_do_pending_vmtruncate(inode); |
| 1985 | |
Jeff Layton | a8810cd | 2020-12-10 14:39:26 -0500 | [diff] [blame] | 1986 | if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask)) |
| 1987 | ceph_check_caps(ci, 0, NULL); |
| 1988 | |
| 1989 | if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask)) |
| 1990 | ceph_flush_snaps(ci, NULL); |
| 1991 | |
Yan, Zheng | 1cf89a8 | 2019-05-18 11:18:44 +0800 | [diff] [blame] | 1992 | iput(inode); |
| 1993 | } |
| 1994 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1995 | /* |
| 1996 | * symlinks |
| 1997 | */ |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1998 | static const struct inode_operations ceph_symlink_iops = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1999 | .get_link = simple_get_link, |
Yan, Zheng | 0b93267 | 2013-04-07 16:28:49 +0800 | [diff] [blame] | 2000 | .setattr = ceph_setattr, |
| 2001 | .getattr = ceph_getattr, |
Yan, Zheng | 0b93267 | 2013-04-07 16:28:49 +0800 | [diff] [blame] | 2002 | .listxattr = ceph_listxattr, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2003 | }; |
| 2004 | |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 2005 | int __ceph_setattr(struct inode *inode, struct iattr *attr) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2006 | { |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2007 | struct ceph_inode_info *ci = ceph_inode(inode); |
Jeff Layton | c62498d | 2019-07-25 13:03:32 -0400 | [diff] [blame] | 2008 | unsigned int ia_valid = attr->ia_valid; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2009 | struct ceph_mds_request *req; |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 2010 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 2011 | struct ceph_cap_flush *prealloc_cf; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2012 | int issued; |
| 2013 | int release = 0, dirtied = 0; |
| 2014 | int mask = 0; |
| 2015 | int err = 0; |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 2016 | int inode_dirty_flags = 0; |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 2017 | bool lock_snap_rwsem = false; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2018 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 2019 | prealloc_cf = ceph_alloc_cap_flush(); |
| 2020 | if (!prealloc_cf) |
| 2021 | return -ENOMEM; |
| 2022 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2023 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR, |
| 2024 | USE_AUTH_MDS); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 2025 | if (IS_ERR(req)) { |
| 2026 | ceph_free_cap_flush(prealloc_cf); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2027 | return PTR_ERR(req); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 2028 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2029 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2030 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2031 | issued = __ceph_caps_issued(ci, NULL); |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 2032 | |
| 2033 | if (!ci->i_head_snapc && |
| 2034 | (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) { |
| 2035 | lock_snap_rwsem = true; |
| 2036 | if (!down_read_trylock(&mdsc->snap_rwsem)) { |
| 2037 | spin_unlock(&ci->i_ceph_lock); |
| 2038 | down_read(&mdsc->snap_rwsem); |
| 2039 | spin_lock(&ci->i_ceph_lock); |
| 2040 | issued = __ceph_caps_issued(ci, NULL); |
| 2041 | } |
| 2042 | } |
| 2043 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2044 | dout("setattr %p issued %s\n", inode, ceph_cap_string(issued)); |
| 2045 | |
| 2046 | if (ia_valid & ATTR_UID) { |
| 2047 | dout("setattr %p uid %d -> %d\n", inode, |
Eric W. Biederman | bd2bae6 | 2013-01-31 04:05:39 -0800 | [diff] [blame] | 2048 | from_kuid(&init_user_ns, inode->i_uid), |
| 2049 | from_kuid(&init_user_ns, attr->ia_uid)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2050 | if (issued & CEPH_CAP_AUTH_EXCL) { |
| 2051 | inode->i_uid = attr->ia_uid; |
| 2052 | dirtied |= CEPH_CAP_AUTH_EXCL; |
| 2053 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || |
Eric W. Biederman | ab871b9 | 2013-01-31 03:40:12 -0800 | [diff] [blame] | 2054 | !uid_eq(attr->ia_uid, inode->i_uid)) { |
| 2055 | req->r_args.setattr.uid = cpu_to_le32( |
| 2056 | from_kuid(&init_user_ns, attr->ia_uid)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2057 | mask |= CEPH_SETATTR_UID; |
| 2058 | release |= CEPH_CAP_AUTH_SHARED; |
| 2059 | } |
| 2060 | } |
| 2061 | if (ia_valid & ATTR_GID) { |
| 2062 | dout("setattr %p gid %d -> %d\n", inode, |
Eric W. Biederman | bd2bae6 | 2013-01-31 04:05:39 -0800 | [diff] [blame] | 2063 | from_kgid(&init_user_ns, inode->i_gid), |
| 2064 | from_kgid(&init_user_ns, attr->ia_gid)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2065 | if (issued & CEPH_CAP_AUTH_EXCL) { |
| 2066 | inode->i_gid = attr->ia_gid; |
| 2067 | dirtied |= CEPH_CAP_AUTH_EXCL; |
| 2068 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || |
Eric W. Biederman | ab871b9 | 2013-01-31 03:40:12 -0800 | [diff] [blame] | 2069 | !gid_eq(attr->ia_gid, inode->i_gid)) { |
| 2070 | req->r_args.setattr.gid = cpu_to_le32( |
| 2071 | from_kgid(&init_user_ns, attr->ia_gid)); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2072 | mask |= CEPH_SETATTR_GID; |
| 2073 | release |= CEPH_CAP_AUTH_SHARED; |
| 2074 | } |
| 2075 | } |
| 2076 | if (ia_valid & ATTR_MODE) { |
| 2077 | dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode, |
| 2078 | attr->ia_mode); |
| 2079 | if (issued & CEPH_CAP_AUTH_EXCL) { |
| 2080 | inode->i_mode = attr->ia_mode; |
| 2081 | dirtied |= CEPH_CAP_AUTH_EXCL; |
| 2082 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || |
| 2083 | attr->ia_mode != inode->i_mode) { |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 2084 | inode->i_mode = attr->ia_mode; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2085 | req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode); |
| 2086 | mask |= CEPH_SETATTR_MODE; |
| 2087 | release |= CEPH_CAP_AUTH_SHARED; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | if (ia_valid & ATTR_ATIME) { |
Deepa Dinamani | 13442b0 | 2018-04-23 19:26:50 -0700 | [diff] [blame] | 2092 | dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode, |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 2093 | inode->i_atime.tv_sec, inode->i_atime.tv_nsec, |
| 2094 | attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2095 | if (issued & CEPH_CAP_FILE_EXCL) { |
| 2096 | ci->i_time_warp_seq++; |
| 2097 | inode->i_atime = attr->ia_atime; |
| 2098 | dirtied |= CEPH_CAP_FILE_EXCL; |
| 2099 | } else if ((issued & CEPH_CAP_FILE_WR) && |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 2100 | timespec64_compare(&inode->i_atime, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2101 | &attr->ia_atime) < 0) { |
| 2102 | inode->i_atime = attr->ia_atime; |
| 2103 | dirtied |= CEPH_CAP_FILE_WR; |
| 2104 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 2105 | !timespec64_equal(&inode->i_atime, &attr->ia_atime)) { |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 2106 | ceph_encode_timespec64(&req->r_args.setattr.atime, |
| 2107 | &attr->ia_atime); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2108 | mask |= CEPH_SETATTR_ATIME; |
Yan, Zheng | be70489 | 2017-11-23 18:22:06 +0800 | [diff] [blame] | 2109 | release |= CEPH_CAP_FILE_SHARED | |
| 2110 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2111 | } |
| 2112 | } |
Jeff Layton | c62498d | 2019-07-25 13:03:32 -0400 | [diff] [blame] | 2113 | if (ia_valid & ATTR_SIZE) { |
Jeff Layton | 2d6795f | 2021-04-09 15:58:35 -0400 | [diff] [blame] | 2114 | loff_t isize = i_size_read(inode); |
| 2115 | |
| 2116 | dout("setattr %p size %lld -> %lld\n", inode, isize, attr->ia_size); |
Xiubo Li | e90334e | 2021-10-08 16:23:58 +0800 | [diff] [blame] | 2117 | if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) { |
| 2118 | if (attr->ia_size > isize) { |
| 2119 | i_size_write(inode, attr->ia_size); |
| 2120 | inode->i_blocks = calc_inode_blocks(attr->ia_size); |
| 2121 | ci->i_reported_size = attr->ia_size; |
| 2122 | dirtied |= CEPH_CAP_FILE_EXCL; |
| 2123 | ia_valid |= ATTR_MTIME; |
| 2124 | } |
Jeff Layton | c62498d | 2019-07-25 13:03:32 -0400 | [diff] [blame] | 2125 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || |
Jeff Layton | 2d6795f | 2021-04-09 15:58:35 -0400 | [diff] [blame] | 2126 | attr->ia_size != isize) { |
Jeff Layton | c62498d | 2019-07-25 13:03:32 -0400 | [diff] [blame] | 2127 | req->r_args.setattr.size = cpu_to_le64(attr->ia_size); |
Jeff Layton | 2d6795f | 2021-04-09 15:58:35 -0400 | [diff] [blame] | 2128 | req->r_args.setattr.old_size = cpu_to_le64(isize); |
Jeff Layton | c62498d | 2019-07-25 13:03:32 -0400 | [diff] [blame] | 2129 | mask |= CEPH_SETATTR_SIZE; |
| 2130 | release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | |
| 2131 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; |
| 2132 | } |
| 2133 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2134 | if (ia_valid & ATTR_MTIME) { |
Deepa Dinamani | 13442b0 | 2018-04-23 19:26:50 -0700 | [diff] [blame] | 2135 | dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode, |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 2136 | inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, |
| 2137 | attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2138 | if (issued & CEPH_CAP_FILE_EXCL) { |
| 2139 | ci->i_time_warp_seq++; |
| 2140 | inode->i_mtime = attr->ia_mtime; |
| 2141 | dirtied |= CEPH_CAP_FILE_EXCL; |
| 2142 | } else if ((issued & CEPH_CAP_FILE_WR) && |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 2143 | timespec64_compare(&inode->i_mtime, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2144 | &attr->ia_mtime) < 0) { |
| 2145 | inode->i_mtime = attr->ia_mtime; |
| 2146 | dirtied |= CEPH_CAP_FILE_WR; |
| 2147 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 2148 | !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) { |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 2149 | ceph_encode_timespec64(&req->r_args.setattr.mtime, |
| 2150 | &attr->ia_mtime); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2151 | mask |= CEPH_SETATTR_MTIME; |
Yan, Zheng | be70489 | 2017-11-23 18:22:06 +0800 | [diff] [blame] | 2152 | release |= CEPH_CAP_FILE_SHARED | |
| 2153 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2154 | } |
| 2155 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2156 | |
| 2157 | /* these do nothing */ |
| 2158 | if (ia_valid & ATTR_CTIME) { |
| 2159 | bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME| |
| 2160 | ATTR_MODE|ATTR_UID|ATTR_GID)) == 0; |
Deepa Dinamani | 13442b0 | 2018-04-23 19:26:50 -0700 | [diff] [blame] | 2161 | dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode, |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 2162 | inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, |
| 2163 | attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2164 | only ? "ctime only" : "ignored"); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2165 | if (only) { |
| 2166 | /* |
| 2167 | * if kernel wants to dirty ctime but nothing else, |
| 2168 | * we need to choose a cap to dirty under, or do |
| 2169 | * a almost-no-op setattr |
| 2170 | */ |
| 2171 | if (issued & CEPH_CAP_AUTH_EXCL) |
| 2172 | dirtied |= CEPH_CAP_AUTH_EXCL; |
| 2173 | else if (issued & CEPH_CAP_FILE_EXCL) |
| 2174 | dirtied |= CEPH_CAP_FILE_EXCL; |
| 2175 | else if (issued & CEPH_CAP_XATTR_EXCL) |
| 2176 | dirtied |= CEPH_CAP_XATTR_EXCL; |
| 2177 | else |
| 2178 | mask |= CEPH_SETATTR_CTIME; |
| 2179 | } |
| 2180 | } |
| 2181 | if (ia_valid & ATTR_FILE) |
| 2182 | dout("setattr %p ATTR_FILE ... hrm!\n", inode); |
| 2183 | |
| 2184 | if (dirtied) { |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 2185 | inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied, |
| 2186 | &prealloc_cf); |
Yan, Zheng | 4ca2fea | 2017-06-01 17:08:00 +0800 | [diff] [blame] | 2187 | inode->i_ctime = attr->ia_ctime; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | release &= issued; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2191 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 2192 | if (lock_snap_rwsem) |
| 2193 | up_read(&mdsc->snap_rwsem); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2194 | |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 2195 | if (inode_dirty_flags) |
| 2196 | __mark_inode_dirty(inode, inode_dirty_flags); |
| 2197 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2198 | if (mask) { |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 2199 | req->r_inode = inode; |
| 2200 | ihold(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2201 | req->r_inode_drop = release; |
| 2202 | req->r_args.setattr.mask = cpu_to_le32(mask); |
| 2203 | req->r_num_caps = 1; |
Arnd Bergmann | 0ed1e90 | 2018-07-13 22:18:38 +0200 | [diff] [blame] | 2204 | req->r_stamp = attr->ia_ctime; |
Sage Weil | 752c8bd | 2013-02-05 13:52:29 -0800 | [diff] [blame] | 2205 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2206 | } |
| 2207 | dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err, |
| 2208 | ceph_cap_string(dirtied), mask); |
| 2209 | |
| 2210 | ceph_mdsc_put_request(req); |
Yan, Zheng | 8179a10 | 2017-04-19 10:01:48 +0800 | [diff] [blame] | 2211 | ceph_free_cap_flush(prealloc_cf); |
| 2212 | |
| 2213 | if (err >= 0 && (mask & CEPH_SETATTR_SIZE)) |
Yan, Zheng | b0d7c22 | 2013-08-12 21:42:15 -0700 | [diff] [blame] | 2214 | __ceph_do_pending_vmtruncate(inode); |
Yan, Zheng | 8179a10 | 2017-04-19 10:01:48 +0800 | [diff] [blame] | 2215 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2216 | return err; |
| 2217 | } |
| 2218 | |
| 2219 | /* |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 2220 | * setattr |
| 2221 | */ |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2222 | int ceph_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 2223 | struct iattr *attr) |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 2224 | { |
Jan Kara | fd5472e | 2016-05-26 16:10:38 +0200 | [diff] [blame] | 2225 | struct inode *inode = d_inode(dentry); |
Chengguang Xu | 36a4c72d | 2018-08-05 19:33:01 +0800 | [diff] [blame] | 2226 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Jan Kara | fd5472e | 2016-05-26 16:10:38 +0200 | [diff] [blame] | 2227 | int err; |
| 2228 | |
| 2229 | if (ceph_snap(inode) != CEPH_NOSNAP) |
| 2230 | return -EROFS; |
| 2231 | |
Jeff Layton | 5d6451b | 2021-08-31 13:39:13 -0400 | [diff] [blame] | 2232 | if (ceph_inode_is_shutdown(inode)) |
| 2233 | return -ESTALE; |
| 2234 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 2235 | err = setattr_prepare(&init_user_ns, dentry, attr); |
Jan Kara | fd5472e | 2016-05-26 16:10:38 +0200 | [diff] [blame] | 2236 | if (err != 0) |
| 2237 | return err; |
| 2238 | |
Luis Henriques | 2b83845 | 2018-01-05 10:47:21 +0000 | [diff] [blame] | 2239 | if ((attr->ia_valid & ATTR_SIZE) && |
Jeff Layton | 2d6795f | 2021-04-09 15:58:35 -0400 | [diff] [blame] | 2240 | attr->ia_size > max(i_size_read(inode), fsc->max_file_size)) |
Chengguang Xu | 36a4c72d | 2018-08-05 19:33:01 +0800 | [diff] [blame] | 2241 | return -EFBIG; |
| 2242 | |
| 2243 | if ((attr->ia_valid & ATTR_SIZE) && |
Luis Henriques | 2b83845 | 2018-01-05 10:47:21 +0000 | [diff] [blame] | 2244 | ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size)) |
| 2245 | return -EDQUOT; |
| 2246 | |
Yan, Zheng | 8179a10 | 2017-04-19 10:01:48 +0800 | [diff] [blame] | 2247 | err = __ceph_setattr(inode, attr); |
| 2248 | |
| 2249 | if (err >= 0 && (attr->ia_valid & ATTR_MODE)) |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 2250 | err = posix_acl_chmod(&init_user_ns, inode, attr->ia_mode); |
Yan, Zheng | 8179a10 | 2017-04-19 10:01:48 +0800 | [diff] [blame] | 2251 | |
| 2252 | return err; |
Andreas Gruenbacher | a26fecc | 2016-04-14 00:30:16 +0200 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /* |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2256 | * Verify that we have a lease on the given mask. If not, |
| 2257 | * do a getattr against an mds. |
| 2258 | */ |
Yan, Zheng | 01deead | 2014-11-14 21:56:29 +0800 | [diff] [blame] | 2259 | int __ceph_do_getattr(struct inode *inode, struct page *locked_page, |
| 2260 | int mask, bool force) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2261 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2262 | struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); |
| 2263 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2264 | struct ceph_mds_request *req; |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 2265 | int mode; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2266 | int err; |
| 2267 | |
| 2268 | if (ceph_snap(inode) == CEPH_SNAPDIR) { |
| 2269 | dout("do_getattr inode %p SNAPDIR\n", inode); |
| 2270 | return 0; |
| 2271 | } |
| 2272 | |
Yan, Zheng | 01deead | 2014-11-14 21:56:29 +0800 | [diff] [blame] | 2273 | dout("do_getattr inode %p mask %s mode 0%o\n", |
| 2274 | inode, ceph_cap_string(mask), inode->i_mode); |
Xiubo Li | 1af16d5 | 2020-03-19 23:45:00 -0400 | [diff] [blame] | 2275 | if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1)) |
| 2276 | return 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2277 | |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 2278 | mode = (mask & CEPH_STAT_RSTAT) ? USE_AUTH_MDS : USE_ANY_MDS; |
| 2279 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2280 | if (IS_ERR(req)) |
| 2281 | return PTR_ERR(req); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 2282 | req->r_inode = inode; |
| 2283 | ihold(inode); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2284 | req->r_num_caps = 1; |
| 2285 | req->r_args.getattr.mask = cpu_to_le32(mask); |
Yan, Zheng | 01deead | 2014-11-14 21:56:29 +0800 | [diff] [blame] | 2286 | req->r_locked_page = locked_page; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2287 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
Yan, Zheng | 01deead | 2014-11-14 21:56:29 +0800 | [diff] [blame] | 2288 | if (locked_page && err == 0) { |
| 2289 | u64 inline_version = req->r_reply_info.targeti.inline_version; |
| 2290 | if (inline_version == 0) { |
| 2291 | /* the reply is supposed to contain inline data */ |
| 2292 | err = -EINVAL; |
| 2293 | } else if (inline_version == CEPH_INLINE_NONE) { |
| 2294 | err = -ENODATA; |
| 2295 | } else { |
| 2296 | err = req->r_reply_info.targeti.inline_len; |
| 2297 | } |
| 2298 | } |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2299 | ceph_mdsc_put_request(req); |
| 2300 | dout("do_getattr result=%d\n", err); |
| 2301 | return err; |
| 2302 | } |
| 2303 | |
| 2304 | |
| 2305 | /* |
| 2306 | * Check inode permissions. We verify we have a valid value for |
| 2307 | * the AUTH cap, then call the generic handler. |
| 2308 | */ |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2309 | int ceph_permission(struct user_namespace *mnt_userns, struct inode *inode, |
| 2310 | int mask) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2311 | { |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 2312 | int err; |
| 2313 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 2314 | if (mask & MAY_NOT_BLOCK) |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 2315 | return -ECHILD; |
| 2316 | |
Yan, Zheng | 508b32d | 2014-09-16 21:46:17 +0800 | [diff] [blame] | 2317 | err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2318 | |
| 2319 | if (!err) |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 2320 | err = generic_permission(&init_user_ns, inode, mask); |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2321 | return err; |
| 2322 | } |
| 2323 | |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2324 | /* Craft a mask of needed caps given a set of requested statx attrs. */ |
Jeff Layton | 04fabb1 | 2020-11-09 13:14:07 -0500 | [diff] [blame] | 2325 | static int statx_to_caps(u32 want, umode_t mode) |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2326 | { |
| 2327 | int mask = 0; |
| 2328 | |
Jeff Layton | 5898178 | 2019-06-05 12:08:32 -0400 | [diff] [blame] | 2329 | if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME)) |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2330 | mask |= CEPH_CAP_AUTH_SHARED; |
| 2331 | |
Jeff Layton | 04fabb1 | 2020-11-09 13:14:07 -0500 | [diff] [blame] | 2332 | if (want & (STATX_NLINK|STATX_CTIME)) { |
| 2333 | /* |
| 2334 | * The link count for directories depends on inode->i_subdirs, |
| 2335 | * and that is only updated when Fs caps are held. |
| 2336 | */ |
| 2337 | if (S_ISDIR(mode)) |
| 2338 | mask |= CEPH_CAP_FILE_SHARED; |
| 2339 | else |
| 2340 | mask |= CEPH_CAP_LINK_SHARED; |
| 2341 | } |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2342 | |
| 2343 | if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE| |
| 2344 | STATX_BLOCKS)) |
| 2345 | mask |= CEPH_CAP_FILE_SHARED; |
| 2346 | |
| 2347 | if (want & (STATX_CTIME)) |
| 2348 | mask |= CEPH_CAP_XATTR_SHARED; |
| 2349 | |
| 2350 | return mask; |
| 2351 | } |
| 2352 | |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2353 | /* |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2354 | * Get all the attributes. If we have sufficient caps for the requested attrs, |
| 2355 | * then we can avoid talking to the MDS at all. |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2356 | */ |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 2357 | int ceph_getattr(struct user_namespace *mnt_userns, const struct path *path, |
| 2358 | struct kstat *stat, u32 request_mask, unsigned int flags) |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2359 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 2360 | struct inode *inode = d_inode(path->dentry); |
Sage Weil | 232d4b0 | 2009-10-21 11:21:49 -0700 | [diff] [blame] | 2361 | struct ceph_inode_info *ci = ceph_inode(inode); |
Jeff Layton | 5898178 | 2019-06-05 12:08:32 -0400 | [diff] [blame] | 2362 | u32 valid_mask = STATX_BASIC_STATS; |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2363 | int err = 0; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2364 | |
Jeff Layton | 5d6451b | 2021-08-31 13:39:13 -0400 | [diff] [blame] | 2365 | if (ceph_inode_is_shutdown(inode)) |
| 2366 | return -ESTALE; |
| 2367 | |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2368 | /* Skip the getattr altogether if we're asked not to sync */ |
| 2369 | if (!(flags & AT_STATX_DONT_SYNC)) { |
Jeff Layton | 04fabb1 | 2020-11-09 13:14:07 -0500 | [diff] [blame] | 2370 | err = ceph_do_getattr(inode, |
| 2371 | statx_to_caps(request_mask, inode->i_mode), |
| 2372 | flags & AT_STATX_FORCE_SYNC); |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2373 | if (err) |
| 2374 | return err; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2375 | } |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2376 | |
Christian Brauner | 0d56a45 | 2021-01-21 14:19:30 +0100 | [diff] [blame] | 2377 | generic_fillattr(&init_user_ns, inode, stat); |
Jeff Layton | ebce3eb | 2020-08-18 08:03:48 -0400 | [diff] [blame] | 2378 | stat->ino = ceph_present_inode(inode); |
Jeff Layton | 5898178 | 2019-06-05 12:08:32 -0400 | [diff] [blame] | 2379 | |
| 2380 | /* |
| 2381 | * btime on newly-allocated inodes is 0, so if this is still set to |
| 2382 | * that, then assume that it's not valid. |
| 2383 | */ |
| 2384 | if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) { |
| 2385 | stat->btime = ci->i_btime; |
| 2386 | valid_mask |= STATX_BTIME; |
| 2387 | } |
| 2388 | |
Jeff Layton | 428bb68 | 2019-04-11 15:27:03 -0400 | [diff] [blame] | 2389 | if (ceph_snap(inode) == CEPH_NOSNAP) |
| 2390 | stat->dev = inode->i_sb->s_dev; |
| 2391 | else |
| 2392 | stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0; |
| 2393 | |
| 2394 | if (S_ISDIR(inode->i_mode)) { |
| 2395 | if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), |
| 2396 | RBYTES)) |
| 2397 | stat->size = ci->i_rbytes; |
| 2398 | else |
| 2399 | stat->size = ci->i_files + ci->i_subdirs; |
| 2400 | stat->blocks = 0; |
| 2401 | stat->blksize = 65536; |
| 2402 | /* |
| 2403 | * Some applications rely on the number of st_nlink |
| 2404 | * value on directories to be either 0 (if unlinked) |
| 2405 | * or 2 + number of subdirectories. |
| 2406 | */ |
| 2407 | if (stat->nlink == 1) |
| 2408 | /* '.' + '..' + subdirs */ |
| 2409 | stat->nlink = 1 + 1 + ci->i_subdirs; |
| 2410 | } |
| 2411 | |
Jeff Layton | 5898178 | 2019-06-05 12:08:32 -0400 | [diff] [blame] | 2412 | stat->result_mask = request_mask & valid_mask; |
Sage Weil | 355da1e | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2413 | return err; |
| 2414 | } |
Jeff Layton | 5d6451b | 2021-08-31 13:39:13 -0400 | [diff] [blame] | 2415 | |
| 2416 | void ceph_inode_shutdown(struct inode *inode) |
| 2417 | { |
| 2418 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2419 | struct rb_node *p; |
| 2420 | int iputs = 0; |
| 2421 | bool invalidate = false; |
| 2422 | |
| 2423 | spin_lock(&ci->i_ceph_lock); |
| 2424 | ci->i_ceph_flags |= CEPH_I_SHUTDOWN; |
| 2425 | p = rb_first(&ci->i_caps); |
| 2426 | while (p) { |
| 2427 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); |
| 2428 | |
| 2429 | p = rb_next(p); |
| 2430 | iputs += ceph_purge_inode_cap(inode, cap, &invalidate); |
| 2431 | } |
| 2432 | spin_unlock(&ci->i_ceph_lock); |
| 2433 | |
| 2434 | if (invalidate) |
| 2435 | ceph_queue_invalidate(inode); |
| 2436 | while (iputs--) |
| 2437 | iput(inode); |
| 2438 | } |