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