xref: /openbmc/linux/fs/ceph/inode.c (revision 0f26c4b2)
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 			if (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
420 				      CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
421 				      CEPH_CAP_FILE_EXCL)) {
422 				ci->i_truncate_pending++;
423 				queue_trunc = 1;
424 			}
425 		}
426 	}
427 	if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
428 	    ci->i_truncate_size != truncate_size) {
429 		dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
430 		     truncate_size);
431 		ci->i_truncate_size = truncate_size;
432 	}
433 	return queue_trunc;
434 }
435 
436 void ceph_fill_file_time(struct inode *inode, int issued,
437 			 u64 time_warp_seq, struct timespec *ctime,
438 			 struct timespec *mtime, struct timespec *atime)
439 {
440 	struct ceph_inode_info *ci = ceph_inode(inode);
441 	int warn = 0;
442 
443 	if (issued & (CEPH_CAP_FILE_EXCL|
444 		      CEPH_CAP_FILE_WR|
445 		      CEPH_CAP_FILE_BUFFER)) {
446 		if (timespec_compare(ctime, &inode->i_ctime) > 0) {
447 			dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
448 			     inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
449 			     ctime->tv_sec, ctime->tv_nsec);
450 			inode->i_ctime = *ctime;
451 		}
452 		if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
453 			/* the MDS did a utimes() */
454 			dout("mtime %ld.%09ld -> %ld.%09ld "
455 			     "tw %d -> %d\n",
456 			     inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
457 			     mtime->tv_sec, mtime->tv_nsec,
458 			     ci->i_time_warp_seq, (int)time_warp_seq);
459 
460 			inode->i_mtime = *mtime;
461 			inode->i_atime = *atime;
462 			ci->i_time_warp_seq = time_warp_seq;
463 		} else if (time_warp_seq == ci->i_time_warp_seq) {
464 			/* nobody did utimes(); take the max */
465 			if (timespec_compare(mtime, &inode->i_mtime) > 0) {
466 				dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
467 				     inode->i_mtime.tv_sec,
468 				     inode->i_mtime.tv_nsec,
469 				     mtime->tv_sec, mtime->tv_nsec);
470 				inode->i_mtime = *mtime;
471 			}
472 			if (timespec_compare(atime, &inode->i_atime) > 0) {
473 				dout("atime %ld.%09ld -> %ld.%09ld inc\n",
474 				     inode->i_atime.tv_sec,
475 				     inode->i_atime.tv_nsec,
476 				     atime->tv_sec, atime->tv_nsec);
477 				inode->i_atime = *atime;
478 			}
479 		} else if (issued & CEPH_CAP_FILE_EXCL) {
480 			/* we did a utimes(); ignore mds values */
481 		} else {
482 			warn = 1;
483 		}
484 	} else {
485 		/* we have no write caps; whatever the MDS says is true */
486 		if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
487 			inode->i_ctime = *ctime;
488 			inode->i_mtime = *mtime;
489 			inode->i_atime = *atime;
490 			ci->i_time_warp_seq = time_warp_seq;
491 		} else {
492 			warn = 1;
493 		}
494 	}
495 	if (warn) /* time_warp_seq shouldn't go backwards */
496 		dout("%p mds time_warp_seq %llu < %u\n",
497 		     inode, time_warp_seq, ci->i_time_warp_seq);
498 }
499 
500 /*
501  * Populate an inode based on info from mds.  May be called on new or
502  * existing inodes.
503  */
504 static int fill_inode(struct inode *inode,
505 		      struct ceph_mds_reply_info_in *iinfo,
506 		      struct ceph_mds_reply_dirfrag *dirinfo,
507 		      struct ceph_mds_session *session,
508 		      unsigned long ttl_from, int cap_fmode,
509 		      struct ceph_cap_reservation *caps_reservation)
510 {
511 	struct ceph_mds_reply_inode *info = iinfo->in;
512 	struct ceph_inode_info *ci = ceph_inode(inode);
513 	int i;
514 	int issued, implemented;
515 	struct timespec mtime, atime, ctime;
516 	u32 nsplits;
517 	struct ceph_buffer *xattr_blob = NULL;
518 	int err = 0;
519 	int queue_trunc = 0;
520 
521 	dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
522 	     inode, ceph_vinop(inode), le64_to_cpu(info->version),
523 	     ci->i_version);
524 
525 	/*
526 	 * prealloc xattr data, if it looks like we'll need it.  only
527 	 * if len > 4 (meaning there are actually xattrs; the first 4
528 	 * bytes are the xattr count).
529 	 */
530 	if (iinfo->xattr_len > 4) {
531 		xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
532 		if (!xattr_blob)
533 			pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
534 			       iinfo->xattr_len);
535 	}
536 
537 	spin_lock(&inode->i_lock);
538 
539 	/*
540 	 * provided version will be odd if inode value is projected,
541 	 * even if stable.  skip the update if we have a newer info
542 	 * (e.g., due to inode info racing form multiple MDSs), or if
543 	 * we are getting projected (unstable) inode info.
544 	 */
545 	if (le64_to_cpu(info->version) > 0 &&
546 	    (ci->i_version & ~1) > le64_to_cpu(info->version))
547 		goto no_change;
548 
549 	issued = __ceph_caps_issued(ci, &implemented);
550 	issued |= implemented | __ceph_caps_dirty(ci);
551 
552 	/* update inode */
553 	ci->i_version = le64_to_cpu(info->version);
554 	inode->i_version++;
555 	inode->i_rdev = le32_to_cpu(info->rdev);
556 
557 	if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
558 		inode->i_mode = le32_to_cpu(info->mode);
559 		inode->i_uid = le32_to_cpu(info->uid);
560 		inode->i_gid = le32_to_cpu(info->gid);
561 		dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
562 		     inode->i_uid, inode->i_gid);
563 	}
564 
565 	if ((issued & CEPH_CAP_LINK_EXCL) == 0)
566 		inode->i_nlink = le32_to_cpu(info->nlink);
567 
568 	/* be careful with mtime, atime, size */
569 	ceph_decode_timespec(&atime, &info->atime);
570 	ceph_decode_timespec(&mtime, &info->mtime);
571 	ceph_decode_timespec(&ctime, &info->ctime);
572 	queue_trunc = ceph_fill_file_size(inode, issued,
573 					  le32_to_cpu(info->truncate_seq),
574 					  le64_to_cpu(info->truncate_size),
575 					  le64_to_cpu(info->size));
576 	ceph_fill_file_time(inode, issued,
577 			    le32_to_cpu(info->time_warp_seq),
578 			    &ctime, &mtime, &atime);
579 
580 	ci->i_max_size = le64_to_cpu(info->max_size);
581 	ci->i_layout = info->layout;
582 	inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
583 
584 	/* xattrs */
585 	/* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
586 	if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
587 	    le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
588 		if (ci->i_xattrs.blob)
589 			ceph_buffer_put(ci->i_xattrs.blob);
590 		ci->i_xattrs.blob = xattr_blob;
591 		if (xattr_blob)
592 			memcpy(ci->i_xattrs.blob->vec.iov_base,
593 			       iinfo->xattr_data, iinfo->xattr_len);
594 		ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
595 	}
596 
597 	inode->i_mapping->a_ops = &ceph_aops;
598 	inode->i_mapping->backing_dev_info =
599 		&ceph_client(inode->i_sb)->backing_dev_info;
600 
601 	switch (inode->i_mode & S_IFMT) {
602 	case S_IFIFO:
603 	case S_IFBLK:
604 	case S_IFCHR:
605 	case S_IFSOCK:
606 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
607 		inode->i_op = &ceph_file_iops;
608 		break;
609 	case S_IFREG:
610 		inode->i_op = &ceph_file_iops;
611 		inode->i_fop = &ceph_file_fops;
612 		break;
613 	case S_IFLNK:
614 		inode->i_op = &ceph_symlink_iops;
615 		if (!ci->i_symlink) {
616 			int symlen = iinfo->symlink_len;
617 			char *sym;
618 
619 			BUG_ON(symlen != inode->i_size);
620 			spin_unlock(&inode->i_lock);
621 
622 			err = -ENOMEM;
623 			sym = kmalloc(symlen+1, GFP_NOFS);
624 			if (!sym)
625 				goto out;
626 			memcpy(sym, iinfo->symlink, symlen);
627 			sym[symlen] = 0;
628 
629 			spin_lock(&inode->i_lock);
630 			if (!ci->i_symlink)
631 				ci->i_symlink = sym;
632 			else
633 				kfree(sym); /* lost a race */
634 		}
635 		break;
636 	case S_IFDIR:
637 		inode->i_op = &ceph_dir_iops;
638 		inode->i_fop = &ceph_dir_fops;
639 
640 		ci->i_files = le64_to_cpu(info->files);
641 		ci->i_subdirs = le64_to_cpu(info->subdirs);
642 		ci->i_rbytes = le64_to_cpu(info->rbytes);
643 		ci->i_rfiles = le64_to_cpu(info->rfiles);
644 		ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
645 		ceph_decode_timespec(&ci->i_rctime, &info->rctime);
646 
647 		/* set dir completion flag? */
648 		if (ci->i_files == 0 && ci->i_subdirs == 0 &&
649 		    ceph_snap(inode) == CEPH_NOSNAP &&
650 		    (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED)) {
651 			dout(" marking %p complete (empty)\n", inode);
652 			ci->i_ceph_flags |= CEPH_I_COMPLETE;
653 			ci->i_max_offset = 2;
654 		}
655 
656 		/* it may be better to set st_size in getattr instead? */
657 		if (ceph_test_opt(ceph_client(inode->i_sb), RBYTES))
658 			inode->i_size = ci->i_rbytes;
659 		break;
660 	default:
661 		pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
662 		       ceph_vinop(inode), inode->i_mode);
663 	}
664 
665 no_change:
666 	spin_unlock(&inode->i_lock);
667 
668 	/* queue truncate if we saw i_size decrease */
669 	if (queue_trunc)
670 		if (queue_work(ceph_client(inode->i_sb)->trunc_wq,
671 			       &ci->i_vmtruncate_work))
672 			igrab(inode);
673 
674 	/* populate frag tree */
675 	/* FIXME: move me up, if/when version reflects fragtree changes */
676 	nsplits = le32_to_cpu(info->fragtree.nsplits);
677 	mutex_lock(&ci->i_fragtree_mutex);
678 	for (i = 0; i < nsplits; i++) {
679 		u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
680 		struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
681 
682 		if (IS_ERR(frag))
683 			continue;
684 		frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
685 		dout(" frag %x split by %d\n", frag->frag, frag->split_by);
686 	}
687 	mutex_unlock(&ci->i_fragtree_mutex);
688 
689 	/* were we issued a capability? */
690 	if (info->cap.caps) {
691 		if (ceph_snap(inode) == CEPH_NOSNAP) {
692 			ceph_add_cap(inode, session,
693 				     le64_to_cpu(info->cap.cap_id),
694 				     cap_fmode,
695 				     le32_to_cpu(info->cap.caps),
696 				     le32_to_cpu(info->cap.wanted),
697 				     le32_to_cpu(info->cap.seq),
698 				     le32_to_cpu(info->cap.mseq),
699 				     le64_to_cpu(info->cap.realm),
700 				     info->cap.flags,
701 				     caps_reservation);
702 		} else {
703 			spin_lock(&inode->i_lock);
704 			dout(" %p got snap_caps %s\n", inode,
705 			     ceph_cap_string(le32_to_cpu(info->cap.caps)));
706 			ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
707 			if (cap_fmode >= 0)
708 				__ceph_get_fmode(ci, cap_fmode);
709 			spin_unlock(&inode->i_lock);
710 		}
711 	}
712 
713 	/* update delegation info? */
714 	if (dirinfo)
715 		ceph_fill_dirfrag(inode, dirinfo);
716 
717 	err = 0;
718 
719 out:
720 	if (xattr_blob)
721 		ceph_buffer_put(xattr_blob);
722 	return err;
723 }
724 
725 /*
726  * caller should hold session s_mutex.
727  */
728 static void update_dentry_lease(struct dentry *dentry,
729 				struct ceph_mds_reply_lease *lease,
730 				struct ceph_mds_session *session,
731 				unsigned long from_time)
732 {
733 	struct ceph_dentry_info *di = ceph_dentry(dentry);
734 	long unsigned duration = le32_to_cpu(lease->duration_ms);
735 	long unsigned ttl = from_time + (duration * HZ) / 1000;
736 	long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
737 	struct inode *dir;
738 
739 	/* only track leases on regular dentries */
740 	if (dentry->d_op != &ceph_dentry_ops)
741 		return;
742 
743 	spin_lock(&dentry->d_lock);
744 	dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
745 	     dentry, le16_to_cpu(lease->mask), duration, ttl);
746 
747 	/* make lease_rdcache_gen match directory */
748 	dir = dentry->d_parent->d_inode;
749 	di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
750 
751 	if (lease->mask == 0)
752 		goto out_unlock;
753 
754 	if (di->lease_gen == session->s_cap_gen &&
755 	    time_before(ttl, dentry->d_time))
756 		goto out_unlock;  /* we already have a newer lease. */
757 
758 	if (di->lease_session && di->lease_session != session)
759 		goto out_unlock;
760 
761 	ceph_dentry_lru_touch(dentry);
762 
763 	if (!di->lease_session)
764 		di->lease_session = ceph_get_mds_session(session);
765 	di->lease_gen = session->s_cap_gen;
766 	di->lease_seq = le32_to_cpu(lease->seq);
767 	di->lease_renew_after = half_ttl;
768 	di->lease_renew_from = 0;
769 	dentry->d_time = ttl;
770 out_unlock:
771 	spin_unlock(&dentry->d_lock);
772 	return;
773 }
774 
775 /*
776  * splice a dentry to an inode.
777  * caller must hold directory i_mutex for this to be safe.
778  *
779  * we will only rehash the resulting dentry if @prehash is
780  * true; @prehash will be set to false (for the benefit of
781  * the caller) if we fail.
782  */
783 static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
784 				    bool *prehash)
785 {
786 	struct dentry *realdn;
787 
788 	/* dn must be unhashed */
789 	if (!d_unhashed(dn))
790 		d_drop(dn);
791 	realdn = d_materialise_unique(dn, in);
792 	if (IS_ERR(realdn)) {
793 		pr_err("splice_dentry error %p inode %p ino %llx.%llx\n",
794 		       dn, in, ceph_vinop(in));
795 		if (prehash)
796 			*prehash = false; /* don't rehash on error */
797 		dn = realdn; /* note realdn contains the error */
798 		goto out;
799 	} else if (realdn) {
800 		dout("dn %p (%d) spliced with %p (%d) "
801 		     "inode %p ino %llx.%llx\n",
802 		     dn, atomic_read(&dn->d_count),
803 		     realdn, atomic_read(&realdn->d_count),
804 		     realdn->d_inode, ceph_vinop(realdn->d_inode));
805 		dput(dn);
806 		dn = realdn;
807 	} else {
808 		BUG_ON(!ceph_dentry(dn));
809 
810 		dout("dn %p attached to %p ino %llx.%llx\n",
811 		     dn, dn->d_inode, ceph_vinop(dn->d_inode));
812 	}
813 	if ((!prehash || *prehash) && d_unhashed(dn))
814 		d_rehash(dn);
815 out:
816 	return dn;
817 }
818 
819 /*
820  * Set dentry's directory position based on the current dir's max, and
821  * order it in d_subdirs, so that dcache_readdir behaves.
822  */
823 static void ceph_set_dentry_offset(struct dentry *dn)
824 {
825 	struct dentry *dir = dn->d_parent;
826 	struct inode *inode = dn->d_parent->d_inode;
827 	struct ceph_dentry_info *di;
828 
829 	BUG_ON(!inode);
830 
831 	di = ceph_dentry(dn);
832 
833 	spin_lock(&inode->i_lock);
834 	di->offset = ceph_inode(inode)->i_max_offset++;
835 	spin_unlock(&inode->i_lock);
836 
837 	spin_lock(&dcache_lock);
838 	spin_lock(&dn->d_lock);
839 	list_move_tail(&dir->d_subdirs, &dn->d_u.d_child);
840 	dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
841 	     dn->d_u.d_child.prev, dn->d_u.d_child.next);
842 	spin_unlock(&dn->d_lock);
843 	spin_unlock(&dcache_lock);
844 }
845 
846 /*
847  * Incorporate results into the local cache.  This is either just
848  * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
849  * after a lookup).
850  *
851  * A reply may contain
852  *         a directory inode along with a dentry.
853  *  and/or a target inode
854  *
855  * Called with snap_rwsem (read).
856  */
857 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
858 		    struct ceph_mds_session *session)
859 {
860 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
861 	struct inode *in = NULL;
862 	struct ceph_mds_reply_inode *ininfo;
863 	struct ceph_vino vino;
864 	int i = 0;
865 	int err = 0;
866 
867 	dout("fill_trace %p is_dentry %d is_target %d\n", req,
868 	     rinfo->head->is_dentry, rinfo->head->is_target);
869 
870 #if 0
871 	/*
872 	 * Debugging hook:
873 	 *
874 	 * If we resend completed ops to a recovering mds, we get no
875 	 * trace.  Since that is very rare, pretend this is the case
876 	 * to ensure the 'no trace' handlers in the callers behave.
877 	 *
878 	 * Fill in inodes unconditionally to avoid breaking cap
879 	 * invariants.
880 	 */
881 	if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
882 		pr_info("fill_trace faking empty trace on %lld %s\n",
883 			req->r_tid, ceph_mds_op_name(rinfo->head->op));
884 		if (rinfo->head->is_dentry) {
885 			rinfo->head->is_dentry = 0;
886 			err = fill_inode(req->r_locked_dir,
887 					 &rinfo->diri, rinfo->dirfrag,
888 					 session, req->r_request_started, -1);
889 		}
890 		if (rinfo->head->is_target) {
891 			rinfo->head->is_target = 0;
892 			ininfo = rinfo->targeti.in;
893 			vino.ino = le64_to_cpu(ininfo->ino);
894 			vino.snap = le64_to_cpu(ininfo->snapid);
895 			in = ceph_get_inode(sb, vino);
896 			err = fill_inode(in, &rinfo->targeti, NULL,
897 					 session, req->r_request_started,
898 					 req->r_fmode);
899 			iput(in);
900 		}
901 	}
902 #endif
903 
904 	if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
905 		dout("fill_trace reply is empty!\n");
906 		if (rinfo->head->result == 0 && req->r_locked_dir) {
907 			struct ceph_inode_info *ci =
908 				ceph_inode(req->r_locked_dir);
909 			dout(" clearing %p complete (empty trace)\n",
910 			     req->r_locked_dir);
911 			ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
912 			ci->i_release_count++;
913 		}
914 		return 0;
915 	}
916 
917 	if (rinfo->head->is_dentry) {
918 		struct inode *dir = req->r_locked_dir;
919 
920 		err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
921 				 session, req->r_request_started, -1,
922 				 &req->r_caps_reservation);
923 		if (err < 0)
924 			return err;
925 	}
926 
927 	if (rinfo->head->is_dentry && !req->r_aborted) {
928 		/*
929 		 * lookup link rename   : null -> possibly existing inode
930 		 * mknod symlink mkdir  : null -> new inode
931 		 * unlink               : linked -> null
932 		 */
933 		struct inode *dir = req->r_locked_dir;
934 		struct dentry *dn = req->r_dentry;
935 		bool have_dir_cap, have_lease;
936 
937 		BUG_ON(!dn);
938 		BUG_ON(!dir);
939 		BUG_ON(dn->d_parent->d_inode != dir);
940 		BUG_ON(ceph_ino(dir) !=
941 		       le64_to_cpu(rinfo->diri.in->ino));
942 		BUG_ON(ceph_snap(dir) !=
943 		       le64_to_cpu(rinfo->diri.in->snapid));
944 
945 		/* do we have a lease on the whole dir? */
946 		have_dir_cap =
947 			(le32_to_cpu(rinfo->diri.in->cap.caps) &
948 			 CEPH_CAP_FILE_SHARED);
949 
950 		/* do we have a dn lease? */
951 		have_lease = have_dir_cap ||
952 			(le16_to_cpu(rinfo->dlease->mask) &
953 			 CEPH_LOCK_DN);
954 
955 		if (!have_lease)
956 			dout("fill_trace  no dentry lease or dir cap\n");
957 
958 		/* rename? */
959 		if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
960 			dout(" src %p '%.*s' dst %p '%.*s'\n",
961 			     req->r_old_dentry,
962 			     req->r_old_dentry->d_name.len,
963 			     req->r_old_dentry->d_name.name,
964 			     dn, dn->d_name.len, dn->d_name.name);
965 			dout("fill_trace doing d_move %p -> %p\n",
966 			     req->r_old_dentry, dn);
967 			d_move(req->r_old_dentry, dn);
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 			/* ensure target dentry is invalidated, despite
974 			   rehashing bug in vfs_rename_dir */
975 			dn->d_time = jiffies;
976 			ceph_dentry(dn)->lease_shared_gen = 0;
977 			/* take overwritten dentry's readdir offset */
978 			ceph_dentry(req->r_old_dentry)->offset =
979 				ceph_dentry(dn)->offset;
980 			dn = req->r_old_dentry;  /* use old_dentry */
981 			in = dn->d_inode;
982 		}
983 
984 		/* null dentry? */
985 		if (!rinfo->head->is_target) {
986 			dout("fill_trace null dentry\n");
987 			if (dn->d_inode) {
988 				dout("d_delete %p\n", dn);
989 				d_delete(dn);
990 			} else {
991 				dout("d_instantiate %p NULL\n", dn);
992 				d_instantiate(dn, NULL);
993 				if (have_lease && d_unhashed(dn))
994 					d_rehash(dn);
995 				update_dentry_lease(dn, rinfo->dlease,
996 						    session,
997 						    req->r_request_started);
998 			}
999 			goto done;
1000 		}
1001 
1002 		/* attach proper inode */
1003 		ininfo = rinfo->targeti.in;
1004 		vino.ino = le64_to_cpu(ininfo->ino);
1005 		vino.snap = le64_to_cpu(ininfo->snapid);
1006 		if (!dn->d_inode) {
1007 			in = ceph_get_inode(sb, vino);
1008 			if (IS_ERR(in)) {
1009 				pr_err("fill_trace bad get_inode "
1010 				       "%llx.%llx\n", vino.ino, vino.snap);
1011 				err = PTR_ERR(in);
1012 				d_delete(dn);
1013 				goto done;
1014 			}
1015 			dn = splice_dentry(dn, in, &have_lease);
1016 			if (IS_ERR(dn)) {
1017 				err = PTR_ERR(dn);
1018 				goto done;
1019 			}
1020 			req->r_dentry = dn;  /* may have spliced */
1021 			ceph_set_dentry_offset(dn);
1022 			igrab(in);
1023 		} else if (ceph_ino(in) == vino.ino &&
1024 			   ceph_snap(in) == vino.snap) {
1025 			igrab(in);
1026 		} else {
1027 			dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1028 			     dn, in, ceph_ino(in), ceph_snap(in),
1029 			     vino.ino, vino.snap);
1030 			have_lease = false;
1031 			in = NULL;
1032 		}
1033 
1034 		if (have_lease)
1035 			update_dentry_lease(dn, rinfo->dlease, session,
1036 					    req->r_request_started);
1037 		dout(" final dn %p\n", dn);
1038 		i++;
1039 	} else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1040 		   req->r_op == CEPH_MDS_OP_MKSNAP) {
1041 		struct dentry *dn = req->r_dentry;
1042 
1043 		/* fill out a snapdir LOOKUPSNAP dentry */
1044 		BUG_ON(!dn);
1045 		BUG_ON(!req->r_locked_dir);
1046 		BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1047 		ininfo = rinfo->targeti.in;
1048 		vino.ino = le64_to_cpu(ininfo->ino);
1049 		vino.snap = le64_to_cpu(ininfo->snapid);
1050 		in = ceph_get_inode(sb, vino);
1051 		if (IS_ERR(in)) {
1052 			pr_err("fill_inode get_inode badness %llx.%llx\n",
1053 			       vino.ino, vino.snap);
1054 			err = PTR_ERR(in);
1055 			d_delete(dn);
1056 			goto done;
1057 		}
1058 		dout(" linking snapped dir %p to dn %p\n", in, dn);
1059 		dn = splice_dentry(dn, in, NULL);
1060 		if (IS_ERR(dn)) {
1061 			err = PTR_ERR(dn);
1062 			goto done;
1063 		}
1064 		ceph_set_dentry_offset(dn);
1065 		req->r_dentry = dn;  /* may have spliced */
1066 		igrab(in);
1067 		rinfo->head->is_dentry = 1;  /* fool notrace handlers */
1068 	}
1069 
1070 	if (rinfo->head->is_target) {
1071 		vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1072 		vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1073 
1074 		if (in == NULL || ceph_ino(in) != vino.ino ||
1075 		    ceph_snap(in) != vino.snap) {
1076 			in = ceph_get_inode(sb, vino);
1077 			if (IS_ERR(in)) {
1078 				err = PTR_ERR(in);
1079 				goto done;
1080 			}
1081 		}
1082 		req->r_target_inode = in;
1083 
1084 		err = fill_inode(in,
1085 				 &rinfo->targeti, NULL,
1086 				 session, req->r_request_started,
1087 				 (le32_to_cpu(rinfo->head->result) == 0) ?
1088 				 req->r_fmode : -1,
1089 				 &req->r_caps_reservation);
1090 		if (err < 0) {
1091 			pr_err("fill_inode badness %p %llx.%llx\n",
1092 			       in, ceph_vinop(in));
1093 			goto done;
1094 		}
1095 	}
1096 
1097 done:
1098 	dout("fill_trace done err=%d\n", err);
1099 	return err;
1100 }
1101 
1102 /*
1103  * Prepopulate our cache with readdir results, leases, etc.
1104  */
1105 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1106 			     struct ceph_mds_session *session)
1107 {
1108 	struct dentry *parent = req->r_dentry;
1109 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1110 	struct qstr dname;
1111 	struct dentry *dn;
1112 	struct inode *in;
1113 	int err = 0, i;
1114 	struct inode *snapdir = NULL;
1115 	struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1116 	u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1117 	struct ceph_dentry_info *di;
1118 
1119 	if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1120 		snapdir = ceph_get_snapdir(parent->d_inode);
1121 		parent = d_find_alias(snapdir);
1122 		dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1123 		     rinfo->dir_nr, parent);
1124 	} else {
1125 		dout("readdir_prepopulate %d items under dn %p\n",
1126 		     rinfo->dir_nr, parent);
1127 		if (rinfo->dir_dir)
1128 			ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1129 	}
1130 
1131 	for (i = 0; i < rinfo->dir_nr; i++) {
1132 		struct ceph_vino vino;
1133 
1134 		dname.name = rinfo->dir_dname[i];
1135 		dname.len = rinfo->dir_dname_len[i];
1136 		dname.hash = full_name_hash(dname.name, dname.len);
1137 
1138 		vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1139 		vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1140 
1141 retry_lookup:
1142 		dn = d_lookup(parent, &dname);
1143 		dout("d_lookup on parent=%p name=%.*s got %p\n",
1144 		     parent, dname.len, dname.name, dn);
1145 
1146 		if (!dn) {
1147 			dn = d_alloc(parent, &dname);
1148 			dout("d_alloc %p '%.*s' = %p\n", parent,
1149 			     dname.len, dname.name, dn);
1150 			if (dn == NULL) {
1151 				dout("d_alloc badness\n");
1152 				err = -ENOMEM;
1153 				goto out;
1154 			}
1155 			err = ceph_init_dentry(dn);
1156 			if (err < 0)
1157 				goto out;
1158 		} else if (dn->d_inode &&
1159 			   (ceph_ino(dn->d_inode) != vino.ino ||
1160 			    ceph_snap(dn->d_inode) != vino.snap)) {
1161 			dout(" dn %p points to wrong inode %p\n",
1162 			     dn, dn->d_inode);
1163 			d_delete(dn);
1164 			dput(dn);
1165 			goto retry_lookup;
1166 		} else {
1167 			/* reorder parent's d_subdirs */
1168 			spin_lock(&dcache_lock);
1169 			spin_lock(&dn->d_lock);
1170 			list_move(&dn->d_u.d_child, &parent->d_subdirs);
1171 			spin_unlock(&dn->d_lock);
1172 			spin_unlock(&dcache_lock);
1173 		}
1174 
1175 		di = dn->d_fsdata;
1176 		di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1177 
1178 		/* inode */
1179 		if (dn->d_inode) {
1180 			in = dn->d_inode;
1181 		} else {
1182 			in = ceph_get_inode(parent->d_sb, vino);
1183 			if (in == NULL) {
1184 				dout("new_inode badness\n");
1185 				d_delete(dn);
1186 				dput(dn);
1187 				err = -ENOMEM;
1188 				goto out;
1189 			}
1190 			dn = splice_dentry(dn, in, NULL);
1191 		}
1192 
1193 		if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1194 			       req->r_request_started, -1,
1195 			       &req->r_caps_reservation) < 0) {
1196 			pr_err("fill_inode badness on %p\n", in);
1197 			dput(dn);
1198 			continue;
1199 		}
1200 		update_dentry_lease(dn, rinfo->dir_dlease[i],
1201 				    req->r_session, req->r_request_started);
1202 		dput(dn);
1203 	}
1204 	req->r_did_prepopulate = true;
1205 
1206 out:
1207 	if (snapdir) {
1208 		iput(snapdir);
1209 		dput(parent);
1210 	}
1211 	dout("readdir_prepopulate done\n");
1212 	return err;
1213 }
1214 
1215 int ceph_inode_set_size(struct inode *inode, loff_t size)
1216 {
1217 	struct ceph_inode_info *ci = ceph_inode(inode);
1218 	int ret = 0;
1219 
1220 	spin_lock(&inode->i_lock);
1221 	dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1222 	inode->i_size = size;
1223 	inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1224 
1225 	/* tell the MDS if we are approaching max_size */
1226 	if ((size << 1) >= ci->i_max_size &&
1227 	    (ci->i_reported_size << 1) < ci->i_max_size)
1228 		ret = 1;
1229 
1230 	spin_unlock(&inode->i_lock);
1231 	return ret;
1232 }
1233 
1234 /*
1235  * Write back inode data in a worker thread.  (This can't be done
1236  * in the message handler context.)
1237  */
1238 void ceph_inode_writeback(struct work_struct *work)
1239 {
1240 	struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1241 						  i_wb_work);
1242 	struct inode *inode = &ci->vfs_inode;
1243 
1244 	dout("writeback %p\n", inode);
1245 	filemap_fdatawrite(&inode->i_data);
1246 	iput(inode);
1247 }
1248 
1249 /*
1250  * Invalidate inode pages in a worker thread.  (This can't be done
1251  * in the message handler context.)
1252  */
1253 static void ceph_inode_invalidate_pages(struct work_struct *work)
1254 {
1255 	struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1256 						  i_pg_inv_work);
1257 	struct inode *inode = &ci->vfs_inode;
1258 	u32 orig_gen;
1259 	int check = 0;
1260 
1261 	spin_lock(&inode->i_lock);
1262 	dout("invalidate_pages %p gen %d revoking %d\n", inode,
1263 	     ci->i_rdcache_gen, ci->i_rdcache_revoking);
1264 	if (ci->i_rdcache_gen == 0 ||
1265 	    ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1266 		BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen);
1267 		/* nevermind! */
1268 		ci->i_rdcache_revoking = 0;
1269 		spin_unlock(&inode->i_lock);
1270 		goto out;
1271 	}
1272 	orig_gen = ci->i_rdcache_gen;
1273 	spin_unlock(&inode->i_lock);
1274 
1275 	truncate_inode_pages(&inode->i_data, 0);
1276 
1277 	spin_lock(&inode->i_lock);
1278 	if (orig_gen == ci->i_rdcache_gen) {
1279 		dout("invalidate_pages %p gen %d successful\n", inode,
1280 		     ci->i_rdcache_gen);
1281 		ci->i_rdcache_gen = 0;
1282 		ci->i_rdcache_revoking = 0;
1283 		check = 1;
1284 	} else {
1285 		dout("invalidate_pages %p gen %d raced, gen now %d\n",
1286 		     inode, orig_gen, ci->i_rdcache_gen);
1287 	}
1288 	spin_unlock(&inode->i_lock);
1289 
1290 	if (check)
1291 		ceph_check_caps(ci, 0, NULL);
1292 out:
1293 	iput(inode);
1294 }
1295 
1296 
1297 /*
1298  * called by trunc_wq; take i_mutex ourselves
1299  *
1300  * We also truncate in a separate thread as well.
1301  */
1302 void ceph_vmtruncate_work(struct work_struct *work)
1303 {
1304 	struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1305 						  i_vmtruncate_work);
1306 	struct inode *inode = &ci->vfs_inode;
1307 
1308 	dout("vmtruncate_work %p\n", inode);
1309 	mutex_lock(&inode->i_mutex);
1310 	__ceph_do_pending_vmtruncate(inode);
1311 	mutex_unlock(&inode->i_mutex);
1312 	iput(inode);
1313 }
1314 
1315 /*
1316  * called with i_mutex held.
1317  *
1318  * Make sure any pending truncation is applied before doing anything
1319  * that may depend on it.
1320  */
1321 void __ceph_do_pending_vmtruncate(struct inode *inode)
1322 {
1323 	struct ceph_inode_info *ci = ceph_inode(inode);
1324 	u64 to;
1325 	int wrbuffer_refs, wake = 0;
1326 
1327 retry:
1328 	spin_lock(&inode->i_lock);
1329 	if (ci->i_truncate_pending == 0) {
1330 		dout("__do_pending_vmtruncate %p none pending\n", inode);
1331 		spin_unlock(&inode->i_lock);
1332 		return;
1333 	}
1334 
1335 	/*
1336 	 * make sure any dirty snapped pages are flushed before we
1337 	 * possibly truncate them.. so write AND block!
1338 	 */
1339 	if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1340 		dout("__do_pending_vmtruncate %p flushing snaps first\n",
1341 		     inode);
1342 		spin_unlock(&inode->i_lock);
1343 		filemap_write_and_wait_range(&inode->i_data, 0,
1344 					     inode->i_sb->s_maxbytes);
1345 		goto retry;
1346 	}
1347 
1348 	to = ci->i_truncate_size;
1349 	wrbuffer_refs = ci->i_wrbuffer_ref;
1350 	dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1351 	     ci->i_truncate_pending, to);
1352 	spin_unlock(&inode->i_lock);
1353 
1354 	truncate_inode_pages(inode->i_mapping, to);
1355 
1356 	spin_lock(&inode->i_lock);
1357 	ci->i_truncate_pending--;
1358 	if (ci->i_truncate_pending == 0)
1359 		wake = 1;
1360 	spin_unlock(&inode->i_lock);
1361 
1362 	if (wrbuffer_refs == 0)
1363 		ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1364 	if (wake)
1365 		wake_up(&ci->i_cap_wq);
1366 }
1367 
1368 
1369 /*
1370  * symlinks
1371  */
1372 static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1373 {
1374 	struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1375 	nd_set_link(nd, ci->i_symlink);
1376 	return NULL;
1377 }
1378 
1379 static const struct inode_operations ceph_symlink_iops = {
1380 	.readlink = generic_readlink,
1381 	.follow_link = ceph_sym_follow_link,
1382 };
1383 
1384 /*
1385  * setattr
1386  */
1387 int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1388 {
1389 	struct inode *inode = dentry->d_inode;
1390 	struct ceph_inode_info *ci = ceph_inode(inode);
1391 	struct inode *parent_inode = dentry->d_parent->d_inode;
1392 	const unsigned int ia_valid = attr->ia_valid;
1393 	struct ceph_mds_request *req;
1394 	struct ceph_mds_client *mdsc = &ceph_client(dentry->d_sb)->mdsc;
1395 	int issued;
1396 	int release = 0, dirtied = 0;
1397 	int mask = 0;
1398 	int err = 0;
1399 
1400 	if (ceph_snap(inode) != CEPH_NOSNAP)
1401 		return -EROFS;
1402 
1403 	__ceph_do_pending_vmtruncate(inode);
1404 
1405 	err = inode_change_ok(inode, attr);
1406 	if (err != 0)
1407 		return err;
1408 
1409 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1410 				       USE_AUTH_MDS);
1411 	if (IS_ERR(req))
1412 		return PTR_ERR(req);
1413 
1414 	spin_lock(&inode->i_lock);
1415 	issued = __ceph_caps_issued(ci, NULL);
1416 	dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1417 
1418 	if (ia_valid & ATTR_UID) {
1419 		dout("setattr %p uid %d -> %d\n", inode,
1420 		     inode->i_uid, attr->ia_uid);
1421 		if (issued & CEPH_CAP_AUTH_EXCL) {
1422 			inode->i_uid = attr->ia_uid;
1423 			dirtied |= CEPH_CAP_AUTH_EXCL;
1424 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1425 			   attr->ia_uid != inode->i_uid) {
1426 			req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
1427 			mask |= CEPH_SETATTR_UID;
1428 			release |= CEPH_CAP_AUTH_SHARED;
1429 		}
1430 	}
1431 	if (ia_valid & ATTR_GID) {
1432 		dout("setattr %p gid %d -> %d\n", inode,
1433 		     inode->i_gid, attr->ia_gid);
1434 		if (issued & CEPH_CAP_AUTH_EXCL) {
1435 			inode->i_gid = attr->ia_gid;
1436 			dirtied |= CEPH_CAP_AUTH_EXCL;
1437 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1438 			   attr->ia_gid != inode->i_gid) {
1439 			req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
1440 			mask |= CEPH_SETATTR_GID;
1441 			release |= CEPH_CAP_AUTH_SHARED;
1442 		}
1443 	}
1444 	if (ia_valid & ATTR_MODE) {
1445 		dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1446 		     attr->ia_mode);
1447 		if (issued & CEPH_CAP_AUTH_EXCL) {
1448 			inode->i_mode = attr->ia_mode;
1449 			dirtied |= CEPH_CAP_AUTH_EXCL;
1450 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1451 			   attr->ia_mode != inode->i_mode) {
1452 			req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1453 			mask |= CEPH_SETATTR_MODE;
1454 			release |= CEPH_CAP_AUTH_SHARED;
1455 		}
1456 	}
1457 
1458 	if (ia_valid & ATTR_ATIME) {
1459 		dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1460 		     inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1461 		     attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1462 		if (issued & CEPH_CAP_FILE_EXCL) {
1463 			ci->i_time_warp_seq++;
1464 			inode->i_atime = attr->ia_atime;
1465 			dirtied |= CEPH_CAP_FILE_EXCL;
1466 		} else if ((issued & CEPH_CAP_FILE_WR) &&
1467 			   timespec_compare(&inode->i_atime,
1468 					    &attr->ia_atime) < 0) {
1469 			inode->i_atime = attr->ia_atime;
1470 			dirtied |= CEPH_CAP_FILE_WR;
1471 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1472 			   !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1473 			ceph_encode_timespec(&req->r_args.setattr.atime,
1474 					     &attr->ia_atime);
1475 			mask |= CEPH_SETATTR_ATIME;
1476 			release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1477 				CEPH_CAP_FILE_WR;
1478 		}
1479 	}
1480 	if (ia_valid & ATTR_MTIME) {
1481 		dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1482 		     inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1483 		     attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1484 		if (issued & CEPH_CAP_FILE_EXCL) {
1485 			ci->i_time_warp_seq++;
1486 			inode->i_mtime = attr->ia_mtime;
1487 			dirtied |= CEPH_CAP_FILE_EXCL;
1488 		} else if ((issued & CEPH_CAP_FILE_WR) &&
1489 			   timespec_compare(&inode->i_mtime,
1490 					    &attr->ia_mtime) < 0) {
1491 			inode->i_mtime = attr->ia_mtime;
1492 			dirtied |= CEPH_CAP_FILE_WR;
1493 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1494 			   !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1495 			ceph_encode_timespec(&req->r_args.setattr.mtime,
1496 					     &attr->ia_mtime);
1497 			mask |= CEPH_SETATTR_MTIME;
1498 			release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1499 				CEPH_CAP_FILE_WR;
1500 		}
1501 	}
1502 	if (ia_valid & ATTR_SIZE) {
1503 		dout("setattr %p size %lld -> %lld\n", inode,
1504 		     inode->i_size, attr->ia_size);
1505 		if (attr->ia_size > inode->i_sb->s_maxbytes) {
1506 			err = -EINVAL;
1507 			goto out;
1508 		}
1509 		if ((issued & CEPH_CAP_FILE_EXCL) &&
1510 		    attr->ia_size > inode->i_size) {
1511 			inode->i_size = attr->ia_size;
1512 			inode->i_blocks =
1513 				(attr->ia_size + (1 << 9) - 1) >> 9;
1514 			inode->i_ctime = attr->ia_ctime;
1515 			ci->i_reported_size = attr->ia_size;
1516 			dirtied |= CEPH_CAP_FILE_EXCL;
1517 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1518 			   attr->ia_size != inode->i_size) {
1519 			req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1520 			req->r_args.setattr.old_size =
1521 				cpu_to_le64(inode->i_size);
1522 			mask |= CEPH_SETATTR_SIZE;
1523 			release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1524 				CEPH_CAP_FILE_WR;
1525 		}
1526 	}
1527 
1528 	/* these do nothing */
1529 	if (ia_valid & ATTR_CTIME) {
1530 		bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1531 					 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1532 		dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1533 		     inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1534 		     attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1535 		     only ? "ctime only" : "ignored");
1536 		inode->i_ctime = attr->ia_ctime;
1537 		if (only) {
1538 			/*
1539 			 * if kernel wants to dirty ctime but nothing else,
1540 			 * we need to choose a cap to dirty under, or do
1541 			 * a almost-no-op setattr
1542 			 */
1543 			if (issued & CEPH_CAP_AUTH_EXCL)
1544 				dirtied |= CEPH_CAP_AUTH_EXCL;
1545 			else if (issued & CEPH_CAP_FILE_EXCL)
1546 				dirtied |= CEPH_CAP_FILE_EXCL;
1547 			else if (issued & CEPH_CAP_XATTR_EXCL)
1548 				dirtied |= CEPH_CAP_XATTR_EXCL;
1549 			else
1550 				mask |= CEPH_SETATTR_CTIME;
1551 		}
1552 	}
1553 	if (ia_valid & ATTR_FILE)
1554 		dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1555 
1556 	if (dirtied) {
1557 		__ceph_mark_dirty_caps(ci, dirtied);
1558 		inode->i_ctime = CURRENT_TIME;
1559 	}
1560 
1561 	release &= issued;
1562 	spin_unlock(&inode->i_lock);
1563 
1564 	if (mask) {
1565 		req->r_inode = igrab(inode);
1566 		req->r_inode_drop = release;
1567 		req->r_args.setattr.mask = cpu_to_le32(mask);
1568 		req->r_num_caps = 1;
1569 		err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1570 	}
1571 	dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1572 	     ceph_cap_string(dirtied), mask);
1573 
1574 	ceph_mdsc_put_request(req);
1575 	__ceph_do_pending_vmtruncate(inode);
1576 	return err;
1577 out:
1578 	spin_unlock(&inode->i_lock);
1579 	ceph_mdsc_put_request(req);
1580 	return err;
1581 }
1582 
1583 /*
1584  * Verify that we have a lease on the given mask.  If not,
1585  * do a getattr against an mds.
1586  */
1587 int ceph_do_getattr(struct inode *inode, int mask)
1588 {
1589 	struct ceph_client *client = ceph_sb_to_client(inode->i_sb);
1590 	struct ceph_mds_client *mdsc = &client->mdsc;
1591 	struct ceph_mds_request *req;
1592 	int err;
1593 
1594 	if (ceph_snap(inode) == CEPH_SNAPDIR) {
1595 		dout("do_getattr inode %p SNAPDIR\n", inode);
1596 		return 0;
1597 	}
1598 
1599 	dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask));
1600 	if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1601 		return 0;
1602 
1603 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1604 	if (IS_ERR(req))
1605 		return PTR_ERR(req);
1606 	req->r_inode = igrab(inode);
1607 	req->r_num_caps = 1;
1608 	req->r_args.getattr.mask = cpu_to_le32(mask);
1609 	err = ceph_mdsc_do_request(mdsc, NULL, req);
1610 	ceph_mdsc_put_request(req);
1611 	dout("do_getattr result=%d\n", err);
1612 	return err;
1613 }
1614 
1615 
1616 /*
1617  * Check inode permissions.  We verify we have a valid value for
1618  * the AUTH cap, then call the generic handler.
1619  */
1620 int ceph_permission(struct inode *inode, int mask)
1621 {
1622 	int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1623 
1624 	if (!err)
1625 		err = generic_permission(inode, mask, NULL);
1626 	return err;
1627 }
1628 
1629 /*
1630  * Get all attributes.  Hopefully somedata we'll have a statlite()
1631  * and can limit the fields we require to be accurate.
1632  */
1633 int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1634 		 struct kstat *stat)
1635 {
1636 	struct inode *inode = dentry->d_inode;
1637 	struct ceph_inode_info *ci = ceph_inode(inode);
1638 	int err;
1639 
1640 	err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1641 	if (!err) {
1642 		generic_fillattr(inode, stat);
1643 		stat->ino = inode->i_ino;
1644 		if (ceph_snap(inode) != CEPH_NOSNAP)
1645 			stat->dev = ceph_snap(inode);
1646 		else
1647 			stat->dev = 0;
1648 		if (S_ISDIR(inode->i_mode)) {
1649 			stat->size = ci->i_rbytes;
1650 			stat->blocks = 0;
1651 			stat->blksize = 65536;
1652 		}
1653 	}
1654 	return err;
1655 }
1656