blob: 72607c17e6fd479d246fcf164db8c948488270ae [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil355da1e2009-10-06 11:31:08 -07002
3#include <linux/module.h>
4#include <linux/fs.h>
Sage Weil355da1e2009-10-06 11:31:08 -07005#include <linux/slab.h>
6#include <linux/string.h>
7#include <linux/uaccess.h>
8#include <linux/kernel.h>
9#include <linux/namei.h>
10#include <linux/writeback.h>
11#include <linux/vmalloc.h>
Linus Torvalds4db658e2014-01-28 18:06:18 -080012#include <linux/posix_acl.h>
Yan, Zheng3e7fbe92014-04-18 16:00:30 +080013#include <linux/random.h>
Sage Weil355da1e2009-10-06 11:31:08 -070014
15#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070016#include "mds_client.h"
Milosz Tanski99ccbd22013-08-21 17:29:54 -040017#include "cache.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070018#include <linux/ceph/decode.h>
Sage Weil355da1e2009-10-06 11:31:08 -070019
20/*
21 * Ceph inode operations
22 *
23 * Implement basic inode helpers (get, alloc) and inode ops (getattr,
24 * setattr, etc.), xattr helpers, and helpers for assimilating
25 * metadata returned by the MDS into our cache.
26 *
27 * Also define helpers for doing asynchronous writeback, invalidation,
28 * and truncation for the benefit of those who can't afford to block
29 * (typically because they are in the message handler path).
30 */
31
32static const struct inode_operations ceph_symlink_iops;
33
Sage Weil3c6f6b72010-02-09 15:24:44 -080034static void ceph_invalidate_work(struct work_struct *work);
35static void ceph_writeback_work(struct work_struct *work);
36static void ceph_vmtruncate_work(struct work_struct *work);
Sage Weil355da1e2009-10-06 11:31:08 -070037
38/*
39 * find or create an inode, given the ceph ino number
40 */
Yehuda Sadehad1fee92011-01-21 16:44:03 -080041static 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
Sage Weil355da1e2009-10-06 11:31:08 -070048struct 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);
54 if (inode == NULL)
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);
Sage Weilb377ff12009-11-11 15:22:37 -080077 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -070078
79 BUG_ON(!S_ISDIR(parent->i_mode));
80 if (IS_ERR(inode))
Julia Lawall7e34bc52010-05-22 12:01:14 +020081 return inode;
Sage Weil355da1e2009-10-06 11:31:08 -070082 inode->i_mode = parent->i_mode;
83 inode->i_uid = parent->i_uid;
84 inode->i_gid = parent->i_gid;
85 inode->i_op = &ceph_dir_iops;
86 inode->i_fop = &ceph_dir_fops;
Sage Weilb377ff12009-11-11 15:22:37 -080087 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
88 ci->i_rbytes = 0;
Sage Weil355da1e2009-10-06 11:31:08 -070089 return inode;
90}
91
92const struct inode_operations ceph_file_iops = {
93 .permission = ceph_permission,
94 .setattr = ceph_setattr,
95 .getattr = ceph_getattr,
96 .setxattr = ceph_setxattr,
97 .getxattr = ceph_getxattr,
98 .listxattr = ceph_listxattr,
99 .removexattr = ceph_removexattr,
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800100 .get_acl = ceph_get_acl,
Sage Weil72466d02014-01-29 06:22:25 -0800101 .set_acl = ceph_set_acl,
Sage Weil355da1e2009-10-06 11:31:08 -0700102};
103
104
105/*
106 * We use a 'frag tree' to keep track of the MDS's directory fragments
107 * for a given inode (usually there is just a single fragment). We
108 * need to know when a child frag is delegated to a new MDS, or when
109 * it is flagged as replicated, so we can direct our requests
110 * accordingly.
111 */
112
113/*
114 * find/create a frag in the tree
115 */
116static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
117 u32 f)
118{
119 struct rb_node **p;
120 struct rb_node *parent = NULL;
121 struct ceph_inode_frag *frag;
122 int c;
123
124 p = &ci->i_fragtree.rb_node;
125 while (*p) {
126 parent = *p;
127 frag = rb_entry(parent, struct ceph_inode_frag, node);
128 c = ceph_frag_compare(f, frag->frag);
129 if (c < 0)
130 p = &(*p)->rb_left;
131 else if (c > 0)
132 p = &(*p)->rb_right;
133 else
134 return frag;
135 }
136
137 frag = kmalloc(sizeof(*frag), GFP_NOFS);
138 if (!frag) {
139 pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
140 "frag %x\n", &ci->vfs_inode,
141 ceph_vinop(&ci->vfs_inode), f);
142 return ERR_PTR(-ENOMEM);
143 }
144 frag->frag = f;
145 frag->split_by = 0;
146 frag->mds = -1;
147 frag->ndist = 0;
148
149 rb_link_node(&frag->node, parent, p);
150 rb_insert_color(&frag->node, &ci->i_fragtree);
151
152 dout("get_or_create_frag added %llx.%llx frag %x\n",
153 ceph_vinop(&ci->vfs_inode), f);
154 return frag;
155}
156
157/*
158 * find a specific frag @f
159 */
160struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
161{
162 struct rb_node *n = ci->i_fragtree.rb_node;
163
164 while (n) {
165 struct ceph_inode_frag *frag =
166 rb_entry(n, struct ceph_inode_frag, node);
167 int c = ceph_frag_compare(f, frag->frag);
168 if (c < 0)
169 n = n->rb_left;
170 else if (c > 0)
171 n = n->rb_right;
172 else
173 return frag;
174 }
175 return NULL;
176}
177
178/*
179 * Choose frag containing the given value @v. If @pfrag is
180 * specified, copy the frag delegation info to the caller if
181 * it is present.
182 */
Yan, Zheng3e7fbe92014-04-18 16:00:30 +0800183static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
184 struct ceph_inode_frag *pfrag, int *found)
Sage Weil355da1e2009-10-06 11:31:08 -0700185{
186 u32 t = ceph_frag_make(0, 0);
187 struct ceph_inode_frag *frag;
188 unsigned nway, i;
189 u32 n;
190
191 if (found)
192 *found = 0;
193
Sage Weil355da1e2009-10-06 11:31:08 -0700194 while (1) {
195 WARN_ON(!ceph_frag_contains_value(t, v));
196 frag = __ceph_find_frag(ci, t);
197 if (!frag)
198 break; /* t is a leaf */
199 if (frag->split_by == 0) {
200 if (pfrag)
201 memcpy(pfrag, frag, sizeof(*pfrag));
202 if (found)
203 *found = 1;
204 break;
205 }
206
207 /* choose child */
208 nway = 1 << frag->split_by;
209 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
210 frag->split_by, nway);
211 for (i = 0; i < nway; i++) {
212 n = ceph_frag_make_child(t, frag->split_by, i);
213 if (ceph_frag_contains_value(n, v)) {
214 t = n;
215 break;
216 }
217 }
218 BUG_ON(i == nway);
219 }
220 dout("choose_frag(%x) = %x\n", v, t);
221
Sage Weil355da1e2009-10-06 11:31:08 -0700222 return t;
223}
224
Yan, Zheng3e7fbe92014-04-18 16:00:30 +0800225u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
226 struct ceph_inode_frag *pfrag, int *found)
227{
228 u32 ret;
229 mutex_lock(&ci->i_fragtree_mutex);
230 ret = __ceph_choose_frag(ci, v, pfrag, found);
231 mutex_unlock(&ci->i_fragtree_mutex);
232 return ret;
233}
234
Sage Weil355da1e2009-10-06 11:31:08 -0700235/*
236 * Process dirfrag (delegation) info from the mds. Include leaf
237 * fragment in tree ONLY if ndist > 0. Otherwise, only
238 * branches/splits are included in i_fragtree)
239 */
240static int ceph_fill_dirfrag(struct inode *inode,
241 struct ceph_mds_reply_dirfrag *dirinfo)
242{
243 struct ceph_inode_info *ci = ceph_inode(inode);
244 struct ceph_inode_frag *frag;
245 u32 id = le32_to_cpu(dirinfo->frag);
246 int mds = le32_to_cpu(dirinfo->auth);
247 int ndist = le32_to_cpu(dirinfo->ndist);
Yan, Zheng8d085032014-04-18 22:01:38 +0800248 int diri_auth = -1;
Sage Weil355da1e2009-10-06 11:31:08 -0700249 int i;
250 int err = 0;
251
Yan, Zheng8d085032014-04-18 22:01:38 +0800252 spin_lock(&ci->i_ceph_lock);
253 if (ci->i_auth_cap)
254 diri_auth = ci->i_auth_cap->mds;
255 spin_unlock(&ci->i_ceph_lock);
256
Sage Weil355da1e2009-10-06 11:31:08 -0700257 mutex_lock(&ci->i_fragtree_mutex);
Yan, Zheng8d085032014-04-18 22:01:38 +0800258 if (ndist == 0 && mds == diri_auth) {
Sage Weil355da1e2009-10-06 11:31:08 -0700259 /* no delegation info needed. */
260 frag = __ceph_find_frag(ci, id);
261 if (!frag)
262 goto out;
263 if (frag->split_by == 0) {
264 /* tree leaf, remove */
265 dout("fill_dirfrag removed %llx.%llx frag %x"
266 " (no ref)\n", ceph_vinop(inode), id);
267 rb_erase(&frag->node, &ci->i_fragtree);
268 kfree(frag);
269 } else {
270 /* tree branch, keep and clear */
271 dout("fill_dirfrag cleared %llx.%llx frag %x"
272 " referral\n", ceph_vinop(inode), id);
273 frag->mds = -1;
274 frag->ndist = 0;
275 }
276 goto out;
277 }
278
279
280 /* find/add this frag to store mds delegation info */
281 frag = __get_or_create_frag(ci, id);
282 if (IS_ERR(frag)) {
283 /* this is not the end of the world; we can continue
284 with bad/inaccurate delegation info */
285 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
286 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
287 err = -ENOMEM;
288 goto out;
289 }
290
291 frag->mds = mds;
292 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
293 for (i = 0; i < frag->ndist; i++)
294 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
295 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
296 ceph_vinop(inode), frag->frag, frag->ndist);
297
298out:
299 mutex_unlock(&ci->i_fragtree_mutex);
300 return err;
301}
302
Yan, Zheng3e7fbe92014-04-18 16:00:30 +0800303static int ceph_fill_fragtree(struct inode *inode,
304 struct ceph_frag_tree_head *fragtree,
305 struct ceph_mds_reply_dirfrag *dirinfo)
306{
307 struct ceph_inode_info *ci = ceph_inode(inode);
308 struct ceph_inode_frag *frag;
309 struct rb_node *rb_node;
310 int i;
311 u32 id, nsplits;
312 bool update = false;
313
314 mutex_lock(&ci->i_fragtree_mutex);
315 nsplits = le32_to_cpu(fragtree->nsplits);
316 if (nsplits) {
317 i = prandom_u32() % nsplits;
318 id = le32_to_cpu(fragtree->splits[i].frag);
319 if (!__ceph_find_frag(ci, id))
320 update = true;
321 } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
322 rb_node = rb_first(&ci->i_fragtree);
323 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
324 if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
325 update = true;
326 }
327 if (!update && dirinfo) {
328 id = le32_to_cpu(dirinfo->frag);
329 if (id != __ceph_choose_frag(ci, id, NULL, NULL))
330 update = true;
331 }
332 if (!update)
333 goto out_unlock;
334
335 dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
336 rb_node = rb_first(&ci->i_fragtree);
337 for (i = 0; i < nsplits; i++) {
338 id = le32_to_cpu(fragtree->splits[i].frag);
339 frag = NULL;
340 while (rb_node) {
341 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
342 if (ceph_frag_compare(frag->frag, id) >= 0) {
343 if (frag->frag != id)
344 frag = NULL;
345 else
346 rb_node = rb_next(rb_node);
347 break;
348 }
349 rb_node = rb_next(rb_node);
350 rb_erase(&frag->node, &ci->i_fragtree);
351 kfree(frag);
352 frag = NULL;
353 }
354 if (!frag) {
355 frag = __get_or_create_frag(ci, id);
356 if (IS_ERR(frag))
357 continue;
358 }
359 frag->split_by = le32_to_cpu(fragtree->splits[i].by);
360 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
361 }
362 while (rb_node) {
363 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
364 rb_node = rb_next(rb_node);
365 rb_erase(&frag->node, &ci->i_fragtree);
366 kfree(frag);
367 }
368out_unlock:
369 mutex_unlock(&ci->i_fragtree_mutex);
370 return 0;
371}
Sage Weil355da1e2009-10-06 11:31:08 -0700372
373/*
374 * initialize a newly allocated inode.
375 */
376struct inode *ceph_alloc_inode(struct super_block *sb)
377{
378 struct ceph_inode_info *ci;
379 int i;
380
381 ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
382 if (!ci)
383 return NULL;
384
385 dout("alloc_inode %p\n", &ci->vfs_inode);
386
Sage Weilbe655592011-11-30 09:47:09 -0800387 spin_lock_init(&ci->i_ceph_lock);
388
Sage Weil355da1e2009-10-06 11:31:08 -0700389 ci->i_version = 0;
390 ci->i_time_warp_seq = 0;
391 ci->i_ceph_flags = 0;
Yan, Zheng70db4f32014-10-21 18:09:56 -0700392 ci->i_ordered_count = 0;
Yan, Zheng2f276c52013-03-13 19:44:32 +0800393 atomic_set(&ci->i_release_count, 1);
394 atomic_set(&ci->i_complete_count, 0);
Sage Weil355da1e2009-10-06 11:31:08 -0700395 ci->i_symlink = NULL;
396
Sage Weil6c0f3af2010-11-16 11:14:34 -0800397 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
398
Sage Weil355da1e2009-10-06 11:31:08 -0700399 ci->i_fragtree = RB_ROOT;
400 mutex_init(&ci->i_fragtree_mutex);
401
402 ci->i_xattrs.blob = NULL;
403 ci->i_xattrs.prealloc_blob = NULL;
404 ci->i_xattrs.dirty = false;
405 ci->i_xattrs.index = RB_ROOT;
406 ci->i_xattrs.count = 0;
407 ci->i_xattrs.names_size = 0;
408 ci->i_xattrs.vals_size = 0;
409 ci->i_xattrs.version = 0;
410 ci->i_xattrs.index_version = 0;
411
412 ci->i_caps = RB_ROOT;
413 ci->i_auth_cap = NULL;
414 ci->i_dirty_caps = 0;
415 ci->i_flushing_caps = 0;
416 INIT_LIST_HEAD(&ci->i_dirty_item);
417 INIT_LIST_HEAD(&ci->i_flushing_item);
418 ci->i_cap_flush_seq = 0;
419 ci->i_cap_flush_last_tid = 0;
420 memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
421 init_waitqueue_head(&ci->i_cap_wq);
422 ci->i_hold_caps_min = 0;
423 ci->i_hold_caps_max = 0;
424 INIT_LIST_HEAD(&ci->i_cap_delay_list);
Sage Weil355da1e2009-10-06 11:31:08 -0700425 INIT_LIST_HEAD(&ci->i_cap_snaps);
426 ci->i_head_snapc = NULL;
427 ci->i_snap_caps = 0;
428
429 for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
430 ci->i_nr_by_mode[i] = 0;
431
Yan, Zhengb0d7c222013-08-12 21:42:15 -0700432 mutex_init(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -0700433 ci->i_truncate_seq = 0;
434 ci->i_truncate_size = 0;
435 ci->i_truncate_pending = 0;
436
437 ci->i_max_size = 0;
438 ci->i_reported_size = 0;
439 ci->i_wanted_max_size = 0;
440 ci->i_requested_max_size = 0;
441
442 ci->i_pin_ref = 0;
443 ci->i_rd_ref = 0;
444 ci->i_rdcache_ref = 0;
445 ci->i_wr_ref = 0;
Henry C Changd3d07202011-05-11 10:29:54 +0000446 ci->i_wb_ref = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700447 ci->i_wrbuffer_ref = 0;
448 ci->i_wrbuffer_ref_head = 0;
449 ci->i_shared_gen = 0;
450 ci->i_rdcache_gen = 0;
451 ci->i_rdcache_revoking = 0;
452
453 INIT_LIST_HEAD(&ci->i_unsafe_writes);
454 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
455 spin_lock_init(&ci->i_unsafe_lock);
456
457 ci->i_snap_realm = NULL;
458 INIT_LIST_HEAD(&ci->i_snap_realm_item);
459 INIT_LIST_HEAD(&ci->i_snap_flush_item);
460
Sage Weil3c6f6b72010-02-09 15:24:44 -0800461 INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
462 INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
Sage Weil355da1e2009-10-06 11:31:08 -0700463
464 INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
465
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400466 ceph_fscache_inode_init(ci);
467
Sage Weil355da1e2009-10-06 11:31:08 -0700468 return &ci->vfs_inode;
469}
470
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100471static void ceph_i_callback(struct rcu_head *head)
472{
473 struct inode *inode = container_of(head, struct inode, i_rcu);
474 struct ceph_inode_info *ci = ceph_inode(inode);
475
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100476 kmem_cache_free(ceph_inode_cachep, ci);
477}
478
Sage Weil355da1e2009-10-06 11:31:08 -0700479void ceph_destroy_inode(struct inode *inode)
480{
481 struct ceph_inode_info *ci = ceph_inode(inode);
482 struct ceph_inode_frag *frag;
483 struct rb_node *n;
484
485 dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
486
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400487 ceph_fscache_unregister_inode_cookie(ci);
488
Sage Weil355da1e2009-10-06 11:31:08 -0700489 ceph_queue_caps_release(inode);
490
Sage Weil8b218b82010-03-09 12:59:08 -0800491 /*
492 * we may still have a snap_realm reference if there are stray
Yan, Zhengd9df2782014-04-18 09:57:11 +0800493 * caps in i_snap_caps.
Sage Weil8b218b82010-03-09 12:59:08 -0800494 */
495 if (ci->i_snap_realm) {
496 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700497 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weil8b218b82010-03-09 12:59:08 -0800498 struct ceph_snap_realm *realm = ci->i_snap_realm;
499
500 dout(" dropping residual ref to snap realm %p\n", realm);
501 spin_lock(&realm->inodes_with_caps_lock);
502 list_del_init(&ci->i_snap_realm_item);
503 spin_unlock(&realm->inodes_with_caps_lock);
504 ceph_put_snap_realm(mdsc, realm);
505 }
506
Sage Weil355da1e2009-10-06 11:31:08 -0700507 kfree(ci->i_symlink);
508 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
509 frag = rb_entry(n, struct ceph_inode_frag, node);
510 rb_erase(n, &ci->i_fragtree);
511 kfree(frag);
512 }
513
514 __ceph_destroy_xattrs(ci);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800515 if (ci->i_xattrs.blob)
516 ceph_buffer_put(ci->i_xattrs.blob);
517 if (ci->i_xattrs.prealloc_blob)
518 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700519
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100520 call_rcu(&inode->i_rcu, ceph_i_callback);
Sage Weil355da1e2009-10-06 11:31:08 -0700521}
522
Yan, Zheng9f12bd12013-09-20 19:55:31 +0800523int ceph_drop_inode(struct inode *inode)
524{
525 /*
526 * Positve dentry and corresponding inode are always accompanied
527 * in MDS reply. So no need to keep inode in the cache after
528 * dropping all its aliases.
529 */
530 return 1;
531}
532
Sage Weil355da1e2009-10-06 11:31:08 -0700533/*
534 * Helpers to fill in size, ctime, mtime, and atime. We have to be
535 * careful because either the client or MDS may have more up to date
536 * info, depending on which capabilities are held, and whether
537 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
538 * and size are monotonically increasing, except when utimes() or
539 * truncate() increments the corresponding _seq values.)
540 */
541int ceph_fill_file_size(struct inode *inode, int issued,
542 u32 truncate_seq, u64 truncate_size, u64 size)
543{
544 struct ceph_inode_info *ci = ceph_inode(inode);
545 int queue_trunc = 0;
546
547 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
548 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
549 dout("size %lld -> %llu\n", inode->i_size, size);
550 inode->i_size = size;
551 inode->i_blocks = (size + (1<<9) - 1) >> 9;
552 ci->i_reported_size = size;
553 if (truncate_seq != ci->i_truncate_seq) {
554 dout("truncate_seq %u -> %u\n",
555 ci->i_truncate_seq, truncate_seq);
556 ci->i_truncate_seq = truncate_seq;
Yan, Zhengb0d7c222013-08-12 21:42:15 -0700557
558 /* the MDS should have revoked these caps */
559 WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
560 CEPH_CAP_FILE_RD |
561 CEPH_CAP_FILE_WR |
562 CEPH_CAP_FILE_LAZYIO));
Yehuda Sadeh3d497d82010-02-09 11:08:40 -0800563 /*
564 * If we hold relevant caps, or in the case where we're
565 * not the only client referencing this file and we
566 * don't hold those caps, then we need to check whether
567 * the file is either opened or mmaped
568 */
Yan, Zhengb0d7c222013-08-12 21:42:15 -0700569 if ((issued & (CEPH_CAP_FILE_CACHE|
570 CEPH_CAP_FILE_BUFFER)) ||
Yehuda Sadeh3d497d82010-02-09 11:08:40 -0800571 mapping_mapped(inode->i_mapping) ||
572 __ceph_caps_file_wanted(ci)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700573 ci->i_truncate_pending++;
574 queue_trunc = 1;
575 }
576 }
577 }
578 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
579 ci->i_truncate_size != truncate_size) {
580 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
581 truncate_size);
582 ci->i_truncate_size = truncate_size;
583 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400584
585 if (queue_trunc)
586 ceph_fscache_invalidate(inode);
587
Sage Weil355da1e2009-10-06 11:31:08 -0700588 return queue_trunc;
589}
590
591void ceph_fill_file_time(struct inode *inode, int issued,
592 u64 time_warp_seq, struct timespec *ctime,
593 struct timespec *mtime, struct timespec *atime)
594{
595 struct ceph_inode_info *ci = ceph_inode(inode);
596 int warn = 0;
597
598 if (issued & (CEPH_CAP_FILE_EXCL|
599 CEPH_CAP_FILE_WR|
Sage Weild8672d62010-11-08 09:24:34 -0800600 CEPH_CAP_FILE_BUFFER|
601 CEPH_CAP_AUTH_EXCL|
602 CEPH_CAP_XATTR_EXCL)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700603 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
604 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
605 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
606 ctime->tv_sec, ctime->tv_nsec);
607 inode->i_ctime = *ctime;
608 }
609 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
610 /* the MDS did a utimes() */
611 dout("mtime %ld.%09ld -> %ld.%09ld "
612 "tw %d -> %d\n",
613 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
614 mtime->tv_sec, mtime->tv_nsec,
615 ci->i_time_warp_seq, (int)time_warp_seq);
616
617 inode->i_mtime = *mtime;
618 inode->i_atime = *atime;
619 ci->i_time_warp_seq = time_warp_seq;
620 } else if (time_warp_seq == ci->i_time_warp_seq) {
621 /* nobody did utimes(); take the max */
622 if (timespec_compare(mtime, &inode->i_mtime) > 0) {
623 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
624 inode->i_mtime.tv_sec,
625 inode->i_mtime.tv_nsec,
626 mtime->tv_sec, mtime->tv_nsec);
627 inode->i_mtime = *mtime;
628 }
629 if (timespec_compare(atime, &inode->i_atime) > 0) {
630 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
631 inode->i_atime.tv_sec,
632 inode->i_atime.tv_nsec,
633 atime->tv_sec, atime->tv_nsec);
634 inode->i_atime = *atime;
635 }
636 } else if (issued & CEPH_CAP_FILE_EXCL) {
637 /* we did a utimes(); ignore mds values */
638 } else {
639 warn = 1;
640 }
641 } else {
Sage Weild8672d62010-11-08 09:24:34 -0800642 /* we have no write|excl caps; whatever the MDS says is true */
Sage Weil355da1e2009-10-06 11:31:08 -0700643 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
644 inode->i_ctime = *ctime;
645 inode->i_mtime = *mtime;
646 inode->i_atime = *atime;
647 ci->i_time_warp_seq = time_warp_seq;
648 } else {
649 warn = 1;
650 }
651 }
652 if (warn) /* time_warp_seq shouldn't go backwards */
653 dout("%p mds time_warp_seq %llu < %u\n",
654 inode, time_warp_seq, ci->i_time_warp_seq);
655}
656
657/*
658 * Populate an inode based on info from mds. May be called on new or
659 * existing inodes.
660 */
661static int fill_inode(struct inode *inode,
662 struct ceph_mds_reply_info_in *iinfo,
663 struct ceph_mds_reply_dirfrag *dirinfo,
664 struct ceph_mds_session *session,
665 unsigned long ttl_from, int cap_fmode,
666 struct ceph_cap_reservation *caps_reservation)
667{
Yan, Zhengd9df2782014-04-18 09:57:11 +0800668 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700669 struct ceph_mds_reply_inode *info = iinfo->in;
670 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengf98a1282014-04-17 08:55:50 +0800671 int issued = 0, implemented, new_issued;
Sage Weil355da1e2009-10-06 11:31:08 -0700672 struct timespec mtime, atime, ctime;
Sage Weil355da1e2009-10-06 11:31:08 -0700673 struct ceph_buffer *xattr_blob = NULL;
Yan, Zhengd9df2782014-04-18 09:57:11 +0800674 struct ceph_cap *new_cap = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700675 int err = 0;
Yan, Zhengd9df2782014-04-18 09:57:11 +0800676 bool wake = false;
Yan, Zhengf98a1282014-04-17 08:55:50 +0800677 bool queue_trunc = false;
678 bool new_version = false;
Sage Weil355da1e2009-10-06 11:31:08 -0700679
680 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
681 inode, ceph_vinop(inode), le64_to_cpu(info->version),
682 ci->i_version);
683
Yan, Zhengd9df2782014-04-18 09:57:11 +0800684 /* prealloc new cap struct */
685 if (info->cap.caps && ceph_snap(inode) == CEPH_NOSNAP)
686 new_cap = ceph_get_cap(mdsc, caps_reservation);
687
Sage Weil355da1e2009-10-06 11:31:08 -0700688 /*
689 * prealloc xattr data, if it looks like we'll need it. only
690 * if len > 4 (meaning there are actually xattrs; the first 4
691 * bytes are the xattr count).
692 */
693 if (iinfo->xattr_len > 4) {
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800694 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700695 if (!xattr_blob)
696 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
697 iinfo->xattr_len);
698 }
699
Sage Weilbe655592011-11-30 09:47:09 -0800700 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700701
702 /*
703 * provided version will be odd if inode value is projected,
Sage Weil8bd59e02010-11-08 09:23:12 -0800704 * even if stable. skip the update if we have newer stable
705 * info (ours>=theirs, e.g. due to racing mds replies), unless
706 * we are getting projected (unstable) info (in which case the
707 * version is odd, and we want ours>theirs).
708 * us them
709 * 2 2 skip
710 * 3 2 skip
711 * 3 3 update
Sage Weil355da1e2009-10-06 11:31:08 -0700712 */
Yan, Zhengf98a1282014-04-17 08:55:50 +0800713 if (ci->i_version == 0 ||
714 ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
715 le64_to_cpu(info->version) > (ci->i_version & ~1)))
716 new_version = true;
717
Sage Weil355da1e2009-10-06 11:31:08 -0700718 issued = __ceph_caps_issued(ci, &implemented);
719 issued |= implemented | __ceph_caps_dirty(ci);
Yan, Zhengf98a1282014-04-17 08:55:50 +0800720 new_issued = ~issued & le32_to_cpu(info->cap.caps);
Sage Weil355da1e2009-10-06 11:31:08 -0700721
722 /* update inode */
723 ci->i_version = le64_to_cpu(info->version);
724 inode->i_version++;
725 inode->i_rdev = le32_to_cpu(info->rdev);
Yan, Zhengf98a1282014-04-17 08:55:50 +0800726 inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700727
Yan, Zhengf98a1282014-04-17 08:55:50 +0800728 if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
729 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
Sage Weil355da1e2009-10-06 11:31:08 -0700730 inode->i_mode = le32_to_cpu(info->mode);
Eric W. Biedermanab871b92013-01-31 03:40:12 -0800731 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
732 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
Sage Weil355da1e2009-10-06 11:31:08 -0700733 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -0800734 from_kuid(&init_user_ns, inode->i_uid),
735 from_kgid(&init_user_ns, inode->i_gid));
Sage Weil355da1e2009-10-06 11:31:08 -0700736 }
737
Yan, Zhengf98a1282014-04-17 08:55:50 +0800738 if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
739 (issued & CEPH_CAP_LINK_EXCL) == 0)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200740 set_nlink(inode, le32_to_cpu(info->nlink));
Sage Weil355da1e2009-10-06 11:31:08 -0700741
Yan, Zhengf98a1282014-04-17 08:55:50 +0800742 if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
743 /* be careful with mtime, atime, size */
744 ceph_decode_timespec(&atime, &info->atime);
745 ceph_decode_timespec(&mtime, &info->mtime);
746 ceph_decode_timespec(&ctime, &info->ctime);
747 ceph_fill_file_time(inode, issued,
748 le32_to_cpu(info->time_warp_seq),
749 &ctime, &mtime, &atime);
750 }
Sage Weil355da1e2009-10-06 11:31:08 -0700751
Yan, Zhengf98a1282014-04-17 08:55:50 +0800752 if (new_version ||
753 (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
754 ci->i_layout = info->layout;
755 queue_trunc = ceph_fill_file_size(inode, issued,
756 le32_to_cpu(info->truncate_seq),
757 le64_to_cpu(info->truncate_size),
758 le64_to_cpu(info->size));
759 /* only update max_size on auth cap */
760 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
761 ci->i_max_size != le64_to_cpu(info->max_size)) {
762 dout("max_size %lld -> %llu\n", ci->i_max_size,
763 le64_to_cpu(info->max_size));
764 ci->i_max_size = le64_to_cpu(info->max_size);
765 }
766 }
Sage Weil355da1e2009-10-06 11:31:08 -0700767
768 /* xattrs */
769 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800770 if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
Sage Weil355da1e2009-10-06 11:31:08 -0700771 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
772 if (ci->i_xattrs.blob)
773 ceph_buffer_put(ci->i_xattrs.blob);
774 ci->i_xattrs.blob = xattr_blob;
775 if (xattr_blob)
776 memcpy(ci->i_xattrs.blob->vec.iov_base,
777 iinfo->xattr_data, iinfo->xattr_len);
778 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800779 ceph_forget_all_cached_acls(inode);
Sage Weila6424e42010-04-29 09:28:11 -0700780 xattr_blob = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700781 }
782
783 inode->i_mapping->a_ops = &ceph_aops;
784 inode->i_mapping->backing_dev_info =
Cheng Renquan640ef792010-03-26 17:40:33 +0800785 &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
Sage Weil355da1e2009-10-06 11:31:08 -0700786
787 switch (inode->i_mode & S_IFMT) {
788 case S_IFIFO:
789 case S_IFBLK:
790 case S_IFCHR:
791 case S_IFSOCK:
792 init_special_inode(inode, inode->i_mode, inode->i_rdev);
793 inode->i_op = &ceph_file_iops;
794 break;
795 case S_IFREG:
796 inode->i_op = &ceph_file_iops;
797 inode->i_fop = &ceph_file_fops;
798 break;
799 case S_IFLNK:
800 inode->i_op = &ceph_symlink_iops;
801 if (!ci->i_symlink) {
Xi Wang810339ec2012-02-03 09:55:36 -0500802 u32 symlen = iinfo->symlink_len;
Sage Weil355da1e2009-10-06 11:31:08 -0700803 char *sym;
804
Sage Weilbe655592011-11-30 09:47:09 -0800805 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700806
Xi Wang810339ec2012-02-03 09:55:36 -0500807 err = -EINVAL;
808 if (WARN_ON(symlen != inode->i_size))
809 goto out;
810
Sage Weil355da1e2009-10-06 11:31:08 -0700811 err = -ENOMEM;
Xi Wang810339ec2012-02-03 09:55:36 -0500812 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700813 if (!sym)
814 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700815
Sage Weilbe655592011-11-30 09:47:09 -0800816 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700817 if (!ci->i_symlink)
818 ci->i_symlink = sym;
819 else
820 kfree(sym); /* lost a race */
821 }
822 break;
823 case S_IFDIR:
824 inode->i_op = &ceph_dir_iops;
825 inode->i_fop = &ceph_dir_fops;
826
Sage Weil14303d22010-12-14 17:37:52 -0800827 ci->i_dir_layout = iinfo->dir_layout;
828
Sage Weil355da1e2009-10-06 11:31:08 -0700829 ci->i_files = le64_to_cpu(info->files);
830 ci->i_subdirs = le64_to_cpu(info->subdirs);
831 ci->i_rbytes = le64_to_cpu(info->rbytes);
832 ci->i_rfiles = le64_to_cpu(info->rfiles);
833 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
834 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
Sage Weil355da1e2009-10-06 11:31:08 -0700835 break;
836 default:
837 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
838 ceph_vinop(inode), inode->i_mode);
839 }
840
Yan, Zhenga8673d62013-02-18 16:38:14 +0800841 /* set dir completion flag? */
842 if (S_ISDIR(inode->i_mode) &&
843 ci->i_files == 0 && ci->i_subdirs == 0 &&
844 ceph_snap(inode) == CEPH_NOSNAP &&
845 (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
846 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
Yan, Zheng2f276c52013-03-13 19:44:32 +0800847 !__ceph_dir_is_complete(ci)) {
Yan, Zhenga8673d62013-02-18 16:38:14 +0800848 dout(" marking %p complete (empty)\n", inode);
Yan, Zheng70db4f32014-10-21 18:09:56 -0700849 __ceph_dir_set_complete(ci, atomic_read(&ci->i_release_count),
850 ci->i_ordered_count);
Yan, Zhenga8673d62013-02-18 16:38:14 +0800851 }
Sage Weil355da1e2009-10-06 11:31:08 -0700852
853 /* were we issued a capability? */
854 if (info->cap.caps) {
855 if (ceph_snap(inode) == CEPH_NOSNAP) {
856 ceph_add_cap(inode, session,
857 le64_to_cpu(info->cap.cap_id),
858 cap_fmode,
859 le32_to_cpu(info->cap.caps),
860 le32_to_cpu(info->cap.wanted),
861 le32_to_cpu(info->cap.seq),
862 le32_to_cpu(info->cap.mseq),
863 le64_to_cpu(info->cap.realm),
Yan, Zhengd9df2782014-04-18 09:57:11 +0800864 info->cap.flags, &new_cap);
865 wake = true;
Sage Weil355da1e2009-10-06 11:31:08 -0700866 } else {
Sage Weil355da1e2009-10-06 11:31:08 -0700867 dout(" %p got snap_caps %s\n", inode,
868 ceph_cap_string(le32_to_cpu(info->cap.caps)));
869 ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
870 if (cap_fmode >= 0)
871 __ceph_get_fmode(ci, cap_fmode);
Sage Weil355da1e2009-10-06 11:31:08 -0700872 }
Sage Weil04d000e2010-05-07 11:26:34 -0700873 } else if (cap_fmode >= 0) {
Fabian Frederickf3ae1b92014-06-06 14:35:37 -0700874 pr_warn("mds issued no caps on %llx.%llx\n",
Sage Weil04d000e2010-05-07 11:26:34 -0700875 ceph_vinop(inode));
876 __ceph_get_fmode(ci, cap_fmode);
Sage Weil355da1e2009-10-06 11:31:08 -0700877 }
Sage Weil355da1e2009-10-06 11:31:08 -0700878 spin_unlock(&ci->i_ceph_lock);
879
Yan, Zhengd9df2782014-04-18 09:57:11 +0800880 if (wake)
881 wake_up_all(&ci->i_cap_wq);
882
Sage Weil355da1e2009-10-06 11:31:08 -0700883 /* queue truncate if we saw i_size decrease */
884 if (queue_trunc)
885 ceph_queue_vmtruncate(inode);
886
887 /* populate frag tree */
Yan, Zheng3e7fbe92014-04-18 16:00:30 +0800888 if (S_ISDIR(inode->i_mode))
889 ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
Sage Weil355da1e2009-10-06 11:31:08 -0700890
891 /* update delegation info? */
892 if (dirinfo)
893 ceph_fill_dirfrag(inode, dirinfo);
894
895 err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700896out:
Yan, Zhengd9df2782014-04-18 09:57:11 +0800897 if (new_cap)
898 ceph_put_cap(mdsc, new_cap);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800899 if (xattr_blob)
900 ceph_buffer_put(xattr_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700901 return err;
902}
903
904/*
905 * caller should hold session s_mutex.
906 */
907static void update_dentry_lease(struct dentry *dentry,
908 struct ceph_mds_reply_lease *lease,
909 struct ceph_mds_session *session,
910 unsigned long from_time)
911{
912 struct ceph_dentry_info *di = ceph_dentry(dentry);
913 long unsigned duration = le32_to_cpu(lease->duration_ms);
914 long unsigned ttl = from_time + (duration * HZ) / 1000;
915 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
916 struct inode *dir;
917
918 /* only track leases on regular dentries */
919 if (dentry->d_op != &ceph_dentry_ops)
920 return;
921
922 spin_lock(&dentry->d_lock);
Sage Weil2f90b852011-07-26 11:28:25 -0700923 dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
924 dentry, duration, ttl);
Sage Weil355da1e2009-10-06 11:31:08 -0700925
926 /* make lease_rdcache_gen match directory */
927 dir = dentry->d_parent->d_inode;
928 di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
929
Sage Weil2f90b852011-07-26 11:28:25 -0700930 if (duration == 0)
Sage Weil355da1e2009-10-06 11:31:08 -0700931 goto out_unlock;
932
933 if (di->lease_gen == session->s_cap_gen &&
934 time_before(ttl, dentry->d_time))
935 goto out_unlock; /* we already have a newer lease. */
936
937 if (di->lease_session && di->lease_session != session)
938 goto out_unlock;
939
940 ceph_dentry_lru_touch(dentry);
941
942 if (!di->lease_session)
943 di->lease_session = ceph_get_mds_session(session);
944 di->lease_gen = session->s_cap_gen;
945 di->lease_seq = le32_to_cpu(lease->seq);
946 di->lease_renew_after = half_ttl;
947 di->lease_renew_from = 0;
948 dentry->d_time = ttl;
949out_unlock:
950 spin_unlock(&dentry->d_lock);
951 return;
952}
953
954/*
Sage Weil1cd39352010-05-03 22:08:02 -0700955 * splice a dentry to an inode.
956 * caller must hold directory i_mutex for this to be safe.
957 *
958 * we will only rehash the resulting dentry if @prehash is
959 * true; @prehash will be set to false (for the benefit of
960 * the caller) if we fail.
961 */
962static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
Yan, Zheng0a8a70f2014-04-14 13:13:02 +0800963 bool *prehash)
Sage Weil1cd39352010-05-03 22:08:02 -0700964{
965 struct dentry *realdn;
966
967 BUG_ON(dn->d_inode);
968
969 /* dn must be unhashed */
970 if (!d_unhashed(dn))
971 d_drop(dn);
972 realdn = d_materialise_unique(dn, in);
973 if (IS_ERR(realdn)) {
Sage Weild69ed052010-06-21 10:38:14 -0700974 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
975 PTR_ERR(realdn), dn, in, ceph_vinop(in));
Sage Weil1cd39352010-05-03 22:08:02 -0700976 if (prehash)
977 *prehash = false; /* don't rehash on error */
978 dn = realdn; /* note realdn contains the error */
979 goto out;
980 } else if (realdn) {
981 dout("dn %p (%d) spliced with %p (%d) "
982 "inode %p ino %llx.%llx\n",
Al Viro84d08fa2013-07-05 18:59:33 +0400983 dn, d_count(dn),
984 realdn, d_count(realdn),
Sage Weil1cd39352010-05-03 22:08:02 -0700985 realdn->d_inode, ceph_vinop(realdn->d_inode));
986 dput(dn);
987 dn = realdn;
988 } else {
989 BUG_ON(!ceph_dentry(dn));
990 dout("dn %p attached to %p ino %llx.%llx\n",
991 dn, dn->d_inode, ceph_vinop(dn->d_inode));
992 }
993 if ((!prehash || *prehash) && d_unhashed(dn))
994 d_rehash(dn);
Sage Weil1cd39352010-05-03 22:08:02 -0700995out:
996 return dn;
997}
998
999/*
Sage Weil355da1e2009-10-06 11:31:08 -07001000 * Incorporate results into the local cache. This is either just
1001 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1002 * after a lookup).
1003 *
1004 * A reply may contain
1005 * a directory inode along with a dentry.
1006 * and/or a target inode
1007 *
1008 * Called with snap_rwsem (read).
1009 */
1010int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
1011 struct ceph_mds_session *session)
1012{
1013 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1014 struct inode *in = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -07001015 struct ceph_vino vino;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001016 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
Sage Weil355da1e2009-10-06 11:31:08 -07001017 int err = 0;
1018
1019 dout("fill_trace %p is_dentry %d is_target %d\n", req,
1020 rinfo->head->is_dentry, rinfo->head->is_target);
1021
1022#if 0
1023 /*
1024 * Debugging hook:
1025 *
1026 * If we resend completed ops to a recovering mds, we get no
1027 * trace. Since that is very rare, pretend this is the case
1028 * to ensure the 'no trace' handlers in the callers behave.
1029 *
1030 * Fill in inodes unconditionally to avoid breaking cap
1031 * invariants.
1032 */
1033 if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
1034 pr_info("fill_trace faking empty trace on %lld %s\n",
1035 req->r_tid, ceph_mds_op_name(rinfo->head->op));
1036 if (rinfo->head->is_dentry) {
1037 rinfo->head->is_dentry = 0;
1038 err = fill_inode(req->r_locked_dir,
1039 &rinfo->diri, rinfo->dirfrag,
1040 session, req->r_request_started, -1);
1041 }
1042 if (rinfo->head->is_target) {
1043 rinfo->head->is_target = 0;
1044 ininfo = rinfo->targeti.in;
1045 vino.ino = le64_to_cpu(ininfo->ino);
1046 vino.snap = le64_to_cpu(ininfo->snapid);
1047 in = ceph_get_inode(sb, vino);
1048 err = fill_inode(in, &rinfo->targeti, NULL,
1049 session, req->r_request_started,
1050 req->r_fmode);
1051 iput(in);
1052 }
1053 }
1054#endif
1055
1056 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1057 dout("fill_trace reply is empty!\n");
Sage Weil167c9e32010-05-14 10:02:57 -07001058 if (rinfo->head->result == 0 && req->r_locked_dir)
1059 ceph_invalidate_dir_request(req);
Sage Weil355da1e2009-10-06 11:31:08 -07001060 return 0;
1061 }
1062
1063 if (rinfo->head->is_dentry) {
Sage Weil5b1daec2010-01-25 11:33:08 -08001064 struct inode *dir = req->r_locked_dir;
1065
Sage Weil6c5e50f2012-08-21 15:55:25 -07001066 if (dir) {
1067 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
1068 session, req->r_request_started, -1,
1069 &req->r_caps_reservation);
1070 if (err < 0)
Yan, Zheng19913b42014-03-06 16:40:32 +08001071 goto done;
Sage Weil6c5e50f2012-08-21 15:55:25 -07001072 } else {
1073 WARN_ON_ONCE(1);
1074 }
Yan, Zheng19913b42014-03-06 16:40:32 +08001075
1076 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME) {
1077 struct qstr dname;
1078 struct dentry *dn, *parent;
1079
1080 BUG_ON(!rinfo->head->is_target);
1081 BUG_ON(req->r_dentry);
1082
1083 parent = d_find_any_alias(dir);
1084 BUG_ON(!parent);
1085
1086 dname.name = rinfo->dname;
1087 dname.len = rinfo->dname_len;
1088 dname.hash = full_name_hash(dname.name, dname.len);
1089 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1090 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1091retry_lookup:
1092 dn = d_lookup(parent, &dname);
1093 dout("d_lookup on parent=%p name=%.*s got %p\n",
1094 parent, dname.len, dname.name, dn);
1095
1096 if (!dn) {
1097 dn = d_alloc(parent, &dname);
1098 dout("d_alloc %p '%.*s' = %p\n", parent,
1099 dname.len, dname.name, dn);
1100 if (dn == NULL) {
1101 dput(parent);
1102 err = -ENOMEM;
1103 goto done;
1104 }
1105 err = ceph_init_dentry(dn);
1106 if (err < 0) {
1107 dput(dn);
1108 dput(parent);
1109 goto done;
1110 }
1111 } else if (dn->d_inode &&
1112 (ceph_ino(dn->d_inode) != vino.ino ||
1113 ceph_snap(dn->d_inode) != vino.snap)) {
1114 dout(" dn %p points to wrong inode %p\n",
1115 dn, dn->d_inode);
1116 d_delete(dn);
1117 dput(dn);
1118 goto retry_lookup;
1119 }
1120
1121 req->r_dentry = dn;
1122 dput(parent);
1123 }
Sage Weil5b1daec2010-01-25 11:33:08 -08001124 }
1125
Yan, Zheng86b58d12013-12-05 12:38:59 +08001126 if (rinfo->head->is_target) {
1127 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1128 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1129
1130 in = ceph_get_inode(sb, vino);
1131 if (IS_ERR(in)) {
1132 err = PTR_ERR(in);
1133 goto done;
1134 }
1135 req->r_target_inode = in;
1136
1137 err = fill_inode(in, &rinfo->targeti, NULL,
1138 session, req->r_request_started,
Yan, Zheng48193012014-04-01 20:34:18 +08001139 (!req->r_aborted && rinfo->head->result == 0) ?
Yan, Zheng86b58d12013-12-05 12:38:59 +08001140 req->r_fmode : -1,
1141 &req->r_caps_reservation);
1142 if (err < 0) {
1143 pr_err("fill_inode badness %p %llx.%llx\n",
1144 in, ceph_vinop(in));
1145 goto done;
1146 }
1147 }
1148
Sage Weil9358c6d2010-03-30 13:54:41 -07001149 /*
1150 * ignore null lease/binding on snapdir ENOENT, or else we
1151 * will have trouble splicing in the virtual snapdir later
1152 */
1153 if (rinfo->head->is_dentry && !req->r_aborted &&
Sage Weil6c5e50f2012-08-21 15:55:25 -07001154 req->r_locked_dir &&
Sage Weil9358c6d2010-03-30 13:54:41 -07001155 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001156 fsc->mount_options->snapdir_name,
Sage Weil9358c6d2010-03-30 13:54:41 -07001157 req->r_dentry->d_name.len))) {
Sage Weil355da1e2009-10-06 11:31:08 -07001158 /*
1159 * lookup link rename : null -> possibly existing inode
1160 * mknod symlink mkdir : null -> new inode
1161 * unlink : linked -> null
1162 */
1163 struct inode *dir = req->r_locked_dir;
1164 struct dentry *dn = req->r_dentry;
1165 bool have_dir_cap, have_lease;
1166
1167 BUG_ON(!dn);
1168 BUG_ON(!dir);
1169 BUG_ON(dn->d_parent->d_inode != dir);
1170 BUG_ON(ceph_ino(dir) !=
1171 le64_to_cpu(rinfo->diri.in->ino));
1172 BUG_ON(ceph_snap(dir) !=
1173 le64_to_cpu(rinfo->diri.in->snapid));
1174
Sage Weil355da1e2009-10-06 11:31:08 -07001175 /* do we have a lease on the whole dir? */
1176 have_dir_cap =
1177 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1178 CEPH_CAP_FILE_SHARED);
1179
1180 /* do we have a dn lease? */
1181 have_lease = have_dir_cap ||
Sage Weil2f90b852011-07-26 11:28:25 -07001182 le32_to_cpu(rinfo->dlease->duration_ms);
Sage Weil355da1e2009-10-06 11:31:08 -07001183 if (!have_lease)
1184 dout("fill_trace no dentry lease or dir cap\n");
1185
1186 /* rename? */
1187 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
Yan, Zheng0a8a70f2014-04-14 13:13:02 +08001188 struct inode *olddir = req->r_old_dentry_dir;
1189 BUG_ON(!olddir);
1190
Sage Weil355da1e2009-10-06 11:31:08 -07001191 dout(" src %p '%.*s' dst %p '%.*s'\n",
1192 req->r_old_dentry,
1193 req->r_old_dentry->d_name.len,
1194 req->r_old_dentry->d_name.name,
1195 dn, dn->d_name.len, dn->d_name.name);
1196 dout("fill_trace doing d_move %p -> %p\n",
1197 req->r_old_dentry, dn);
Sage Weilc10f5e12010-04-16 12:56:11 -07001198
Sage Weil355da1e2009-10-06 11:31:08 -07001199 d_move(req->r_old_dentry, dn);
1200 dout(" src %p '%.*s' dst %p '%.*s'\n",
1201 req->r_old_dentry,
1202 req->r_old_dentry->d_name.len,
1203 req->r_old_dentry->d_name.name,
1204 dn, dn->d_name.len, dn->d_name.name);
Sage Weil81a6cf22010-05-14 09:35:38 -07001205
Sage Weilc4a29f22009-12-21 11:42:18 -08001206 /* ensure target dentry is invalidated, despite
1207 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001208 ceph_invalidate_dentry_lease(dn);
1209
Yan, Zheng0a8a70f2014-04-14 13:13:02 +08001210 /* d_move screws up sibling dentries' offsets */
Yan, Zheng70db4f32014-10-21 18:09:56 -07001211 ceph_dir_clear_ordered(dir);
1212 ceph_dir_clear_ordered(olddir);
Yan, Zheng0a8a70f2014-04-14 13:13:02 +08001213
Milosz Tanski99ccbd22013-08-21 17:29:54 -04001214 dout("dn %p gets new offset %lld\n", req->r_old_dentry,
Sage Weil1cd39352010-05-03 22:08:02 -07001215 ceph_dentry(req->r_old_dentry)->offset);
Sage Weil81a6cf22010-05-14 09:35:38 -07001216
Sage Weil355da1e2009-10-06 11:31:08 -07001217 dn = req->r_old_dentry; /* use old_dentry */
Sage Weil355da1e2009-10-06 11:31:08 -07001218 }
1219
1220 /* null dentry? */
1221 if (!rinfo->head->is_target) {
1222 dout("fill_trace null dentry\n");
1223 if (dn->d_inode) {
Yan, Zheng70db4f32014-10-21 18:09:56 -07001224 ceph_dir_clear_ordered(dir);
Sage Weil355da1e2009-10-06 11:31:08 -07001225 dout("d_delete %p\n", dn);
1226 d_delete(dn);
1227 } else {
1228 dout("d_instantiate %p NULL\n", dn);
1229 d_instantiate(dn, NULL);
1230 if (have_lease && d_unhashed(dn))
1231 d_rehash(dn);
1232 update_dentry_lease(dn, rinfo->dlease,
1233 session,
1234 req->r_request_started);
1235 }
1236 goto done;
1237 }
1238
1239 /* attach proper inode */
Yan, Zheng86b58d12013-12-05 12:38:59 +08001240 if (!dn->d_inode) {
Yan, Zheng70db4f32014-10-21 18:09:56 -07001241 ceph_dir_clear_ordered(dir);
Yan, Zheng86b58d12013-12-05 12:38:59 +08001242 ihold(in);
Yan, Zheng0a8a70f2014-04-14 13:13:02 +08001243 dn = splice_dentry(dn, in, &have_lease);
Sage Weil355da1e2009-10-06 11:31:08 -07001244 if (IS_ERR(dn)) {
1245 err = PTR_ERR(dn);
1246 goto done;
1247 }
1248 req->r_dentry = dn; /* may have spliced */
Yan, Zheng86b58d12013-12-05 12:38:59 +08001249 } else if (dn->d_inode && dn->d_inode != in) {
Sage Weil355da1e2009-10-06 11:31:08 -07001250 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
Yan, Zheng86b58d12013-12-05 12:38:59 +08001251 dn, dn->d_inode, ceph_vinop(dn->d_inode),
1252 ceph_vinop(in));
Sage Weil355da1e2009-10-06 11:31:08 -07001253 have_lease = false;
Sage Weil355da1e2009-10-06 11:31:08 -07001254 }
1255
1256 if (have_lease)
1257 update_dentry_lease(dn, rinfo->dlease, session,
1258 req->r_request_started);
1259 dout(" final dn %p\n", dn);
Yan, Zheng86b58d12013-12-05 12:38:59 +08001260 } else if (!req->r_aborted &&
1261 (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1262 req->r_op == CEPH_MDS_OP_MKSNAP)) {
Sage Weil355da1e2009-10-06 11:31:08 -07001263 struct dentry *dn = req->r_dentry;
Yan, Zheng0a8a70f2014-04-14 13:13:02 +08001264 struct inode *dir = req->r_locked_dir;
Sage Weil355da1e2009-10-06 11:31:08 -07001265
1266 /* fill out a snapdir LOOKUPSNAP dentry */
1267 BUG_ON(!dn);
Yan, Zheng0a8a70f2014-04-14 13:13:02 +08001268 BUG_ON(!dir);
1269 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
Sage Weil355da1e2009-10-06 11:31:08 -07001270 dout(" linking snapped dir %p to dn %p\n", in, dn);
Yan, Zheng70db4f32014-10-21 18:09:56 -07001271 ceph_dir_clear_ordered(dir);
Yan, Zheng86b58d12013-12-05 12:38:59 +08001272 ihold(in);
Yan, Zheng0a8a70f2014-04-14 13:13:02 +08001273 dn = splice_dentry(dn, in, NULL);
Sage Weil355da1e2009-10-06 11:31:08 -07001274 if (IS_ERR(dn)) {
1275 err = PTR_ERR(dn);
1276 goto done;
1277 }
1278 req->r_dentry = dn; /* may have spliced */
Sage Weil355da1e2009-10-06 11:31:08 -07001279 }
Sage Weil355da1e2009-10-06 11:31:08 -07001280done:
1281 dout("fill_trace done err=%d\n", err);
1282 return err;
1283}
1284
1285/*
1286 * Prepopulate our cache with readdir results, leases, etc.
1287 */
Sage Weil79f9f992013-01-29 02:55:31 -05001288static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1289 struct ceph_mds_session *session)
1290{
1291 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1292 int i, err = 0;
1293
1294 for (i = 0; i < rinfo->dir_nr; i++) {
1295 struct ceph_vino vino;
1296 struct inode *in;
1297 int rc;
1298
1299 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1300 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1301
1302 in = ceph_get_inode(req->r_dentry->d_sb, vino);
1303 if (IS_ERR(in)) {
1304 err = PTR_ERR(in);
1305 dout("new_inode badness got %d\n", err);
1306 continue;
1307 }
1308 rc = fill_inode(in, &rinfo->dir_in[i], NULL, session,
1309 req->r_request_started, -1,
1310 &req->r_caps_reservation);
1311 if (rc < 0) {
1312 pr_err("fill_inode badness on %p got %d\n", in, rc);
1313 err = rc;
1314 continue;
1315 }
1316 }
1317
1318 return err;
1319}
1320
Sage Weil355da1e2009-10-06 11:31:08 -07001321int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1322 struct ceph_mds_session *session)
1323{
1324 struct dentry *parent = req->r_dentry;
1325 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1326 struct qstr dname;
1327 struct dentry *dn;
1328 struct inode *in;
Yan, Zheng86b58d12013-12-05 12:38:59 +08001329 int err = 0, ret, i;
Sage Weil355da1e2009-10-06 11:31:08 -07001330 struct inode *snapdir = NULL;
1331 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
Sage Weil355da1e2009-10-06 11:31:08 -07001332 struct ceph_dentry_info *di;
Yan, Zheng81c6aea2013-09-18 09:44:13 +08001333 u64 r_readdir_offset = req->r_readdir_offset;
1334 u32 frag = le32_to_cpu(rhead->args.readdir.frag);
1335
1336 if (rinfo->dir_dir &&
1337 le32_to_cpu(rinfo->dir_dir->frag) != frag) {
1338 dout("readdir_prepopulate got new frag %x -> %x\n",
1339 frag, le32_to_cpu(rinfo->dir_dir->frag));
1340 frag = le32_to_cpu(rinfo->dir_dir->frag);
1341 if (ceph_frag_is_leftmost(frag))
1342 r_readdir_offset = 2;
1343 else
1344 r_readdir_offset = 0;
1345 }
Sage Weil355da1e2009-10-06 11:31:08 -07001346
Sage Weil79f9f992013-01-29 02:55:31 -05001347 if (req->r_aborted)
1348 return readdir_prepopulate_inodes_only(req, session);
1349
Sage Weil355da1e2009-10-06 11:31:08 -07001350 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1351 snapdir = ceph_get_snapdir(parent->d_inode);
1352 parent = d_find_alias(snapdir);
1353 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1354 rinfo->dir_nr, parent);
1355 } else {
1356 dout("readdir_prepopulate %d items under dn %p\n",
1357 rinfo->dir_nr, parent);
1358 if (rinfo->dir_dir)
1359 ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1360 }
1361
Yan, Zheng86b58d12013-12-05 12:38:59 +08001362 /* FIXME: release caps/leases if error occurs */
Sage Weil355da1e2009-10-06 11:31:08 -07001363 for (i = 0; i < rinfo->dir_nr; i++) {
1364 struct ceph_vino vino;
1365
1366 dname.name = rinfo->dir_dname[i];
1367 dname.len = rinfo->dir_dname_len[i];
1368 dname.hash = full_name_hash(dname.name, dname.len);
1369
1370 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1371 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1372
1373retry_lookup:
1374 dn = d_lookup(parent, &dname);
1375 dout("d_lookup on parent=%p name=%.*s got %p\n",
1376 parent, dname.len, dname.name, dn);
1377
1378 if (!dn) {
1379 dn = d_alloc(parent, &dname);
1380 dout("d_alloc %p '%.*s' = %p\n", parent,
1381 dname.len, dname.name, dn);
1382 if (dn == NULL) {
1383 dout("d_alloc badness\n");
1384 err = -ENOMEM;
1385 goto out;
1386 }
Yan, Zheng86b58d12013-12-05 12:38:59 +08001387 ret = ceph_init_dentry(dn);
1388 if (ret < 0) {
Sage Weil8c696732010-07-22 14:11:56 -07001389 dput(dn);
Yan, Zheng86b58d12013-12-05 12:38:59 +08001390 err = ret;
Sage Weil355da1e2009-10-06 11:31:08 -07001391 goto out;
Sage Weil8c696732010-07-22 14:11:56 -07001392 }
Sage Weil355da1e2009-10-06 11:31:08 -07001393 } else if (dn->d_inode &&
1394 (ceph_ino(dn->d_inode) != vino.ino ||
1395 ceph_snap(dn->d_inode) != vino.snap)) {
1396 dout(" dn %p points to wrong inode %p\n",
1397 dn, dn->d_inode);
1398 d_delete(dn);
1399 dput(dn);
1400 goto retry_lookup;
1401 } else {
1402 /* reorder parent's d_subdirs */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001403 spin_lock(&parent->d_lock);
1404 spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
Sage Weil355da1e2009-10-06 11:31:08 -07001405 list_move(&dn->d_u.d_child, &parent->d_subdirs);
1406 spin_unlock(&dn->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001407 spin_unlock(&parent->d_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001408 }
1409
Sage Weil355da1e2009-10-06 11:31:08 -07001410 /* inode */
1411 if (dn->d_inode) {
1412 in = dn->d_inode;
1413 } else {
1414 in = ceph_get_inode(parent->d_sb, vino);
Dan Carpenterac1f12e2010-08-25 09:11:35 +02001415 if (IS_ERR(in)) {
Sage Weil355da1e2009-10-06 11:31:08 -07001416 dout("new_inode badness\n");
Al Viro2744c172012-09-26 21:41:05 -04001417 d_drop(dn);
Sage Weil355da1e2009-10-06 11:31:08 -07001418 dput(dn);
Dan Carpenterac1f12e2010-08-25 09:11:35 +02001419 err = PTR_ERR(in);
Sage Weil355da1e2009-10-06 11:31:08 -07001420 goto out;
1421 }
Sage Weil355da1e2009-10-06 11:31:08 -07001422 }
1423
1424 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1425 req->r_request_started, -1,
1426 &req->r_caps_reservation) < 0) {
1427 pr_err("fill_inode badness on %p\n", in);
Yan, Zheng86b58d12013-12-05 12:38:59 +08001428 if (!dn->d_inode)
1429 iput(in);
1430 d_drop(dn);
Sage Weild69ed052010-06-21 10:38:14 -07001431 goto next_item;
Sage Weil355da1e2009-10-06 11:31:08 -07001432 }
Yan, Zheng86b58d12013-12-05 12:38:59 +08001433
1434 if (!dn->d_inode) {
Yan, Zheng0a8a70f2014-04-14 13:13:02 +08001435 dn = splice_dentry(dn, in, NULL);
Yan, Zheng86b58d12013-12-05 12:38:59 +08001436 if (IS_ERR(dn)) {
1437 err = PTR_ERR(dn);
1438 dn = NULL;
1439 goto next_item;
1440 }
1441 }
1442
1443 di = dn->d_fsdata;
1444 di->offset = ceph_make_fpos(frag, i + r_readdir_offset);
1445
1446 update_dentry_lease(dn, rinfo->dir_dlease[i],
1447 req->r_session,
1448 req->r_request_started);
Sage Weild69ed052010-06-21 10:38:14 -07001449next_item:
1450 if (dn)
1451 dput(dn);
Sage Weil355da1e2009-10-06 11:31:08 -07001452 }
Yan, Zheng86b58d12013-12-05 12:38:59 +08001453 if (err == 0)
1454 req->r_did_prepopulate = true;
Sage Weil355da1e2009-10-06 11:31:08 -07001455
1456out:
1457 if (snapdir) {
1458 iput(snapdir);
1459 dput(parent);
1460 }
1461 dout("readdir_prepopulate done\n");
1462 return err;
1463}
1464
1465int ceph_inode_set_size(struct inode *inode, loff_t size)
1466{
1467 struct ceph_inode_info *ci = ceph_inode(inode);
1468 int ret = 0;
1469
Sage Weilbe655592011-11-30 09:47:09 -08001470 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001471 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1472 inode->i_size = size;
1473 inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1474
1475 /* tell the MDS if we are approaching max_size */
1476 if ((size << 1) >= ci->i_max_size &&
1477 (ci->i_reported_size << 1) < ci->i_max_size)
1478 ret = 1;
1479
Sage Weilbe655592011-11-30 09:47:09 -08001480 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001481 return ret;
1482}
1483
1484/*
1485 * Write back inode data in a worker thread. (This can't be done
1486 * in the message handler context.)
1487 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001488void ceph_queue_writeback(struct inode *inode)
1489{
Sage Weil15a20152011-11-05 22:06:31 -07001490 ihold(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001491 if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1492 &ceph_inode(inode)->i_wb_work)) {
Sage Weil2c27c9a2010-02-17 15:45:51 -08001493 dout("ceph_queue_writeback %p\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001494 } else {
Sage Weil2c27c9a2010-02-17 15:45:51 -08001495 dout("ceph_queue_writeback %p failed\n", inode);
Sage Weil15a20152011-11-05 22:06:31 -07001496 iput(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001497 }
1498}
1499
1500static void ceph_writeback_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001501{
1502 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1503 i_wb_work);
1504 struct inode *inode = &ci->vfs_inode;
1505
1506 dout("writeback %p\n", inode);
1507 filemap_fdatawrite(&inode->i_data);
1508 iput(inode);
1509}
1510
1511/*
Sage Weil3c6f6b72010-02-09 15:24:44 -08001512 * queue an async invalidation
1513 */
1514void ceph_queue_invalidate(struct inode *inode)
1515{
Sage Weil15a20152011-11-05 22:06:31 -07001516 ihold(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001517 if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1518 &ceph_inode(inode)->i_pg_inv_work)) {
1519 dout("ceph_queue_invalidate %p\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001520 } else {
1521 dout("ceph_queue_invalidate %p failed\n", inode);
Sage Weil15a20152011-11-05 22:06:31 -07001522 iput(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001523 }
1524}
1525
1526/*
Sage Weil355da1e2009-10-06 11:31:08 -07001527 * Invalidate inode pages in a worker thread. (This can't be done
1528 * in the message handler context.)
1529 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001530static void ceph_invalidate_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001531{
1532 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1533 i_pg_inv_work);
1534 struct inode *inode = &ci->vfs_inode;
1535 u32 orig_gen;
1536 int check = 0;
1537
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001538 mutex_lock(&ci->i_truncate_mutex);
Sage Weilbe655592011-11-30 09:47:09 -08001539 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001540 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1541 ci->i_rdcache_gen, ci->i_rdcache_revoking);
Sage Weilcd045cb2010-11-04 11:05:05 -07001542 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
Yan, Zheng9563f882013-11-22 13:50:45 +08001543 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1544 check = 1;
Sage Weilbe655592011-11-30 09:47:09 -08001545 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001546 mutex_unlock(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -07001547 goto out;
1548 }
1549 orig_gen = ci->i_rdcache_gen;
Sage Weilbe655592011-11-30 09:47:09 -08001550 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001551
Yan, Zheng4e217b52014-06-08 05:08:56 +08001552 truncate_pagecache(inode, 0);
Sage Weil355da1e2009-10-06 11:31:08 -07001553
Sage Weilbe655592011-11-30 09:47:09 -08001554 spin_lock(&ci->i_ceph_lock);
Sage Weilcd045cb2010-11-04 11:05:05 -07001555 if (orig_gen == ci->i_rdcache_gen &&
1556 orig_gen == ci->i_rdcache_revoking) {
Sage Weil355da1e2009-10-06 11:31:08 -07001557 dout("invalidate_pages %p gen %d successful\n", inode,
1558 ci->i_rdcache_gen);
Sage Weilcd045cb2010-11-04 11:05:05 -07001559 ci->i_rdcache_revoking--;
Sage Weil355da1e2009-10-06 11:31:08 -07001560 check = 1;
1561 } else {
Sage Weilcd045cb2010-11-04 11:05:05 -07001562 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1563 inode, orig_gen, ci->i_rdcache_gen,
1564 ci->i_rdcache_revoking);
Yan, Zheng9563f882013-11-22 13:50:45 +08001565 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1566 check = 1;
Sage Weil355da1e2009-10-06 11:31:08 -07001567 }
Sage Weilbe655592011-11-30 09:47:09 -08001568 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001569 mutex_unlock(&ci->i_truncate_mutex);
Yan, Zheng9563f882013-11-22 13:50:45 +08001570out:
Sage Weil355da1e2009-10-06 11:31:08 -07001571 if (check)
1572 ceph_check_caps(ci, 0, NULL);
Sage Weil355da1e2009-10-06 11:31:08 -07001573 iput(inode);
1574}
1575
1576
1577/*
Yan, Zheng3f999692013-03-01 10:57:54 +08001578 * called by trunc_wq;
Sage Weil355da1e2009-10-06 11:31:08 -07001579 *
1580 * We also truncate in a separate thread as well.
1581 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001582static void ceph_vmtruncate_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001583{
1584 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1585 i_vmtruncate_work);
1586 struct inode *inode = &ci->vfs_inode;
1587
1588 dout("vmtruncate_work %p\n", inode);
Yan, Zhengb415bf42013-07-02 12:40:19 +08001589 __ceph_do_pending_vmtruncate(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001590 iput(inode);
1591}
1592
1593/*
Sage Weil3c6f6b72010-02-09 15:24:44 -08001594 * Queue an async vmtruncate. If we fail to queue work, we will handle
1595 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1596 */
1597void ceph_queue_vmtruncate(struct inode *inode)
1598{
1599 struct ceph_inode_info *ci = ceph_inode(inode);
1600
Sage Weil15a20152011-11-05 22:06:31 -07001601 ihold(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -04001602
Cheng Renquan640ef792010-03-26 17:40:33 +08001603 if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
Sage Weil3c6f6b72010-02-09 15:24:44 -08001604 &ci->i_vmtruncate_work)) {
1605 dout("ceph_queue_vmtruncate %p\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001606 } else {
1607 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1608 inode, ci->i_truncate_pending);
Sage Weil15a20152011-11-05 22:06:31 -07001609 iput(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001610 }
1611}
1612
1613/*
Sage Weil355da1e2009-10-06 11:31:08 -07001614 * Make sure any pending truncation is applied before doing anything
1615 * that may depend on it.
1616 */
Yan, Zhengb415bf42013-07-02 12:40:19 +08001617void __ceph_do_pending_vmtruncate(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -07001618{
1619 struct ceph_inode_info *ci = ceph_inode(inode);
1620 u64 to;
Yan, Zhenga85f50b2012-11-19 10:49:08 +08001621 int wrbuffer_refs, finish = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001622
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001623 mutex_lock(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -07001624retry:
Sage Weilbe655592011-11-30 09:47:09 -08001625 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001626 if (ci->i_truncate_pending == 0) {
1627 dout("__do_pending_vmtruncate %p none pending\n", inode);
Sage Weilbe655592011-11-30 09:47:09 -08001628 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001629 mutex_unlock(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -07001630 return;
1631 }
1632
1633 /*
1634 * make sure any dirty snapped pages are flushed before we
1635 * possibly truncate them.. so write AND block!
1636 */
1637 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1638 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1639 inode);
Sage Weilbe655592011-11-30 09:47:09 -08001640 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001641 filemap_write_and_wait_range(&inode->i_data, 0,
1642 inode->i_sb->s_maxbytes);
1643 goto retry;
1644 }
1645
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001646 /* there should be no reader or writer */
1647 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
1648
Sage Weil355da1e2009-10-06 11:31:08 -07001649 to = ci->i_truncate_size;
1650 wrbuffer_refs = ci->i_wrbuffer_ref;
1651 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1652 ci->i_truncate_pending, to);
Sage Weilbe655592011-11-30 09:47:09 -08001653 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001654
Yan, Zheng4e217b52014-06-08 05:08:56 +08001655 truncate_pagecache(inode, to);
Sage Weil355da1e2009-10-06 11:31:08 -07001656
Sage Weilbe655592011-11-30 09:47:09 -08001657 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga85f50b2012-11-19 10:49:08 +08001658 if (to == ci->i_truncate_size) {
1659 ci->i_truncate_pending = 0;
1660 finish = 1;
1661 }
Sage Weilbe655592011-11-30 09:47:09 -08001662 spin_unlock(&ci->i_ceph_lock);
Yan, Zhenga85f50b2012-11-19 10:49:08 +08001663 if (!finish)
1664 goto retry;
Sage Weil355da1e2009-10-06 11:31:08 -07001665
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001666 mutex_unlock(&ci->i_truncate_mutex);
1667
Sage Weil355da1e2009-10-06 11:31:08 -07001668 if (wrbuffer_refs == 0)
1669 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
Yan, Zhenga85f50b2012-11-19 10:49:08 +08001670
1671 wake_up_all(&ci->i_cap_wq);
Sage Weil355da1e2009-10-06 11:31:08 -07001672}
1673
Sage Weil355da1e2009-10-06 11:31:08 -07001674/*
1675 * symlinks
1676 */
1677static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1678{
1679 struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1680 nd_set_link(nd, ci->i_symlink);
1681 return NULL;
1682}
1683
1684static const struct inode_operations ceph_symlink_iops = {
1685 .readlink = generic_readlink,
1686 .follow_link = ceph_sym_follow_link,
Yan, Zheng0b932672013-04-07 16:28:49 +08001687 .setattr = ceph_setattr,
1688 .getattr = ceph_getattr,
1689 .setxattr = ceph_setxattr,
1690 .getxattr = ceph_getxattr,
1691 .listxattr = ceph_listxattr,
1692 .removexattr = ceph_removexattr,
Sage Weil355da1e2009-10-06 11:31:08 -07001693};
1694
1695/*
1696 * setattr
1697 */
1698int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1699{
1700 struct inode *inode = dentry->d_inode;
1701 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001702 const unsigned int ia_valid = attr->ia_valid;
1703 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001704 struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -07001705 int issued;
1706 int release = 0, dirtied = 0;
1707 int mask = 0;
1708 int err = 0;
Sage Weilfca65b42011-05-04 11:33:47 -07001709 int inode_dirty_flags = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001710
1711 if (ceph_snap(inode) != CEPH_NOSNAP)
1712 return -EROFS;
1713
Sage Weil355da1e2009-10-06 11:31:08 -07001714 err = inode_change_ok(inode, attr);
1715 if (err != 0)
1716 return err;
1717
1718 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1719 USE_AUTH_MDS);
1720 if (IS_ERR(req))
1721 return PTR_ERR(req);
1722
Sage Weilbe655592011-11-30 09:47:09 -08001723 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001724 issued = __ceph_caps_issued(ci, NULL);
1725 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1726
1727 if (ia_valid & ATTR_UID) {
1728 dout("setattr %p uid %d -> %d\n", inode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -08001729 from_kuid(&init_user_ns, inode->i_uid),
1730 from_kuid(&init_user_ns, attr->ia_uid));
Sage Weil355da1e2009-10-06 11:31:08 -07001731 if (issued & CEPH_CAP_AUTH_EXCL) {
1732 inode->i_uid = attr->ia_uid;
1733 dirtied |= CEPH_CAP_AUTH_EXCL;
1734 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
Eric W. Biedermanab871b92013-01-31 03:40:12 -08001735 !uid_eq(attr->ia_uid, inode->i_uid)) {
1736 req->r_args.setattr.uid = cpu_to_le32(
1737 from_kuid(&init_user_ns, attr->ia_uid));
Sage Weil355da1e2009-10-06 11:31:08 -07001738 mask |= CEPH_SETATTR_UID;
1739 release |= CEPH_CAP_AUTH_SHARED;
1740 }
1741 }
1742 if (ia_valid & ATTR_GID) {
1743 dout("setattr %p gid %d -> %d\n", inode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -08001744 from_kgid(&init_user_ns, inode->i_gid),
1745 from_kgid(&init_user_ns, attr->ia_gid));
Sage Weil355da1e2009-10-06 11:31:08 -07001746 if (issued & CEPH_CAP_AUTH_EXCL) {
1747 inode->i_gid = attr->ia_gid;
1748 dirtied |= CEPH_CAP_AUTH_EXCL;
1749 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
Eric W. Biedermanab871b92013-01-31 03:40:12 -08001750 !gid_eq(attr->ia_gid, inode->i_gid)) {
1751 req->r_args.setattr.gid = cpu_to_le32(
1752 from_kgid(&init_user_ns, attr->ia_gid));
Sage Weil355da1e2009-10-06 11:31:08 -07001753 mask |= CEPH_SETATTR_GID;
1754 release |= CEPH_CAP_AUTH_SHARED;
1755 }
1756 }
1757 if (ia_valid & ATTR_MODE) {
1758 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1759 attr->ia_mode);
1760 if (issued & CEPH_CAP_AUTH_EXCL) {
1761 inode->i_mode = attr->ia_mode;
1762 dirtied |= CEPH_CAP_AUTH_EXCL;
1763 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1764 attr->ia_mode != inode->i_mode) {
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001765 inode->i_mode = attr->ia_mode;
Sage Weil355da1e2009-10-06 11:31:08 -07001766 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1767 mask |= CEPH_SETATTR_MODE;
1768 release |= CEPH_CAP_AUTH_SHARED;
1769 }
1770 }
1771
1772 if (ia_valid & ATTR_ATIME) {
1773 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1774 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1775 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1776 if (issued & CEPH_CAP_FILE_EXCL) {
1777 ci->i_time_warp_seq++;
1778 inode->i_atime = attr->ia_atime;
1779 dirtied |= CEPH_CAP_FILE_EXCL;
1780 } else if ((issued & CEPH_CAP_FILE_WR) &&
1781 timespec_compare(&inode->i_atime,
1782 &attr->ia_atime) < 0) {
1783 inode->i_atime = attr->ia_atime;
1784 dirtied |= CEPH_CAP_FILE_WR;
1785 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1786 !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1787 ceph_encode_timespec(&req->r_args.setattr.atime,
1788 &attr->ia_atime);
1789 mask |= CEPH_SETATTR_ATIME;
1790 release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1791 CEPH_CAP_FILE_WR;
1792 }
1793 }
1794 if (ia_valid & ATTR_MTIME) {
1795 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1796 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1797 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1798 if (issued & CEPH_CAP_FILE_EXCL) {
1799 ci->i_time_warp_seq++;
1800 inode->i_mtime = attr->ia_mtime;
1801 dirtied |= CEPH_CAP_FILE_EXCL;
1802 } else if ((issued & CEPH_CAP_FILE_WR) &&
1803 timespec_compare(&inode->i_mtime,
1804 &attr->ia_mtime) < 0) {
1805 inode->i_mtime = attr->ia_mtime;
1806 dirtied |= CEPH_CAP_FILE_WR;
1807 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1808 !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1809 ceph_encode_timespec(&req->r_args.setattr.mtime,
1810 &attr->ia_mtime);
1811 mask |= CEPH_SETATTR_MTIME;
1812 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1813 CEPH_CAP_FILE_WR;
1814 }
1815 }
1816 if (ia_valid & ATTR_SIZE) {
1817 dout("setattr %p size %lld -> %lld\n", inode,
1818 inode->i_size, attr->ia_size);
Sage Weil355da1e2009-10-06 11:31:08 -07001819 if ((issued & CEPH_CAP_FILE_EXCL) &&
1820 attr->ia_size > inode->i_size) {
1821 inode->i_size = attr->ia_size;
Sage Weil355da1e2009-10-06 11:31:08 -07001822 inode->i_blocks =
1823 (attr->ia_size + (1 << 9) - 1) >> 9;
1824 inode->i_ctime = attr->ia_ctime;
1825 ci->i_reported_size = attr->ia_size;
1826 dirtied |= CEPH_CAP_FILE_EXCL;
1827 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1828 attr->ia_size != inode->i_size) {
1829 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1830 req->r_args.setattr.old_size =
1831 cpu_to_le64(inode->i_size);
1832 mask |= CEPH_SETATTR_SIZE;
1833 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1834 CEPH_CAP_FILE_WR;
1835 }
1836 }
1837
1838 /* these do nothing */
1839 if (ia_valid & ATTR_CTIME) {
1840 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1841 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1842 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1843 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1844 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1845 only ? "ctime only" : "ignored");
1846 inode->i_ctime = attr->ia_ctime;
1847 if (only) {
1848 /*
1849 * if kernel wants to dirty ctime but nothing else,
1850 * we need to choose a cap to dirty under, or do
1851 * a almost-no-op setattr
1852 */
1853 if (issued & CEPH_CAP_AUTH_EXCL)
1854 dirtied |= CEPH_CAP_AUTH_EXCL;
1855 else if (issued & CEPH_CAP_FILE_EXCL)
1856 dirtied |= CEPH_CAP_FILE_EXCL;
1857 else if (issued & CEPH_CAP_XATTR_EXCL)
1858 dirtied |= CEPH_CAP_XATTR_EXCL;
1859 else
1860 mask |= CEPH_SETATTR_CTIME;
1861 }
1862 }
1863 if (ia_valid & ATTR_FILE)
1864 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1865
1866 if (dirtied) {
Sage Weilfca65b42011-05-04 11:33:47 -07001867 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied);
Sage Weil355da1e2009-10-06 11:31:08 -07001868 inode->i_ctime = CURRENT_TIME;
1869 }
1870
1871 release &= issued;
Sage Weilbe655592011-11-30 09:47:09 -08001872 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001873
Sage Weilfca65b42011-05-04 11:33:47 -07001874 if (inode_dirty_flags)
1875 __mark_inode_dirty(inode, inode_dirty_flags);
1876
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001877 if (ia_valid & ATTR_MODE) {
Linus Torvalds4db658e2014-01-28 18:06:18 -08001878 err = posix_acl_chmod(inode, attr->ia_mode);
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001879 if (err)
1880 goto out_put;
1881 }
1882
Sage Weil355da1e2009-10-06 11:31:08 -07001883 if (mask) {
Sage Weil70b666c2011-05-27 09:24:26 -07001884 req->r_inode = inode;
1885 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001886 req->r_inode_drop = release;
1887 req->r_args.setattr.mask = cpu_to_le32(mask);
1888 req->r_num_caps = 1;
Sage Weil752c8bd2013-02-05 13:52:29 -08001889 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -07001890 }
1891 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1892 ceph_cap_string(dirtied), mask);
1893
1894 ceph_mdsc_put_request(req);
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001895 if (mask & CEPH_SETATTR_SIZE)
1896 __ceph_do_pending_vmtruncate(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001897 return err;
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001898out_put:
Sage Weil355da1e2009-10-06 11:31:08 -07001899 ceph_mdsc_put_request(req);
1900 return err;
1901}
1902
1903/*
1904 * Verify that we have a lease on the given mask. If not,
1905 * do a getattr against an mds.
1906 */
Yan, Zheng508b32d2014-09-16 21:46:17 +08001907int ceph_do_getattr(struct inode *inode, int mask, bool force)
Sage Weil355da1e2009-10-06 11:31:08 -07001908{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001909 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1910 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -07001911 struct ceph_mds_request *req;
1912 int err;
1913
1914 if (ceph_snap(inode) == CEPH_SNAPDIR) {
1915 dout("do_getattr inode %p SNAPDIR\n", inode);
1916 return 0;
1917 }
1918
Sage Weilb7495fc2010-11-09 12:43:12 -08001919 dout("do_getattr inode %p mask %s mode 0%o\n", inode, ceph_cap_string(mask), inode->i_mode);
Yan, Zheng508b32d2014-09-16 21:46:17 +08001920 if (!force && ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
Sage Weil355da1e2009-10-06 11:31:08 -07001921 return 0;
1922
1923 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1924 if (IS_ERR(req))
1925 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -07001926 req->r_inode = inode;
1927 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001928 req->r_num_caps = 1;
1929 req->r_args.getattr.mask = cpu_to_le32(mask);
1930 err = ceph_mdsc_do_request(mdsc, NULL, req);
1931 ceph_mdsc_put_request(req);
1932 dout("do_getattr result=%d\n", err);
1933 return err;
1934}
1935
1936
1937/*
1938 * Check inode permissions. We verify we have a valid value for
1939 * the AUTH cap, then call the generic handler.
1940 */
Al Viro10556cb2011-06-20 19:28:19 -04001941int ceph_permission(struct inode *inode, int mask)
Sage Weil355da1e2009-10-06 11:31:08 -07001942{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001943 int err;
1944
Al Viro10556cb2011-06-20 19:28:19 -04001945 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +11001946 return -ECHILD;
1947
Yan, Zheng508b32d2014-09-16 21:46:17 +08001948 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
Sage Weil355da1e2009-10-06 11:31:08 -07001949
1950 if (!err)
Al Viro2830ba72011-06-20 19:16:29 -04001951 err = generic_permission(inode, mask);
Sage Weil355da1e2009-10-06 11:31:08 -07001952 return err;
1953}
1954
1955/*
1956 * Get all attributes. Hopefully somedata we'll have a statlite()
1957 * and can limit the fields we require to be accurate.
1958 */
1959int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1960 struct kstat *stat)
1961{
1962 struct inode *inode = dentry->d_inode;
Sage Weil232d4b02009-10-21 11:21:49 -07001963 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001964 int err;
1965
Yan, Zheng508b32d2014-09-16 21:46:17 +08001966 err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL, false);
Sage Weil355da1e2009-10-06 11:31:08 -07001967 if (!err) {
1968 generic_fillattr(inode, stat);
Yehuda Sadehad1fee92011-01-21 16:44:03 -08001969 stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
Sage Weil355da1e2009-10-06 11:31:08 -07001970 if (ceph_snap(inode) != CEPH_NOSNAP)
1971 stat->dev = ceph_snap(inode);
1972 else
1973 stat->dev = 0;
Sage Weil232d4b02009-10-21 11:21:49 -07001974 if (S_ISDIR(inode->i_mode)) {
Yehuda Sadeh1c1266b2011-01-12 16:53:27 -08001975 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
1976 RBYTES))
1977 stat->size = ci->i_rbytes;
1978 else
1979 stat->size = ci->i_files + ci->i_subdirs;
Sage Weil232d4b02009-10-21 11:21:49 -07001980 stat->blocks = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001981 stat->blksize = 65536;
Sage Weil232d4b02009-10-21 11:21:49 -07001982 }
Sage Weil355da1e2009-10-06 11:31:08 -07001983 }
1984 return err;
1985}