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