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