xref: /openbmc/linux/fs/ceph/inode.c (revision 99ccbd22)
1 #include <linux/ceph/ceph_debug.h>
2 
3 #include <linux/module.h>
4 #include <linux/fs.h>
5 #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>
12 
13 #include "super.h"
14 #include "mds_client.h"
15 #include "cache.h"
16 #include <linux/ceph/decode.h>
17 
18 /*
19  * Ceph inode operations
20  *
21  * Implement basic inode helpers (get, alloc) and inode ops (getattr,
22  * setattr, etc.), xattr helpers, and helpers for assimilating
23  * metadata returned by the MDS into our cache.
24  *
25  * Also define helpers for doing asynchronous writeback, invalidation,
26  * and truncation for the benefit of those who can't afford to block
27  * (typically because they are in the message handler path).
28  */
29 
30 static const struct inode_operations ceph_symlink_iops;
31 
32 static void ceph_invalidate_work(struct work_struct *work);
33 static void ceph_writeback_work(struct work_struct *work);
34 static void ceph_vmtruncate_work(struct work_struct *work);
35 
36 /*
37  * find or create an inode, given the ceph ino number
38  */
39 static int ceph_set_ino_cb(struct inode *inode, void *data)
40 {
41 	ceph_inode(inode)->i_vino = *(struct ceph_vino *)data;
42 	inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data);
43 	return 0;
44 }
45 
46 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
47 {
48 	struct inode *inode;
49 	ino_t t = ceph_vino_to_ino(vino);
50 
51 	inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
52 	if (inode == NULL)
53 		return ERR_PTR(-ENOMEM);
54 	if (inode->i_state & I_NEW) {
55 		dout("get_inode created new inode %p %llx.%llx ino %llx\n",
56 		     inode, ceph_vinop(inode), (u64)inode->i_ino);
57 		unlock_new_inode(inode);
58 	}
59 
60 	dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
61 	     vino.snap, inode);
62 	return inode;
63 }
64 
65 struct inode *ceph_lookup_inode(struct super_block *sb, struct ceph_vino vino)
66 {
67 	struct inode *inode;
68 	ino_t t = ceph_vino_to_ino(vino);
69 	inode = ilookup5_nowait(sb, t, ceph_ino_compare, &vino);
70 	return inode;
71 }
72 
73 /*
74  * get/constuct snapdir inode for a given directory
75  */
76 struct inode *ceph_get_snapdir(struct inode *parent)
77 {
78 	struct ceph_vino vino = {
79 		.ino = ceph_ino(parent),
80 		.snap = CEPH_SNAPDIR,
81 	};
82 	struct inode *inode = ceph_get_inode(parent->i_sb, vino);
83 	struct ceph_inode_info *ci = ceph_inode(inode);
84 
85 	BUG_ON(!S_ISDIR(parent->i_mode));
86 	if (IS_ERR(inode))
87 		return inode;
88 	inode->i_mode = parent->i_mode;
89 	inode->i_uid = parent->i_uid;
90 	inode->i_gid = parent->i_gid;
91 	inode->i_op = &ceph_dir_iops;
92 	inode->i_fop = &ceph_dir_fops;
93 	ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
94 	ci->i_rbytes = 0;
95 	return inode;
96 }
97 
98 const struct inode_operations ceph_file_iops = {
99 	.permission = ceph_permission,
100 	.setattr = ceph_setattr,
101 	.getattr = ceph_getattr,
102 	.setxattr = ceph_setxattr,
103 	.getxattr = ceph_getxattr,
104 	.listxattr = ceph_listxattr,
105 	.removexattr = ceph_removexattr,
106 };
107 
108 
109 /*
110  * We use a 'frag tree' to keep track of the MDS's directory fragments
111  * for a given inode (usually there is just a single fragment).  We
112  * need to know when a child frag is delegated to a new MDS, or when
113  * it is flagged as replicated, so we can direct our requests
114  * accordingly.
115  */
116 
117 /*
118  * find/create a frag in the tree
119  */
120 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
121 						    u32 f)
122 {
123 	struct rb_node **p;
124 	struct rb_node *parent = NULL;
125 	struct ceph_inode_frag *frag;
126 	int c;
127 
128 	p = &ci->i_fragtree.rb_node;
129 	while (*p) {
130 		parent = *p;
131 		frag = rb_entry(parent, struct ceph_inode_frag, node);
132 		c = ceph_frag_compare(f, frag->frag);
133 		if (c < 0)
134 			p = &(*p)->rb_left;
135 		else if (c > 0)
136 			p = &(*p)->rb_right;
137 		else
138 			return frag;
139 	}
140 
141 	frag = kmalloc(sizeof(*frag), GFP_NOFS);
142 	if (!frag) {
143 		pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
144 		       "frag %x\n", &ci->vfs_inode,
145 		       ceph_vinop(&ci->vfs_inode), f);
146 		return ERR_PTR(-ENOMEM);
147 	}
148 	frag->frag = f;
149 	frag->split_by = 0;
150 	frag->mds = -1;
151 	frag->ndist = 0;
152 
153 	rb_link_node(&frag->node, parent, p);
154 	rb_insert_color(&frag->node, &ci->i_fragtree);
155 
156 	dout("get_or_create_frag added %llx.%llx frag %x\n",
157 	     ceph_vinop(&ci->vfs_inode), f);
158 	return frag;
159 }
160 
161 /*
162  * find a specific frag @f
163  */
164 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
165 {
166 	struct rb_node *n = ci->i_fragtree.rb_node;
167 
168 	while (n) {
169 		struct ceph_inode_frag *frag =
170 			rb_entry(n, struct ceph_inode_frag, node);
171 		int c = ceph_frag_compare(f, frag->frag);
172 		if (c < 0)
173 			n = n->rb_left;
174 		else if (c > 0)
175 			n = n->rb_right;
176 		else
177 			return frag;
178 	}
179 	return NULL;
180 }
181 
182 /*
183  * Choose frag containing the given value @v.  If @pfrag is
184  * specified, copy the frag delegation info to the caller if
185  * it is present.
186  */
187 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
188 		     struct ceph_inode_frag *pfrag,
189 		     int *found)
190 {
191 	u32 t = ceph_frag_make(0, 0);
192 	struct ceph_inode_frag *frag;
193 	unsigned nway, i;
194 	u32 n;
195 
196 	if (found)
197 		*found = 0;
198 
199 	mutex_lock(&ci->i_fragtree_mutex);
200 	while (1) {
201 		WARN_ON(!ceph_frag_contains_value(t, v));
202 		frag = __ceph_find_frag(ci, t);
203 		if (!frag)
204 			break; /* t is a leaf */
205 		if (frag->split_by == 0) {
206 			if (pfrag)
207 				memcpy(pfrag, frag, sizeof(*pfrag));
208 			if (found)
209 				*found = 1;
210 			break;
211 		}
212 
213 		/* choose child */
214 		nway = 1 << frag->split_by;
215 		dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
216 		     frag->split_by, nway);
217 		for (i = 0; i < nway; i++) {
218 			n = ceph_frag_make_child(t, frag->split_by, i);
219 			if (ceph_frag_contains_value(n, v)) {
220 				t = n;
221 				break;
222 			}
223 		}
224 		BUG_ON(i == nway);
225 	}
226 	dout("choose_frag(%x) = %x\n", v, t);
227 
228 	mutex_unlock(&ci->i_fragtree_mutex);
229 	return t;
230 }
231 
232 /*
233  * Process dirfrag (delegation) info from the mds.  Include leaf
234  * fragment in tree ONLY if ndist > 0.  Otherwise, only
235  * branches/splits are included in i_fragtree)
236  */
237 static int ceph_fill_dirfrag(struct inode *inode,
238 			     struct ceph_mds_reply_dirfrag *dirinfo)
239 {
240 	struct ceph_inode_info *ci = ceph_inode(inode);
241 	struct ceph_inode_frag *frag;
242 	u32 id = le32_to_cpu(dirinfo->frag);
243 	int mds = le32_to_cpu(dirinfo->auth);
244 	int ndist = le32_to_cpu(dirinfo->ndist);
245 	int i;
246 	int err = 0;
247 
248 	mutex_lock(&ci->i_fragtree_mutex);
249 	if (ndist == 0) {
250 		/* no delegation info needed. */
251 		frag = __ceph_find_frag(ci, id);
252 		if (!frag)
253 			goto out;
254 		if (frag->split_by == 0) {
255 			/* tree leaf, remove */
256 			dout("fill_dirfrag removed %llx.%llx frag %x"
257 			     " (no ref)\n", ceph_vinop(inode), id);
258 			rb_erase(&frag->node, &ci->i_fragtree);
259 			kfree(frag);
260 		} else {
261 			/* tree branch, keep and clear */
262 			dout("fill_dirfrag cleared %llx.%llx frag %x"
263 			     " referral\n", ceph_vinop(inode), id);
264 			frag->mds = -1;
265 			frag->ndist = 0;
266 		}
267 		goto out;
268 	}
269 
270 
271 	/* find/add this frag to store mds delegation info */
272 	frag = __get_or_create_frag(ci, id);
273 	if (IS_ERR(frag)) {
274 		/* this is not the end of the world; we can continue
275 		   with bad/inaccurate delegation info */
276 		pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
277 		       ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
278 		err = -ENOMEM;
279 		goto out;
280 	}
281 
282 	frag->mds = mds;
283 	frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
284 	for (i = 0; i < frag->ndist; i++)
285 		frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
286 	dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
287 	     ceph_vinop(inode), frag->frag, frag->ndist);
288 
289 out:
290 	mutex_unlock(&ci->i_fragtree_mutex);
291 	return err;
292 }
293 
294 
295 /*
296  * initialize a newly allocated inode.
297  */
298 struct inode *ceph_alloc_inode(struct super_block *sb)
299 {
300 	struct ceph_inode_info *ci;
301 	int i;
302 
303 	ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
304 	if (!ci)
305 		return NULL;
306 
307 	dout("alloc_inode %p\n", &ci->vfs_inode);
308 
309 	spin_lock_init(&ci->i_ceph_lock);
310 
311 	ci->i_version = 0;
312 	ci->i_time_warp_seq = 0;
313 	ci->i_ceph_flags = 0;
314 	atomic_set(&ci->i_release_count, 1);
315 	atomic_set(&ci->i_complete_count, 0);
316 	ci->i_symlink = NULL;
317 
318 	memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
319 
320 	ci->i_fragtree = RB_ROOT;
321 	mutex_init(&ci->i_fragtree_mutex);
322 
323 	ci->i_xattrs.blob = NULL;
324 	ci->i_xattrs.prealloc_blob = NULL;
325 	ci->i_xattrs.dirty = false;
326 	ci->i_xattrs.index = RB_ROOT;
327 	ci->i_xattrs.count = 0;
328 	ci->i_xattrs.names_size = 0;
329 	ci->i_xattrs.vals_size = 0;
330 	ci->i_xattrs.version = 0;
331 	ci->i_xattrs.index_version = 0;
332 
333 	ci->i_caps = RB_ROOT;
334 	ci->i_auth_cap = NULL;
335 	ci->i_dirty_caps = 0;
336 	ci->i_flushing_caps = 0;
337 	INIT_LIST_HEAD(&ci->i_dirty_item);
338 	INIT_LIST_HEAD(&ci->i_flushing_item);
339 	ci->i_cap_flush_seq = 0;
340 	ci->i_cap_flush_last_tid = 0;
341 	memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
342 	init_waitqueue_head(&ci->i_cap_wq);
343 	ci->i_hold_caps_min = 0;
344 	ci->i_hold_caps_max = 0;
345 	INIT_LIST_HEAD(&ci->i_cap_delay_list);
346 	ci->i_cap_exporting_mds = 0;
347 	ci->i_cap_exporting_mseq = 0;
348 	ci->i_cap_exporting_issued = 0;
349 	INIT_LIST_HEAD(&ci->i_cap_snaps);
350 	ci->i_head_snapc = NULL;
351 	ci->i_snap_caps = 0;
352 
353 	for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
354 		ci->i_nr_by_mode[i] = 0;
355 
356 	mutex_init(&ci->i_truncate_mutex);
357 	ci->i_truncate_seq = 0;
358 	ci->i_truncate_size = 0;
359 	ci->i_truncate_pending = 0;
360 
361 	ci->i_max_size = 0;
362 	ci->i_reported_size = 0;
363 	ci->i_wanted_max_size = 0;
364 	ci->i_requested_max_size = 0;
365 
366 	ci->i_pin_ref = 0;
367 	ci->i_rd_ref = 0;
368 	ci->i_rdcache_ref = 0;
369 	ci->i_wr_ref = 0;
370 	ci->i_wb_ref = 0;
371 	ci->i_wrbuffer_ref = 0;
372 	ci->i_wrbuffer_ref_head = 0;
373 	ci->i_shared_gen = 0;
374 	ci->i_rdcache_gen = 0;
375 	ci->i_rdcache_revoking = 0;
376 
377 	INIT_LIST_HEAD(&ci->i_unsafe_writes);
378 	INIT_LIST_HEAD(&ci->i_unsafe_dirops);
379 	spin_lock_init(&ci->i_unsafe_lock);
380 
381 	ci->i_snap_realm = NULL;
382 	INIT_LIST_HEAD(&ci->i_snap_realm_item);
383 	INIT_LIST_HEAD(&ci->i_snap_flush_item);
384 
385 	INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
386 	INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
387 
388 	INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
389 
390 	ceph_fscache_inode_init(ci);
391 
392 	return &ci->vfs_inode;
393 }
394 
395 static void ceph_i_callback(struct rcu_head *head)
396 {
397 	struct inode *inode = container_of(head, struct inode, i_rcu);
398 	struct ceph_inode_info *ci = ceph_inode(inode);
399 
400 	kmem_cache_free(ceph_inode_cachep, ci);
401 }
402 
403 void ceph_destroy_inode(struct inode *inode)
404 {
405 	struct ceph_inode_info *ci = ceph_inode(inode);
406 	struct ceph_inode_frag *frag;
407 	struct rb_node *n;
408 
409 	dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
410 
411 	ceph_fscache_unregister_inode_cookie(ci);
412 
413 	ceph_queue_caps_release(inode);
414 
415 	/*
416 	 * we may still have a snap_realm reference if there are stray
417 	 * caps in i_cap_exporting_issued or i_snap_caps.
418 	 */
419 	if (ci->i_snap_realm) {
420 		struct ceph_mds_client *mdsc =
421 			ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
422 		struct ceph_snap_realm *realm = ci->i_snap_realm;
423 
424 		dout(" dropping residual ref to snap realm %p\n", realm);
425 		spin_lock(&realm->inodes_with_caps_lock);
426 		list_del_init(&ci->i_snap_realm_item);
427 		spin_unlock(&realm->inodes_with_caps_lock);
428 		ceph_put_snap_realm(mdsc, realm);
429 	}
430 
431 	kfree(ci->i_symlink);
432 	while ((n = rb_first(&ci->i_fragtree)) != NULL) {
433 		frag = rb_entry(n, struct ceph_inode_frag, node);
434 		rb_erase(n, &ci->i_fragtree);
435 		kfree(frag);
436 	}
437 
438 	__ceph_destroy_xattrs(ci);
439 	if (ci->i_xattrs.blob)
440 		ceph_buffer_put(ci->i_xattrs.blob);
441 	if (ci->i_xattrs.prealloc_blob)
442 		ceph_buffer_put(ci->i_xattrs.prealloc_blob);
443 
444 	call_rcu(&inode->i_rcu, ceph_i_callback);
445 }
446 
447 /*
448  * Helpers to fill in size, ctime, mtime, and atime.  We have to be
449  * careful because either the client or MDS may have more up to date
450  * info, depending on which capabilities are held, and whether
451  * time_warp_seq or truncate_seq have increased.  (Ordinarily, mtime
452  * and size are monotonically increasing, except when utimes() or
453  * truncate() increments the corresponding _seq values.)
454  */
455 int ceph_fill_file_size(struct inode *inode, int issued,
456 			u32 truncate_seq, u64 truncate_size, u64 size)
457 {
458 	struct ceph_inode_info *ci = ceph_inode(inode);
459 	int queue_trunc = 0;
460 
461 	if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
462 	    (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
463 		dout("size %lld -> %llu\n", inode->i_size, size);
464 		inode->i_size = size;
465 		inode->i_blocks = (size + (1<<9) - 1) >> 9;
466 		ci->i_reported_size = size;
467 		if (truncate_seq != ci->i_truncate_seq) {
468 			dout("truncate_seq %u -> %u\n",
469 			     ci->i_truncate_seq, truncate_seq);
470 			ci->i_truncate_seq = truncate_seq;
471 
472 			/* the MDS should have revoked these caps */
473 			WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
474 					       CEPH_CAP_FILE_RD |
475 					       CEPH_CAP_FILE_WR |
476 					       CEPH_CAP_FILE_LAZYIO));
477 			/*
478 			 * If we hold relevant caps, or in the case where we're
479 			 * not the only client referencing this file and we
480 			 * don't hold those caps, then we need to check whether
481 			 * the file is either opened or mmaped
482 			 */
483 			if ((issued & (CEPH_CAP_FILE_CACHE|
484 				       CEPH_CAP_FILE_BUFFER)) ||
485 			    mapping_mapped(inode->i_mapping) ||
486 			    __ceph_caps_file_wanted(ci)) {
487 				ci->i_truncate_pending++;
488 				queue_trunc = 1;
489 			}
490 		}
491 	}
492 	if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
493 	    ci->i_truncate_size != truncate_size) {
494 		dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
495 		     truncate_size);
496 		ci->i_truncate_size = truncate_size;
497 	}
498 
499 	if (queue_trunc)
500 		ceph_fscache_invalidate(inode);
501 
502 	return queue_trunc;
503 }
504 
505 void ceph_fill_file_time(struct inode *inode, int issued,
506 			 u64 time_warp_seq, struct timespec *ctime,
507 			 struct timespec *mtime, struct timespec *atime)
508 {
509 	struct ceph_inode_info *ci = ceph_inode(inode);
510 	int warn = 0;
511 
512 	if (issued & (CEPH_CAP_FILE_EXCL|
513 		      CEPH_CAP_FILE_WR|
514 		      CEPH_CAP_FILE_BUFFER|
515 		      CEPH_CAP_AUTH_EXCL|
516 		      CEPH_CAP_XATTR_EXCL)) {
517 		if (timespec_compare(ctime, &inode->i_ctime) > 0) {
518 			dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
519 			     inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
520 			     ctime->tv_sec, ctime->tv_nsec);
521 			inode->i_ctime = *ctime;
522 		}
523 		if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
524 			/* the MDS did a utimes() */
525 			dout("mtime %ld.%09ld -> %ld.%09ld "
526 			     "tw %d -> %d\n",
527 			     inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
528 			     mtime->tv_sec, mtime->tv_nsec,
529 			     ci->i_time_warp_seq, (int)time_warp_seq);
530 
531 			inode->i_mtime = *mtime;
532 			inode->i_atime = *atime;
533 			ci->i_time_warp_seq = time_warp_seq;
534 		} else if (time_warp_seq == ci->i_time_warp_seq) {
535 			/* nobody did utimes(); take the max */
536 			if (timespec_compare(mtime, &inode->i_mtime) > 0) {
537 				dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
538 				     inode->i_mtime.tv_sec,
539 				     inode->i_mtime.tv_nsec,
540 				     mtime->tv_sec, mtime->tv_nsec);
541 				inode->i_mtime = *mtime;
542 			}
543 			if (timespec_compare(atime, &inode->i_atime) > 0) {
544 				dout("atime %ld.%09ld -> %ld.%09ld inc\n",
545 				     inode->i_atime.tv_sec,
546 				     inode->i_atime.tv_nsec,
547 				     atime->tv_sec, atime->tv_nsec);
548 				inode->i_atime = *atime;
549 			}
550 		} else if (issued & CEPH_CAP_FILE_EXCL) {
551 			/* we did a utimes(); ignore mds values */
552 		} else {
553 			warn = 1;
554 		}
555 	} else {
556 		/* we have no write|excl caps; whatever the MDS says is true */
557 		if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
558 			inode->i_ctime = *ctime;
559 			inode->i_mtime = *mtime;
560 			inode->i_atime = *atime;
561 			ci->i_time_warp_seq = time_warp_seq;
562 		} else {
563 			warn = 1;
564 		}
565 	}
566 	if (warn) /* time_warp_seq shouldn't go backwards */
567 		dout("%p mds time_warp_seq %llu < %u\n",
568 		     inode, time_warp_seq, ci->i_time_warp_seq);
569 }
570 
571 /*
572  * Populate an inode based on info from mds.  May be called on new or
573  * existing inodes.
574  */
575 static int fill_inode(struct inode *inode,
576 		      struct ceph_mds_reply_info_in *iinfo,
577 		      struct ceph_mds_reply_dirfrag *dirinfo,
578 		      struct ceph_mds_session *session,
579 		      unsigned long ttl_from, int cap_fmode,
580 		      struct ceph_cap_reservation *caps_reservation)
581 {
582 	struct ceph_mds_reply_inode *info = iinfo->in;
583 	struct ceph_inode_info *ci = ceph_inode(inode);
584 	int i;
585 	int issued = 0, implemented;
586 	struct timespec mtime, atime, ctime;
587 	u32 nsplits;
588 	struct ceph_buffer *xattr_blob = NULL;
589 	int err = 0;
590 	int queue_trunc = 0;
591 
592 	dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
593 	     inode, ceph_vinop(inode), le64_to_cpu(info->version),
594 	     ci->i_version);
595 
596 	/*
597 	 * prealloc xattr data, if it looks like we'll need it.  only
598 	 * if len > 4 (meaning there are actually xattrs; the first 4
599 	 * bytes are the xattr count).
600 	 */
601 	if (iinfo->xattr_len > 4) {
602 		xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
603 		if (!xattr_blob)
604 			pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
605 			       iinfo->xattr_len);
606 	}
607 
608 	spin_lock(&ci->i_ceph_lock);
609 
610 	/*
611 	 * provided version will be odd if inode value is projected,
612 	 * even if stable.  skip the update if we have newer stable
613 	 * info (ours>=theirs, e.g. due to racing mds replies), unless
614 	 * we are getting projected (unstable) info (in which case the
615 	 * version is odd, and we want ours>theirs).
616 	 *   us   them
617 	 *   2    2     skip
618 	 *   3    2     skip
619 	 *   3    3     update
620 	 */
621 	if (le64_to_cpu(info->version) > 0 &&
622 	    (ci->i_version & ~1) >= le64_to_cpu(info->version))
623 		goto no_change;
624 
625 	issued = __ceph_caps_issued(ci, &implemented);
626 	issued |= implemented | __ceph_caps_dirty(ci);
627 
628 	/* update inode */
629 	ci->i_version = le64_to_cpu(info->version);
630 	inode->i_version++;
631 	inode->i_rdev = le32_to_cpu(info->rdev);
632 
633 	if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
634 		inode->i_mode = le32_to_cpu(info->mode);
635 		inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
636 		inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
637 		dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
638 		     from_kuid(&init_user_ns, inode->i_uid),
639 		     from_kgid(&init_user_ns, inode->i_gid));
640 	}
641 
642 	if ((issued & CEPH_CAP_LINK_EXCL) == 0)
643 		set_nlink(inode, le32_to_cpu(info->nlink));
644 
645 	/* be careful with mtime, atime, size */
646 	ceph_decode_timespec(&atime, &info->atime);
647 	ceph_decode_timespec(&mtime, &info->mtime);
648 	ceph_decode_timespec(&ctime, &info->ctime);
649 	queue_trunc = ceph_fill_file_size(inode, issued,
650 					  le32_to_cpu(info->truncate_seq),
651 					  le64_to_cpu(info->truncate_size),
652 					  le64_to_cpu(info->size));
653 	ceph_fill_file_time(inode, issued,
654 			    le32_to_cpu(info->time_warp_seq),
655 			    &ctime, &mtime, &atime);
656 
657 	/* only update max_size on auth cap */
658 	if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
659 	    ci->i_max_size != le64_to_cpu(info->max_size)) {
660 		dout("max_size %lld -> %llu\n", ci->i_max_size,
661 		     le64_to_cpu(info->max_size));
662 		ci->i_max_size = le64_to_cpu(info->max_size);
663 	}
664 
665 	ci->i_layout = info->layout;
666 	inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
667 
668 	/* xattrs */
669 	/* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
670 	if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
671 	    le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
672 		if (ci->i_xattrs.blob)
673 			ceph_buffer_put(ci->i_xattrs.blob);
674 		ci->i_xattrs.blob = xattr_blob;
675 		if (xattr_blob)
676 			memcpy(ci->i_xattrs.blob->vec.iov_base,
677 			       iinfo->xattr_data, iinfo->xattr_len);
678 		ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
679 		xattr_blob = NULL;
680 	}
681 
682 	inode->i_mapping->a_ops = &ceph_aops;
683 	inode->i_mapping->backing_dev_info =
684 		&ceph_sb_to_client(inode->i_sb)->backing_dev_info;
685 
686 	switch (inode->i_mode & S_IFMT) {
687 	case S_IFIFO:
688 	case S_IFBLK:
689 	case S_IFCHR:
690 	case S_IFSOCK:
691 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
692 		inode->i_op = &ceph_file_iops;
693 		break;
694 	case S_IFREG:
695 		inode->i_op = &ceph_file_iops;
696 		inode->i_fop = &ceph_file_fops;
697 		break;
698 	case S_IFLNK:
699 		inode->i_op = &ceph_symlink_iops;
700 		if (!ci->i_symlink) {
701 			u32 symlen = iinfo->symlink_len;
702 			char *sym;
703 
704 			spin_unlock(&ci->i_ceph_lock);
705 
706 			err = -EINVAL;
707 			if (WARN_ON(symlen != inode->i_size))
708 				goto out;
709 
710 			err = -ENOMEM;
711 			sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
712 			if (!sym)
713 				goto out;
714 
715 			spin_lock(&ci->i_ceph_lock);
716 			if (!ci->i_symlink)
717 				ci->i_symlink = sym;
718 			else
719 				kfree(sym); /* lost a race */
720 		}
721 		break;
722 	case S_IFDIR:
723 		inode->i_op = &ceph_dir_iops;
724 		inode->i_fop = &ceph_dir_fops;
725 
726 		ci->i_dir_layout = iinfo->dir_layout;
727 
728 		ci->i_files = le64_to_cpu(info->files);
729 		ci->i_subdirs = le64_to_cpu(info->subdirs);
730 		ci->i_rbytes = le64_to_cpu(info->rbytes);
731 		ci->i_rfiles = le64_to_cpu(info->rfiles);
732 		ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
733 		ceph_decode_timespec(&ci->i_rctime, &info->rctime);
734 		break;
735 	default:
736 		pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
737 		       ceph_vinop(inode), inode->i_mode);
738 	}
739 
740 	/* set dir completion flag? */
741 	if (S_ISDIR(inode->i_mode) &&
742 	    ci->i_files == 0 && ci->i_subdirs == 0 &&
743 	    ceph_snap(inode) == CEPH_NOSNAP &&
744 	    (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
745 	    (issued & CEPH_CAP_FILE_EXCL) == 0 &&
746 	    !__ceph_dir_is_complete(ci)) {
747 		dout(" marking %p complete (empty)\n", inode);
748 		__ceph_dir_set_complete(ci, atomic_read(&ci->i_release_count));
749 		ci->i_max_offset = 2;
750 	}
751 no_change:
752 	spin_unlock(&ci->i_ceph_lock);
753 
754 	/* queue truncate if we saw i_size decrease */
755 	if (queue_trunc)
756 		ceph_queue_vmtruncate(inode);
757 
758 	/* populate frag tree */
759 	/* FIXME: move me up, if/when version reflects fragtree changes */
760 	nsplits = le32_to_cpu(info->fragtree.nsplits);
761 	mutex_lock(&ci->i_fragtree_mutex);
762 	for (i = 0; i < nsplits; i++) {
763 		u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
764 		struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
765 
766 		if (IS_ERR(frag))
767 			continue;
768 		frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
769 		dout(" frag %x split by %d\n", frag->frag, frag->split_by);
770 	}
771 	mutex_unlock(&ci->i_fragtree_mutex);
772 
773 	/* were we issued a capability? */
774 	if (info->cap.caps) {
775 		if (ceph_snap(inode) == CEPH_NOSNAP) {
776 			ceph_add_cap(inode, session,
777 				     le64_to_cpu(info->cap.cap_id),
778 				     cap_fmode,
779 				     le32_to_cpu(info->cap.caps),
780 				     le32_to_cpu(info->cap.wanted),
781 				     le32_to_cpu(info->cap.seq),
782 				     le32_to_cpu(info->cap.mseq),
783 				     le64_to_cpu(info->cap.realm),
784 				     info->cap.flags,
785 				     caps_reservation);
786 		} else {
787 			spin_lock(&ci->i_ceph_lock);
788 			dout(" %p got snap_caps %s\n", inode,
789 			     ceph_cap_string(le32_to_cpu(info->cap.caps)));
790 			ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
791 			if (cap_fmode >= 0)
792 				__ceph_get_fmode(ci, cap_fmode);
793 			spin_unlock(&ci->i_ceph_lock);
794 		}
795 	} else if (cap_fmode >= 0) {
796 		pr_warning("mds issued no caps on %llx.%llx\n",
797 			   ceph_vinop(inode));
798 		__ceph_get_fmode(ci, cap_fmode);
799 	}
800 
801 	/* update delegation info? */
802 	if (dirinfo)
803 		ceph_fill_dirfrag(inode, dirinfo);
804 
805 	err = 0;
806 
807 out:
808 	if (xattr_blob)
809 		ceph_buffer_put(xattr_blob);
810 	return err;
811 }
812 
813 /*
814  * caller should hold session s_mutex.
815  */
816 static void update_dentry_lease(struct dentry *dentry,
817 				struct ceph_mds_reply_lease *lease,
818 				struct ceph_mds_session *session,
819 				unsigned long from_time)
820 {
821 	struct ceph_dentry_info *di = ceph_dentry(dentry);
822 	long unsigned duration = le32_to_cpu(lease->duration_ms);
823 	long unsigned ttl = from_time + (duration * HZ) / 1000;
824 	long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
825 	struct inode *dir;
826 
827 	/* only track leases on regular dentries */
828 	if (dentry->d_op != &ceph_dentry_ops)
829 		return;
830 
831 	spin_lock(&dentry->d_lock);
832 	dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
833 	     dentry, duration, ttl);
834 
835 	/* make lease_rdcache_gen match directory */
836 	dir = dentry->d_parent->d_inode;
837 	di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
838 
839 	if (duration == 0)
840 		goto out_unlock;
841 
842 	if (di->lease_gen == session->s_cap_gen &&
843 	    time_before(ttl, dentry->d_time))
844 		goto out_unlock;  /* we already have a newer lease. */
845 
846 	if (di->lease_session && di->lease_session != session)
847 		goto out_unlock;
848 
849 	ceph_dentry_lru_touch(dentry);
850 
851 	if (!di->lease_session)
852 		di->lease_session = ceph_get_mds_session(session);
853 	di->lease_gen = session->s_cap_gen;
854 	di->lease_seq = le32_to_cpu(lease->seq);
855 	di->lease_renew_after = half_ttl;
856 	di->lease_renew_from = 0;
857 	dentry->d_time = ttl;
858 out_unlock:
859 	spin_unlock(&dentry->d_lock);
860 	return;
861 }
862 
863 /*
864  * Set dentry's directory position based on the current dir's max, and
865  * order it in d_subdirs, so that dcache_readdir behaves.
866  *
867  * Always called under directory's i_mutex.
868  */
869 static void ceph_set_dentry_offset(struct dentry *dn)
870 {
871 	struct dentry *dir = dn->d_parent;
872 	struct inode *inode = dir->d_inode;
873 	struct ceph_inode_info *ci;
874 	struct ceph_dentry_info *di;
875 
876 	BUG_ON(!inode);
877 
878 	ci = ceph_inode(inode);
879 	di = ceph_dentry(dn);
880 
881 	spin_lock(&ci->i_ceph_lock);
882 	if (!__ceph_dir_is_complete(ci)) {
883 		spin_unlock(&ci->i_ceph_lock);
884 		return;
885 	}
886 	di->offset = ceph_inode(inode)->i_max_offset++;
887 	spin_unlock(&ci->i_ceph_lock);
888 
889 	spin_lock(&dir->d_lock);
890 	spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
891 	list_move(&dn->d_u.d_child, &dir->d_subdirs);
892 	dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
893 	     dn->d_u.d_child.prev, dn->d_u.d_child.next);
894 	spin_unlock(&dn->d_lock);
895 	spin_unlock(&dir->d_lock);
896 }
897 
898 /*
899  * splice a dentry to an inode.
900  * caller must hold directory i_mutex for this to be safe.
901  *
902  * we will only rehash the resulting dentry if @prehash is
903  * true; @prehash will be set to false (for the benefit of
904  * the caller) if we fail.
905  */
906 static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
907 				    bool *prehash, bool set_offset)
908 {
909 	struct dentry *realdn;
910 
911 	BUG_ON(dn->d_inode);
912 
913 	/* dn must be unhashed */
914 	if (!d_unhashed(dn))
915 		d_drop(dn);
916 	realdn = d_materialise_unique(dn, in);
917 	if (IS_ERR(realdn)) {
918 		pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
919 		       PTR_ERR(realdn), dn, in, ceph_vinop(in));
920 		if (prehash)
921 			*prehash = false; /* don't rehash on error */
922 		dn = realdn; /* note realdn contains the error */
923 		goto out;
924 	} else if (realdn) {
925 		dout("dn %p (%d) spliced with %p (%d) "
926 		     "inode %p ino %llx.%llx\n",
927 		     dn, d_count(dn),
928 		     realdn, d_count(realdn),
929 		     realdn->d_inode, ceph_vinop(realdn->d_inode));
930 		dput(dn);
931 		dn = realdn;
932 	} else {
933 		BUG_ON(!ceph_dentry(dn));
934 		dout("dn %p attached to %p ino %llx.%llx\n",
935 		     dn, dn->d_inode, ceph_vinop(dn->d_inode));
936 	}
937 	if ((!prehash || *prehash) && d_unhashed(dn))
938 		d_rehash(dn);
939 	if (set_offset)
940 		ceph_set_dentry_offset(dn);
941 out:
942 	return dn;
943 }
944 
945 /*
946  * Incorporate results into the local cache.  This is either just
947  * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
948  * after a lookup).
949  *
950  * A reply may contain
951  *         a directory inode along with a dentry.
952  *  and/or a target inode
953  *
954  * Called with snap_rwsem (read).
955  */
956 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
957 		    struct ceph_mds_session *session)
958 {
959 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
960 	struct inode *in = NULL;
961 	struct ceph_mds_reply_inode *ininfo;
962 	struct ceph_vino vino;
963 	struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
964 	int i = 0;
965 	int err = 0;
966 
967 	dout("fill_trace %p is_dentry %d is_target %d\n", req,
968 	     rinfo->head->is_dentry, rinfo->head->is_target);
969 
970 #if 0
971 	/*
972 	 * Debugging hook:
973 	 *
974 	 * If we resend completed ops to a recovering mds, we get no
975 	 * trace.  Since that is very rare, pretend this is the case
976 	 * to ensure the 'no trace' handlers in the callers behave.
977 	 *
978 	 * Fill in inodes unconditionally to avoid breaking cap
979 	 * invariants.
980 	 */
981 	if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
982 		pr_info("fill_trace faking empty trace on %lld %s\n",
983 			req->r_tid, ceph_mds_op_name(rinfo->head->op));
984 		if (rinfo->head->is_dentry) {
985 			rinfo->head->is_dentry = 0;
986 			err = fill_inode(req->r_locked_dir,
987 					 &rinfo->diri, rinfo->dirfrag,
988 					 session, req->r_request_started, -1);
989 		}
990 		if (rinfo->head->is_target) {
991 			rinfo->head->is_target = 0;
992 			ininfo = rinfo->targeti.in;
993 			vino.ino = le64_to_cpu(ininfo->ino);
994 			vino.snap = le64_to_cpu(ininfo->snapid);
995 			in = ceph_get_inode(sb, vino);
996 			err = fill_inode(in, &rinfo->targeti, NULL,
997 					 session, req->r_request_started,
998 					 req->r_fmode);
999 			iput(in);
1000 		}
1001 	}
1002 #endif
1003 
1004 	if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1005 		dout("fill_trace reply is empty!\n");
1006 		if (rinfo->head->result == 0 && req->r_locked_dir)
1007 			ceph_invalidate_dir_request(req);
1008 		return 0;
1009 	}
1010 
1011 	if (rinfo->head->is_dentry) {
1012 		struct inode *dir = req->r_locked_dir;
1013 
1014 		if (dir) {
1015 			err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
1016 					 session, req->r_request_started, -1,
1017 					 &req->r_caps_reservation);
1018 			if (err < 0)
1019 				return err;
1020 		} else {
1021 			WARN_ON_ONCE(1);
1022 		}
1023 	}
1024 
1025 	/*
1026 	 * ignore null lease/binding on snapdir ENOENT, or else we
1027 	 * will have trouble splicing in the virtual snapdir later
1028 	 */
1029 	if (rinfo->head->is_dentry && !req->r_aborted &&
1030 	    req->r_locked_dir &&
1031 	    (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1032 					       fsc->mount_options->snapdir_name,
1033 					       req->r_dentry->d_name.len))) {
1034 		/*
1035 		 * lookup link rename   : null -> possibly existing inode
1036 		 * mknod symlink mkdir  : null -> new inode
1037 		 * unlink               : linked -> null
1038 		 */
1039 		struct inode *dir = req->r_locked_dir;
1040 		struct dentry *dn = req->r_dentry;
1041 		bool have_dir_cap, have_lease;
1042 
1043 		BUG_ON(!dn);
1044 		BUG_ON(!dir);
1045 		BUG_ON(dn->d_parent->d_inode != dir);
1046 		BUG_ON(ceph_ino(dir) !=
1047 		       le64_to_cpu(rinfo->diri.in->ino));
1048 		BUG_ON(ceph_snap(dir) !=
1049 		       le64_to_cpu(rinfo->diri.in->snapid));
1050 
1051 		/* do we have a lease on the whole dir? */
1052 		have_dir_cap =
1053 			(le32_to_cpu(rinfo->diri.in->cap.caps) &
1054 			 CEPH_CAP_FILE_SHARED);
1055 
1056 		/* do we have a dn lease? */
1057 		have_lease = have_dir_cap ||
1058 			le32_to_cpu(rinfo->dlease->duration_ms);
1059 		if (!have_lease)
1060 			dout("fill_trace  no dentry lease or dir cap\n");
1061 
1062 		/* rename? */
1063 		if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1064 			dout(" src %p '%.*s' dst %p '%.*s'\n",
1065 			     req->r_old_dentry,
1066 			     req->r_old_dentry->d_name.len,
1067 			     req->r_old_dentry->d_name.name,
1068 			     dn, dn->d_name.len, dn->d_name.name);
1069 			dout("fill_trace doing d_move %p -> %p\n",
1070 			     req->r_old_dentry, dn);
1071 
1072 			d_move(req->r_old_dentry, dn);
1073 			dout(" src %p '%.*s' dst %p '%.*s'\n",
1074 			     req->r_old_dentry,
1075 			     req->r_old_dentry->d_name.len,
1076 			     req->r_old_dentry->d_name.name,
1077 			     dn, dn->d_name.len, dn->d_name.name);
1078 
1079 			/* ensure target dentry is invalidated, despite
1080 			   rehashing bug in vfs_rename_dir */
1081 			ceph_invalidate_dentry_lease(dn);
1082 
1083 			/*
1084 			 * d_move() puts the renamed dentry at the end of
1085 			 * d_subdirs.  We need to assign it an appropriate
1086 			 * directory offset so we can behave when dir is
1087 			 * complete.
1088 			 */
1089 			ceph_set_dentry_offset(req->r_old_dentry);
1090 			dout("dn %p gets new offset %lld\n", req->r_old_dentry,
1091 			     ceph_dentry(req->r_old_dentry)->offset);
1092 
1093 			dn = req->r_old_dentry;  /* use old_dentry */
1094 			in = dn->d_inode;
1095 		}
1096 
1097 		/* null dentry? */
1098 		if (!rinfo->head->is_target) {
1099 			dout("fill_trace null dentry\n");
1100 			if (dn->d_inode) {
1101 				dout("d_delete %p\n", dn);
1102 				d_delete(dn);
1103 			} else {
1104 				dout("d_instantiate %p NULL\n", dn);
1105 				d_instantiate(dn, NULL);
1106 				if (have_lease && d_unhashed(dn))
1107 					d_rehash(dn);
1108 				update_dentry_lease(dn, rinfo->dlease,
1109 						    session,
1110 						    req->r_request_started);
1111 			}
1112 			goto done;
1113 		}
1114 
1115 		/* attach proper inode */
1116 		ininfo = rinfo->targeti.in;
1117 		vino.ino = le64_to_cpu(ininfo->ino);
1118 		vino.snap = le64_to_cpu(ininfo->snapid);
1119 		in = dn->d_inode;
1120 		if (!in) {
1121 			in = ceph_get_inode(sb, vino);
1122 			if (IS_ERR(in)) {
1123 				pr_err("fill_trace bad get_inode "
1124 				       "%llx.%llx\n", vino.ino, vino.snap);
1125 				err = PTR_ERR(in);
1126 				d_drop(dn);
1127 				goto done;
1128 			}
1129 			dn = splice_dentry(dn, in, &have_lease, true);
1130 			if (IS_ERR(dn)) {
1131 				err = PTR_ERR(dn);
1132 				goto done;
1133 			}
1134 			req->r_dentry = dn;  /* may have spliced */
1135 			ihold(in);
1136 		} else if (ceph_ino(in) == vino.ino &&
1137 			   ceph_snap(in) == vino.snap) {
1138 			ihold(in);
1139 		} else {
1140 			dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1141 			     dn, in, ceph_ino(in), ceph_snap(in),
1142 			     vino.ino, vino.snap);
1143 			have_lease = false;
1144 			in = NULL;
1145 		}
1146 
1147 		if (have_lease)
1148 			update_dentry_lease(dn, rinfo->dlease, session,
1149 					    req->r_request_started);
1150 		dout(" final dn %p\n", dn);
1151 		i++;
1152 	} else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1153 		   req->r_op == CEPH_MDS_OP_MKSNAP) && !req->r_aborted) {
1154 		struct dentry *dn = req->r_dentry;
1155 
1156 		/* fill out a snapdir LOOKUPSNAP dentry */
1157 		BUG_ON(!dn);
1158 		BUG_ON(!req->r_locked_dir);
1159 		BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1160 		ininfo = rinfo->targeti.in;
1161 		vino.ino = le64_to_cpu(ininfo->ino);
1162 		vino.snap = le64_to_cpu(ininfo->snapid);
1163 		in = ceph_get_inode(sb, vino);
1164 		if (IS_ERR(in)) {
1165 			pr_err("fill_inode get_inode badness %llx.%llx\n",
1166 			       vino.ino, vino.snap);
1167 			err = PTR_ERR(in);
1168 			d_delete(dn);
1169 			goto done;
1170 		}
1171 		dout(" linking snapped dir %p to dn %p\n", in, dn);
1172 		dn = splice_dentry(dn, in, NULL, true);
1173 		if (IS_ERR(dn)) {
1174 			err = PTR_ERR(dn);
1175 			goto done;
1176 		}
1177 		req->r_dentry = dn;  /* may have spliced */
1178 		ihold(in);
1179 		rinfo->head->is_dentry = 1;  /* fool notrace handlers */
1180 	}
1181 
1182 	if (rinfo->head->is_target) {
1183 		vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1184 		vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1185 
1186 		if (in == NULL || ceph_ino(in) != vino.ino ||
1187 		    ceph_snap(in) != vino.snap) {
1188 			in = ceph_get_inode(sb, vino);
1189 			if (IS_ERR(in)) {
1190 				err = PTR_ERR(in);
1191 				goto done;
1192 			}
1193 		}
1194 		req->r_target_inode = in;
1195 
1196 		err = fill_inode(in,
1197 				 &rinfo->targeti, NULL,
1198 				 session, req->r_request_started,
1199 				 (le32_to_cpu(rinfo->head->result) == 0) ?
1200 				 req->r_fmode : -1,
1201 				 &req->r_caps_reservation);
1202 		if (err < 0) {
1203 			pr_err("fill_inode badness %p %llx.%llx\n",
1204 			       in, ceph_vinop(in));
1205 			goto done;
1206 		}
1207 	}
1208 
1209 done:
1210 	dout("fill_trace done err=%d\n", err);
1211 	return err;
1212 }
1213 
1214 /*
1215  * Prepopulate our cache with readdir results, leases, etc.
1216  */
1217 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1218 					   struct ceph_mds_session *session)
1219 {
1220 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1221 	int i, err = 0;
1222 
1223 	for (i = 0; i < rinfo->dir_nr; i++) {
1224 		struct ceph_vino vino;
1225 		struct inode *in;
1226 		int rc;
1227 
1228 		vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1229 		vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1230 
1231 		in = ceph_get_inode(req->r_dentry->d_sb, vino);
1232 		if (IS_ERR(in)) {
1233 			err = PTR_ERR(in);
1234 			dout("new_inode badness got %d\n", err);
1235 			continue;
1236 		}
1237 		rc = fill_inode(in, &rinfo->dir_in[i], NULL, session,
1238 				req->r_request_started, -1,
1239 				&req->r_caps_reservation);
1240 		if (rc < 0) {
1241 			pr_err("fill_inode badness on %p got %d\n", in, rc);
1242 			err = rc;
1243 			continue;
1244 		}
1245 	}
1246 
1247 	return err;
1248 }
1249 
1250 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1251 			     struct ceph_mds_session *session)
1252 {
1253 	struct dentry *parent = req->r_dentry;
1254 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1255 	struct qstr dname;
1256 	struct dentry *dn;
1257 	struct inode *in;
1258 	int err = 0, i;
1259 	struct inode *snapdir = NULL;
1260 	struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1261 	u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1262 	struct ceph_dentry_info *di;
1263 
1264 	if (req->r_aborted)
1265 		return readdir_prepopulate_inodes_only(req, session);
1266 
1267 	if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1268 		snapdir = ceph_get_snapdir(parent->d_inode);
1269 		parent = d_find_alias(snapdir);
1270 		dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1271 		     rinfo->dir_nr, parent);
1272 	} else {
1273 		dout("readdir_prepopulate %d items under dn %p\n",
1274 		     rinfo->dir_nr, parent);
1275 		if (rinfo->dir_dir)
1276 			ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1277 	}
1278 
1279 	for (i = 0; i < rinfo->dir_nr; i++) {
1280 		struct ceph_vino vino;
1281 
1282 		dname.name = rinfo->dir_dname[i];
1283 		dname.len = rinfo->dir_dname_len[i];
1284 		dname.hash = full_name_hash(dname.name, dname.len);
1285 
1286 		vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1287 		vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1288 
1289 retry_lookup:
1290 		dn = d_lookup(parent, &dname);
1291 		dout("d_lookup on parent=%p name=%.*s got %p\n",
1292 		     parent, dname.len, dname.name, dn);
1293 
1294 		if (!dn) {
1295 			dn = d_alloc(parent, &dname);
1296 			dout("d_alloc %p '%.*s' = %p\n", parent,
1297 			     dname.len, dname.name, dn);
1298 			if (dn == NULL) {
1299 				dout("d_alloc badness\n");
1300 				err = -ENOMEM;
1301 				goto out;
1302 			}
1303 			err = ceph_init_dentry(dn);
1304 			if (err < 0) {
1305 				dput(dn);
1306 				goto out;
1307 			}
1308 		} else if (dn->d_inode &&
1309 			   (ceph_ino(dn->d_inode) != vino.ino ||
1310 			    ceph_snap(dn->d_inode) != vino.snap)) {
1311 			dout(" dn %p points to wrong inode %p\n",
1312 			     dn, dn->d_inode);
1313 			d_delete(dn);
1314 			dput(dn);
1315 			goto retry_lookup;
1316 		} else {
1317 			/* reorder parent's d_subdirs */
1318 			spin_lock(&parent->d_lock);
1319 			spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
1320 			list_move(&dn->d_u.d_child, &parent->d_subdirs);
1321 			spin_unlock(&dn->d_lock);
1322 			spin_unlock(&parent->d_lock);
1323 		}
1324 
1325 		di = dn->d_fsdata;
1326 		di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1327 
1328 		/* inode */
1329 		if (dn->d_inode) {
1330 			in = dn->d_inode;
1331 		} else {
1332 			in = ceph_get_inode(parent->d_sb, vino);
1333 			if (IS_ERR(in)) {
1334 				dout("new_inode badness\n");
1335 				d_drop(dn);
1336 				dput(dn);
1337 				err = PTR_ERR(in);
1338 				goto out;
1339 			}
1340 			dn = splice_dentry(dn, in, NULL, false);
1341 			if (IS_ERR(dn))
1342 				dn = NULL;
1343 		}
1344 
1345 		if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1346 			       req->r_request_started, -1,
1347 			       &req->r_caps_reservation) < 0) {
1348 			pr_err("fill_inode badness on %p\n", in);
1349 			goto next_item;
1350 		}
1351 		if (dn)
1352 			update_dentry_lease(dn, rinfo->dir_dlease[i],
1353 					    req->r_session,
1354 					    req->r_request_started);
1355 next_item:
1356 		if (dn)
1357 			dput(dn);
1358 	}
1359 	req->r_did_prepopulate = true;
1360 
1361 out:
1362 	if (snapdir) {
1363 		iput(snapdir);
1364 		dput(parent);
1365 	}
1366 	dout("readdir_prepopulate done\n");
1367 	return err;
1368 }
1369 
1370 int ceph_inode_set_size(struct inode *inode, loff_t size)
1371 {
1372 	struct ceph_inode_info *ci = ceph_inode(inode);
1373 	int ret = 0;
1374 
1375 	spin_lock(&ci->i_ceph_lock);
1376 	dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1377 	inode->i_size = size;
1378 	inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1379 
1380 	/* tell the MDS if we are approaching max_size */
1381 	if ((size << 1) >= ci->i_max_size &&
1382 	    (ci->i_reported_size << 1) < ci->i_max_size)
1383 		ret = 1;
1384 
1385 	spin_unlock(&ci->i_ceph_lock);
1386 	return ret;
1387 }
1388 
1389 /*
1390  * Write back inode data in a worker thread.  (This can't be done
1391  * in the message handler context.)
1392  */
1393 void ceph_queue_writeback(struct inode *inode)
1394 {
1395 	ihold(inode);
1396 	if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1397 		       &ceph_inode(inode)->i_wb_work)) {
1398 		dout("ceph_queue_writeback %p\n", inode);
1399 	} else {
1400 		dout("ceph_queue_writeback %p failed\n", inode);
1401 		iput(inode);
1402 	}
1403 }
1404 
1405 static void ceph_writeback_work(struct work_struct *work)
1406 {
1407 	struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1408 						  i_wb_work);
1409 	struct inode *inode = &ci->vfs_inode;
1410 
1411 	dout("writeback %p\n", inode);
1412 	filemap_fdatawrite(&inode->i_data);
1413 	iput(inode);
1414 }
1415 
1416 /*
1417  * queue an async invalidation
1418  */
1419 void ceph_queue_invalidate(struct inode *inode)
1420 {
1421 	ihold(inode);
1422 	if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1423 		       &ceph_inode(inode)->i_pg_inv_work)) {
1424 		dout("ceph_queue_invalidate %p\n", inode);
1425 	} else {
1426 		dout("ceph_queue_invalidate %p failed\n", inode);
1427 		iput(inode);
1428 	}
1429 }
1430 
1431 /*
1432  * Invalidate inode pages in a worker thread.  (This can't be done
1433  * in the message handler context.)
1434  */
1435 static void ceph_invalidate_work(struct work_struct *work)
1436 {
1437 	struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1438 						  i_pg_inv_work);
1439 	struct inode *inode = &ci->vfs_inode;
1440 	u32 orig_gen;
1441 	int check = 0;
1442 
1443 	mutex_lock(&ci->i_truncate_mutex);
1444 	spin_lock(&ci->i_ceph_lock);
1445 	dout("invalidate_pages %p gen %d revoking %d\n", inode,
1446 	     ci->i_rdcache_gen, ci->i_rdcache_revoking);
1447 	if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1448 		/* nevermind! */
1449 		spin_unlock(&ci->i_ceph_lock);
1450 		mutex_unlock(&ci->i_truncate_mutex);
1451 		goto out;
1452 	}
1453 	orig_gen = ci->i_rdcache_gen;
1454 	spin_unlock(&ci->i_ceph_lock);
1455 
1456 	truncate_inode_pages(inode->i_mapping, 0);
1457 
1458 	spin_lock(&ci->i_ceph_lock);
1459 	if (orig_gen == ci->i_rdcache_gen &&
1460 	    orig_gen == ci->i_rdcache_revoking) {
1461 		dout("invalidate_pages %p gen %d successful\n", inode,
1462 		     ci->i_rdcache_gen);
1463 		ci->i_rdcache_revoking--;
1464 		check = 1;
1465 	} else {
1466 		dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1467 		     inode, orig_gen, ci->i_rdcache_gen,
1468 		     ci->i_rdcache_revoking);
1469 	}
1470 	spin_unlock(&ci->i_ceph_lock);
1471 	mutex_unlock(&ci->i_truncate_mutex);
1472 
1473 	if (check)
1474 		ceph_check_caps(ci, 0, NULL);
1475 out:
1476 	iput(inode);
1477 }
1478 
1479 
1480 /*
1481  * called by trunc_wq;
1482  *
1483  * We also truncate in a separate thread as well.
1484  */
1485 static void ceph_vmtruncate_work(struct work_struct *work)
1486 {
1487 	struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1488 						  i_vmtruncate_work);
1489 	struct inode *inode = &ci->vfs_inode;
1490 
1491 	dout("vmtruncate_work %p\n", inode);
1492 	__ceph_do_pending_vmtruncate(inode);
1493 	iput(inode);
1494 }
1495 
1496 /*
1497  * Queue an async vmtruncate.  If we fail to queue work, we will handle
1498  * the truncation the next time we call __ceph_do_pending_vmtruncate.
1499  */
1500 void ceph_queue_vmtruncate(struct inode *inode)
1501 {
1502 	struct ceph_inode_info *ci = ceph_inode(inode);
1503 
1504 	ihold(inode);
1505 
1506 	if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
1507 		       &ci->i_vmtruncate_work)) {
1508 		dout("ceph_queue_vmtruncate %p\n", inode);
1509 	} else {
1510 		dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1511 		     inode, ci->i_truncate_pending);
1512 		iput(inode);
1513 	}
1514 }
1515 
1516 /*
1517  * Make sure any pending truncation is applied before doing anything
1518  * that may depend on it.
1519  */
1520 void __ceph_do_pending_vmtruncate(struct inode *inode)
1521 {
1522 	struct ceph_inode_info *ci = ceph_inode(inode);
1523 	u64 to;
1524 	int wrbuffer_refs, finish = 0;
1525 
1526 	mutex_lock(&ci->i_truncate_mutex);
1527 retry:
1528 	spin_lock(&ci->i_ceph_lock);
1529 	if (ci->i_truncate_pending == 0) {
1530 		dout("__do_pending_vmtruncate %p none pending\n", inode);
1531 		spin_unlock(&ci->i_ceph_lock);
1532 		mutex_unlock(&ci->i_truncate_mutex);
1533 		return;
1534 	}
1535 
1536 	/*
1537 	 * make sure any dirty snapped pages are flushed before we
1538 	 * possibly truncate them.. so write AND block!
1539 	 */
1540 	if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1541 		dout("__do_pending_vmtruncate %p flushing snaps first\n",
1542 		     inode);
1543 		spin_unlock(&ci->i_ceph_lock);
1544 		filemap_write_and_wait_range(&inode->i_data, 0,
1545 					     inode->i_sb->s_maxbytes);
1546 		goto retry;
1547 	}
1548 
1549 	/* there should be no reader or writer */
1550 	WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
1551 
1552 	to = ci->i_truncate_size;
1553 	wrbuffer_refs = ci->i_wrbuffer_ref;
1554 	dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1555 	     ci->i_truncate_pending, to);
1556 	spin_unlock(&ci->i_ceph_lock);
1557 
1558 	truncate_inode_pages(inode->i_mapping, to);
1559 
1560 	spin_lock(&ci->i_ceph_lock);
1561 	if (to == ci->i_truncate_size) {
1562 		ci->i_truncate_pending = 0;
1563 		finish = 1;
1564 	}
1565 	spin_unlock(&ci->i_ceph_lock);
1566 	if (!finish)
1567 		goto retry;
1568 
1569 	mutex_unlock(&ci->i_truncate_mutex);
1570 
1571 	if (wrbuffer_refs == 0)
1572 		ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1573 
1574 	wake_up_all(&ci->i_cap_wq);
1575 }
1576 
1577 /*
1578  * symlinks
1579  */
1580 static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1581 {
1582 	struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1583 	nd_set_link(nd, ci->i_symlink);
1584 	return NULL;
1585 }
1586 
1587 static const struct inode_operations ceph_symlink_iops = {
1588 	.readlink = generic_readlink,
1589 	.follow_link = ceph_sym_follow_link,
1590 	.setattr = ceph_setattr,
1591 	.getattr = ceph_getattr,
1592 	.setxattr = ceph_setxattr,
1593 	.getxattr = ceph_getxattr,
1594 	.listxattr = ceph_listxattr,
1595 	.removexattr = ceph_removexattr,
1596 };
1597 
1598 /*
1599  * setattr
1600  */
1601 int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1602 {
1603 	struct inode *inode = dentry->d_inode;
1604 	struct ceph_inode_info *ci = ceph_inode(inode);
1605 	struct inode *parent_inode;
1606 	const unsigned int ia_valid = attr->ia_valid;
1607 	struct ceph_mds_request *req;
1608 	struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
1609 	int issued;
1610 	int release = 0, dirtied = 0;
1611 	int mask = 0;
1612 	int err = 0;
1613 	int inode_dirty_flags = 0;
1614 
1615 	if (ceph_snap(inode) != CEPH_NOSNAP)
1616 		return -EROFS;
1617 
1618 	err = inode_change_ok(inode, attr);
1619 	if (err != 0)
1620 		return err;
1621 
1622 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1623 				       USE_AUTH_MDS);
1624 	if (IS_ERR(req))
1625 		return PTR_ERR(req);
1626 
1627 	spin_lock(&ci->i_ceph_lock);
1628 	issued = __ceph_caps_issued(ci, NULL);
1629 	dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1630 
1631 	if (ia_valid & ATTR_UID) {
1632 		dout("setattr %p uid %d -> %d\n", inode,
1633 		     from_kuid(&init_user_ns, inode->i_uid),
1634 		     from_kuid(&init_user_ns, attr->ia_uid));
1635 		if (issued & CEPH_CAP_AUTH_EXCL) {
1636 			inode->i_uid = attr->ia_uid;
1637 			dirtied |= CEPH_CAP_AUTH_EXCL;
1638 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1639 			   !uid_eq(attr->ia_uid, inode->i_uid)) {
1640 			req->r_args.setattr.uid = cpu_to_le32(
1641 				from_kuid(&init_user_ns, attr->ia_uid));
1642 			mask |= CEPH_SETATTR_UID;
1643 			release |= CEPH_CAP_AUTH_SHARED;
1644 		}
1645 	}
1646 	if (ia_valid & ATTR_GID) {
1647 		dout("setattr %p gid %d -> %d\n", inode,
1648 		     from_kgid(&init_user_ns, inode->i_gid),
1649 		     from_kgid(&init_user_ns, attr->ia_gid));
1650 		if (issued & CEPH_CAP_AUTH_EXCL) {
1651 			inode->i_gid = attr->ia_gid;
1652 			dirtied |= CEPH_CAP_AUTH_EXCL;
1653 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1654 			   !gid_eq(attr->ia_gid, inode->i_gid)) {
1655 			req->r_args.setattr.gid = cpu_to_le32(
1656 				from_kgid(&init_user_ns, attr->ia_gid));
1657 			mask |= CEPH_SETATTR_GID;
1658 			release |= CEPH_CAP_AUTH_SHARED;
1659 		}
1660 	}
1661 	if (ia_valid & ATTR_MODE) {
1662 		dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1663 		     attr->ia_mode);
1664 		if (issued & CEPH_CAP_AUTH_EXCL) {
1665 			inode->i_mode = attr->ia_mode;
1666 			dirtied |= CEPH_CAP_AUTH_EXCL;
1667 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1668 			   attr->ia_mode != inode->i_mode) {
1669 			req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1670 			mask |= CEPH_SETATTR_MODE;
1671 			release |= CEPH_CAP_AUTH_SHARED;
1672 		}
1673 	}
1674 
1675 	if (ia_valid & ATTR_ATIME) {
1676 		dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1677 		     inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1678 		     attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1679 		if (issued & CEPH_CAP_FILE_EXCL) {
1680 			ci->i_time_warp_seq++;
1681 			inode->i_atime = attr->ia_atime;
1682 			dirtied |= CEPH_CAP_FILE_EXCL;
1683 		} else if ((issued & CEPH_CAP_FILE_WR) &&
1684 			   timespec_compare(&inode->i_atime,
1685 					    &attr->ia_atime) < 0) {
1686 			inode->i_atime = attr->ia_atime;
1687 			dirtied |= CEPH_CAP_FILE_WR;
1688 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1689 			   !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1690 			ceph_encode_timespec(&req->r_args.setattr.atime,
1691 					     &attr->ia_atime);
1692 			mask |= CEPH_SETATTR_ATIME;
1693 			release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1694 				CEPH_CAP_FILE_WR;
1695 		}
1696 	}
1697 	if (ia_valid & ATTR_MTIME) {
1698 		dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1699 		     inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1700 		     attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1701 		if (issued & CEPH_CAP_FILE_EXCL) {
1702 			ci->i_time_warp_seq++;
1703 			inode->i_mtime = attr->ia_mtime;
1704 			dirtied |= CEPH_CAP_FILE_EXCL;
1705 		} else if ((issued & CEPH_CAP_FILE_WR) &&
1706 			   timespec_compare(&inode->i_mtime,
1707 					    &attr->ia_mtime) < 0) {
1708 			inode->i_mtime = attr->ia_mtime;
1709 			dirtied |= CEPH_CAP_FILE_WR;
1710 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1711 			   !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1712 			ceph_encode_timespec(&req->r_args.setattr.mtime,
1713 					     &attr->ia_mtime);
1714 			mask |= CEPH_SETATTR_MTIME;
1715 			release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1716 				CEPH_CAP_FILE_WR;
1717 		}
1718 	}
1719 	if (ia_valid & ATTR_SIZE) {
1720 		dout("setattr %p size %lld -> %lld\n", inode,
1721 		     inode->i_size, attr->ia_size);
1722 		if (attr->ia_size > inode->i_sb->s_maxbytes) {
1723 			err = -EINVAL;
1724 			goto out;
1725 		}
1726 		if ((issued & CEPH_CAP_FILE_EXCL) &&
1727 		    attr->ia_size > inode->i_size) {
1728 			inode->i_size = attr->ia_size;
1729 			inode->i_blocks =
1730 				(attr->ia_size + (1 << 9) - 1) >> 9;
1731 			inode->i_ctime = attr->ia_ctime;
1732 			ci->i_reported_size = attr->ia_size;
1733 			dirtied |= CEPH_CAP_FILE_EXCL;
1734 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1735 			   attr->ia_size != inode->i_size) {
1736 			req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1737 			req->r_args.setattr.old_size =
1738 				cpu_to_le64(inode->i_size);
1739 			mask |= CEPH_SETATTR_SIZE;
1740 			release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1741 				CEPH_CAP_FILE_WR;
1742 		}
1743 	}
1744 
1745 	/* these do nothing */
1746 	if (ia_valid & ATTR_CTIME) {
1747 		bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1748 					 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1749 		dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1750 		     inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1751 		     attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1752 		     only ? "ctime only" : "ignored");
1753 		inode->i_ctime = attr->ia_ctime;
1754 		if (only) {
1755 			/*
1756 			 * if kernel wants to dirty ctime but nothing else,
1757 			 * we need to choose a cap to dirty under, or do
1758 			 * a almost-no-op setattr
1759 			 */
1760 			if (issued & CEPH_CAP_AUTH_EXCL)
1761 				dirtied |= CEPH_CAP_AUTH_EXCL;
1762 			else if (issued & CEPH_CAP_FILE_EXCL)
1763 				dirtied |= CEPH_CAP_FILE_EXCL;
1764 			else if (issued & CEPH_CAP_XATTR_EXCL)
1765 				dirtied |= CEPH_CAP_XATTR_EXCL;
1766 			else
1767 				mask |= CEPH_SETATTR_CTIME;
1768 		}
1769 	}
1770 	if (ia_valid & ATTR_FILE)
1771 		dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1772 
1773 	if (dirtied) {
1774 		inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied);
1775 		inode->i_ctime = CURRENT_TIME;
1776 	}
1777 
1778 	release &= issued;
1779 	spin_unlock(&ci->i_ceph_lock);
1780 
1781 	if (inode_dirty_flags)
1782 		__mark_inode_dirty(inode, inode_dirty_flags);
1783 
1784 	if (mask) {
1785 		req->r_inode = inode;
1786 		ihold(inode);
1787 		req->r_inode_drop = release;
1788 		req->r_args.setattr.mask = cpu_to_le32(mask);
1789 		req->r_num_caps = 1;
1790 		parent_inode = ceph_get_dentry_parent_inode(dentry);
1791 		err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1792 		iput(parent_inode);
1793 	}
1794 	dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1795 	     ceph_cap_string(dirtied), mask);
1796 
1797 	ceph_mdsc_put_request(req);
1798 	if (mask & CEPH_SETATTR_SIZE)
1799 		__ceph_do_pending_vmtruncate(inode);
1800 	return err;
1801 out:
1802 	spin_unlock(&ci->i_ceph_lock);
1803 	ceph_mdsc_put_request(req);
1804 	return err;
1805 }
1806 
1807 /*
1808  * Verify that we have a lease on the given mask.  If not,
1809  * do a getattr against an mds.
1810  */
1811 int ceph_do_getattr(struct inode *inode, int mask)
1812 {
1813 	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1814 	struct ceph_mds_client *mdsc = fsc->mdsc;
1815 	struct ceph_mds_request *req;
1816 	int err;
1817 
1818 	if (ceph_snap(inode) == CEPH_SNAPDIR) {
1819 		dout("do_getattr inode %p SNAPDIR\n", inode);
1820 		return 0;
1821 	}
1822 
1823 	dout("do_getattr inode %p mask %s mode 0%o\n", inode, ceph_cap_string(mask), inode->i_mode);
1824 	if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1825 		return 0;
1826 
1827 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1828 	if (IS_ERR(req))
1829 		return PTR_ERR(req);
1830 	req->r_inode = inode;
1831 	ihold(inode);
1832 	req->r_num_caps = 1;
1833 	req->r_args.getattr.mask = cpu_to_le32(mask);
1834 	err = ceph_mdsc_do_request(mdsc, NULL, req);
1835 	ceph_mdsc_put_request(req);
1836 	dout("do_getattr result=%d\n", err);
1837 	return err;
1838 }
1839 
1840 
1841 /*
1842  * Check inode permissions.  We verify we have a valid value for
1843  * the AUTH cap, then call the generic handler.
1844  */
1845 int ceph_permission(struct inode *inode, int mask)
1846 {
1847 	int err;
1848 
1849 	if (mask & MAY_NOT_BLOCK)
1850 		return -ECHILD;
1851 
1852 	err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1853 
1854 	if (!err)
1855 		err = generic_permission(inode, mask);
1856 	return err;
1857 }
1858 
1859 /*
1860  * Get all attributes.  Hopefully somedata we'll have a statlite()
1861  * and can limit the fields we require to be accurate.
1862  */
1863 int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1864 		 struct kstat *stat)
1865 {
1866 	struct inode *inode = dentry->d_inode;
1867 	struct ceph_inode_info *ci = ceph_inode(inode);
1868 	int err;
1869 
1870 	err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1871 	if (!err) {
1872 		generic_fillattr(inode, stat);
1873 		stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
1874 		if (ceph_snap(inode) != CEPH_NOSNAP)
1875 			stat->dev = ceph_snap(inode);
1876 		else
1877 			stat->dev = 0;
1878 		if (S_ISDIR(inode->i_mode)) {
1879 			if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
1880 						RBYTES))
1881 				stat->size = ci->i_rbytes;
1882 			else
1883 				stat->size = ci->i_files + ci->i_subdirs;
1884 			stat->blocks = 0;
1885 			stat->blksize = 65536;
1886 		}
1887 	}
1888 	return err;
1889 }
1890