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