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