xref: /openbmc/linux/fs/ceph/file.c (revision 84fbfc33)
1 #include <linux/ceph/ceph_debug.h>
2 
3 #include <linux/module.h>
4 #include <linux/sched.h>
5 #include <linux/slab.h>
6 #include <linux/file.h>
7 #include <linux/mount.h>
8 #include <linux/namei.h>
9 #include <linux/writeback.h>
10 #include <linux/falloc.h>
11 
12 #include "super.h"
13 #include "mds_client.h"
14 #include "cache.h"
15 
16 static __le32 ceph_flags_sys2wire(u32 flags)
17 {
18 	u32 wire_flags = 0;
19 
20 	switch (flags & O_ACCMODE) {
21 	case O_RDONLY:
22 		wire_flags |= CEPH_O_RDONLY;
23 		break;
24 	case O_WRONLY:
25 		wire_flags |= CEPH_O_WRONLY;
26 		break;
27 	case O_RDWR:
28 		wire_flags |= CEPH_O_RDWR;
29 		break;
30 	}
31 
32 #define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; }
33 
34 	ceph_sys2wire(O_CREAT);
35 	ceph_sys2wire(O_EXCL);
36 	ceph_sys2wire(O_TRUNC);
37 	ceph_sys2wire(O_DIRECTORY);
38 	ceph_sys2wire(O_NOFOLLOW);
39 
40 #undef ceph_sys2wire
41 
42 	if (flags)
43 		dout("unused open flags: %x", flags);
44 
45 	return cpu_to_le32(wire_flags);
46 }
47 
48 /*
49  * Ceph file operations
50  *
51  * Implement basic open/close functionality, and implement
52  * read/write.
53  *
54  * We implement three modes of file I/O:
55  *  - buffered uses the generic_file_aio_{read,write} helpers
56  *
57  *  - synchronous is used when there is multi-client read/write
58  *    sharing, avoids the page cache, and synchronously waits for an
59  *    ack from the OSD.
60  *
61  *  - direct io takes the variant of the sync path that references
62  *    user pages directly.
63  *
64  * fsync() flushes and waits on dirty pages, but just queues metadata
65  * for writeback: since the MDS can recover size and mtime there is no
66  * need to wait for MDS acknowledgement.
67  */
68 
69 /*
70  * Calculate the length sum of direct io vectors that can
71  * be combined into one page vector.
72  */
73 static size_t dio_get_pagev_size(const struct iov_iter *it)
74 {
75     const struct iovec *iov = it->iov;
76     const struct iovec *iovend = iov + it->nr_segs;
77     size_t size;
78 
79     size = iov->iov_len - it->iov_offset;
80     /*
81      * An iov can be page vectored when both the current tail
82      * and the next base are page aligned.
83      */
84     while (PAGE_ALIGNED((iov->iov_base + iov->iov_len)) &&
85            (++iov < iovend && PAGE_ALIGNED((iov->iov_base)))) {
86         size += iov->iov_len;
87     }
88     dout("dio_get_pagevlen len = %zu\n", size);
89     return size;
90 }
91 
92 /*
93  * Allocate a page vector based on (@it, @nbytes).
94  * The return value is the tuple describing a page vector,
95  * that is (@pages, @page_align, @num_pages).
96  */
97 static struct page **
98 dio_get_pages_alloc(const struct iov_iter *it, size_t nbytes,
99 		    size_t *page_align, int *num_pages)
100 {
101 	struct iov_iter tmp_it = *it;
102 	size_t align;
103 	struct page **pages;
104 	int ret = 0, idx, npages;
105 
106 	align = (unsigned long)(it->iov->iov_base + it->iov_offset) &
107 		(PAGE_SIZE - 1);
108 	npages = calc_pages_for(align, nbytes);
109 	pages = kvmalloc(sizeof(*pages) * npages, GFP_KERNEL);
110 	if (!pages)
111 		return ERR_PTR(-ENOMEM);
112 
113 	for (idx = 0; idx < npages; ) {
114 		size_t start;
115 		ret = iov_iter_get_pages(&tmp_it, pages + idx, nbytes,
116 					 npages - idx, &start);
117 		if (ret < 0)
118 			goto fail;
119 
120 		iov_iter_advance(&tmp_it, ret);
121 		nbytes -= ret;
122 		idx += (ret + start + PAGE_SIZE - 1) / PAGE_SIZE;
123 	}
124 
125 	BUG_ON(nbytes != 0);
126 	*num_pages = npages;
127 	*page_align = align;
128 	dout("dio_get_pages_alloc: got %d pages align %zu\n", npages, align);
129 	return pages;
130 fail:
131 	ceph_put_page_vector(pages, idx, false);
132 	return ERR_PTR(ret);
133 }
134 
135 /*
136  * Prepare an open request.  Preallocate ceph_cap to avoid an
137  * inopportune ENOMEM later.
138  */
139 static struct ceph_mds_request *
140 prepare_open_request(struct super_block *sb, int flags, int create_mode)
141 {
142 	struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
143 	struct ceph_mds_client *mdsc = fsc->mdsc;
144 	struct ceph_mds_request *req;
145 	int want_auth = USE_ANY_MDS;
146 	int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
147 
148 	if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
149 		want_auth = USE_AUTH_MDS;
150 
151 	req = ceph_mdsc_create_request(mdsc, op, want_auth);
152 	if (IS_ERR(req))
153 		goto out;
154 	req->r_fmode = ceph_flags_to_mode(flags);
155 	req->r_args.open.flags = ceph_flags_sys2wire(flags);
156 	req->r_args.open.mode = cpu_to_le32(create_mode);
157 out:
158 	return req;
159 }
160 
161 /*
162  * initialize private struct file data.
163  * if we fail, clean up by dropping fmode reference on the ceph_inode
164  */
165 static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
166 {
167 	struct ceph_file_info *cf;
168 	int ret = 0;
169 
170 	switch (inode->i_mode & S_IFMT) {
171 	case S_IFREG:
172 		ceph_fscache_register_inode_cookie(inode);
173 		ceph_fscache_file_set_cookie(inode, file);
174 	case S_IFDIR:
175 		dout("init_file %p %p 0%o (regular)\n", inode, file,
176 		     inode->i_mode);
177 		cf = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
178 		if (!cf) {
179 			ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
180 			return -ENOMEM;
181 		}
182 		cf->fmode = fmode;
183 		cf->next_offset = 2;
184 		cf->readdir_cache_idx = -1;
185 		file->private_data = cf;
186 		BUG_ON(inode->i_fop->release != ceph_release);
187 		break;
188 
189 	case S_IFLNK:
190 		dout("init_file %p %p 0%o (symlink)\n", inode, file,
191 		     inode->i_mode);
192 		ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
193 		break;
194 
195 	default:
196 		dout("init_file %p %p 0%o (special)\n", inode, file,
197 		     inode->i_mode);
198 		/*
199 		 * we need to drop the open ref now, since we don't
200 		 * have .release set to ceph_release.
201 		 */
202 		ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */
203 		BUG_ON(inode->i_fop->release == ceph_release);
204 
205 		/* call the proper open fop */
206 		ret = inode->i_fop->open(inode, file);
207 	}
208 	return ret;
209 }
210 
211 /*
212  * try renew caps after session gets killed.
213  */
214 int ceph_renew_caps(struct inode *inode)
215 {
216 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
217 	struct ceph_inode_info *ci = ceph_inode(inode);
218 	struct ceph_mds_request *req;
219 	int err, flags, wanted;
220 
221 	spin_lock(&ci->i_ceph_lock);
222 	wanted = __ceph_caps_file_wanted(ci);
223 	if (__ceph_is_any_real_caps(ci) &&
224 	    (!(wanted & CEPH_CAP_ANY_WR) || ci->i_auth_cap)) {
225 		int issued = __ceph_caps_issued(ci, NULL);
226 		spin_unlock(&ci->i_ceph_lock);
227 		dout("renew caps %p want %s issued %s updating mds_wanted\n",
228 		     inode, ceph_cap_string(wanted), ceph_cap_string(issued));
229 		ceph_check_caps(ci, 0, NULL);
230 		return 0;
231 	}
232 	spin_unlock(&ci->i_ceph_lock);
233 
234 	flags = 0;
235 	if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
236 		flags = O_RDWR;
237 	else if (wanted & CEPH_CAP_FILE_RD)
238 		flags = O_RDONLY;
239 	else if (wanted & CEPH_CAP_FILE_WR)
240 		flags = O_WRONLY;
241 #ifdef O_LAZY
242 	if (wanted & CEPH_CAP_FILE_LAZYIO)
243 		flags |= O_LAZY;
244 #endif
245 
246 	req = prepare_open_request(inode->i_sb, flags, 0);
247 	if (IS_ERR(req)) {
248 		err = PTR_ERR(req);
249 		goto out;
250 	}
251 
252 	req->r_inode = inode;
253 	ihold(inode);
254 	req->r_num_caps = 1;
255 	req->r_fmode = -1;
256 
257 	err = ceph_mdsc_do_request(mdsc, NULL, req);
258 	ceph_mdsc_put_request(req);
259 out:
260 	dout("renew caps %p open result=%d\n", inode, err);
261 	return err < 0 ? err : 0;
262 }
263 
264 /*
265  * If we already have the requisite capabilities, we can satisfy
266  * the open request locally (no need to request new caps from the
267  * MDS).  We do, however, need to inform the MDS (asynchronously)
268  * if our wanted caps set expands.
269  */
270 int ceph_open(struct inode *inode, struct file *file)
271 {
272 	struct ceph_inode_info *ci = ceph_inode(inode);
273 	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
274 	struct ceph_mds_client *mdsc = fsc->mdsc;
275 	struct ceph_mds_request *req;
276 	struct ceph_file_info *cf = file->private_data;
277 	int err;
278 	int flags, fmode, wanted;
279 
280 	if (cf) {
281 		dout("open file %p is already opened\n", file);
282 		return 0;
283 	}
284 
285 	/* filter out O_CREAT|O_EXCL; vfs did that already.  yuck. */
286 	flags = file->f_flags & ~(O_CREAT|O_EXCL);
287 	if (S_ISDIR(inode->i_mode))
288 		flags = O_DIRECTORY;  /* mds likes to know */
289 
290 	dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
291 	     ceph_vinop(inode), file, flags, file->f_flags);
292 	fmode = ceph_flags_to_mode(flags);
293 	wanted = ceph_caps_for_mode(fmode);
294 
295 	/* snapped files are read-only */
296 	if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
297 		return -EROFS;
298 
299 	/* trivially open snapdir */
300 	if (ceph_snap(inode) == CEPH_SNAPDIR) {
301 		spin_lock(&ci->i_ceph_lock);
302 		__ceph_get_fmode(ci, fmode);
303 		spin_unlock(&ci->i_ceph_lock);
304 		return ceph_init_file(inode, file, fmode);
305 	}
306 
307 	/*
308 	 * No need to block if we have caps on the auth MDS (for
309 	 * write) or any MDS (for read).  Update wanted set
310 	 * asynchronously.
311 	 */
312 	spin_lock(&ci->i_ceph_lock);
313 	if (__ceph_is_any_real_caps(ci) &&
314 	    (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
315 		int mds_wanted = __ceph_caps_mds_wanted(ci, true);
316 		int issued = __ceph_caps_issued(ci, NULL);
317 
318 		dout("open %p fmode %d want %s issued %s using existing\n",
319 		     inode, fmode, ceph_cap_string(wanted),
320 		     ceph_cap_string(issued));
321 		__ceph_get_fmode(ci, fmode);
322 		spin_unlock(&ci->i_ceph_lock);
323 
324 		/* adjust wanted? */
325 		if ((issued & wanted) != wanted &&
326 		    (mds_wanted & wanted) != wanted &&
327 		    ceph_snap(inode) != CEPH_SNAPDIR)
328 			ceph_check_caps(ci, 0, NULL);
329 
330 		return ceph_init_file(inode, file, fmode);
331 	} else if (ceph_snap(inode) != CEPH_NOSNAP &&
332 		   (ci->i_snap_caps & wanted) == wanted) {
333 		__ceph_get_fmode(ci, fmode);
334 		spin_unlock(&ci->i_ceph_lock);
335 		return ceph_init_file(inode, file, fmode);
336 	}
337 
338 	spin_unlock(&ci->i_ceph_lock);
339 
340 	dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
341 	req = prepare_open_request(inode->i_sb, flags, 0);
342 	if (IS_ERR(req)) {
343 		err = PTR_ERR(req);
344 		goto out;
345 	}
346 	req->r_inode = inode;
347 	ihold(inode);
348 
349 	req->r_num_caps = 1;
350 	err = ceph_mdsc_do_request(mdsc, NULL, req);
351 	if (!err)
352 		err = ceph_init_file(inode, file, req->r_fmode);
353 	ceph_mdsc_put_request(req);
354 	dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
355 out:
356 	return err;
357 }
358 
359 
360 /*
361  * Do a lookup + open with a single request.  If we get a non-existent
362  * file or symlink, return 1 so the VFS can retry.
363  */
364 int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
365 		     struct file *file, unsigned flags, umode_t mode,
366 		     int *opened)
367 {
368 	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
369 	struct ceph_mds_client *mdsc = fsc->mdsc;
370 	struct ceph_mds_request *req;
371 	struct dentry *dn;
372 	struct ceph_acls_info acls = {};
373        int mask;
374 	int err;
375 
376 	dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
377 	     dir, dentry, dentry,
378 	     d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
379 
380 	if (dentry->d_name.len > NAME_MAX)
381 		return -ENAMETOOLONG;
382 
383 	if (flags & O_CREAT) {
384 		err = ceph_pre_init_acls(dir, &mode, &acls);
385 		if (err < 0)
386 			return err;
387 	}
388 
389 	/* do the open */
390 	req = prepare_open_request(dir->i_sb, flags, mode);
391 	if (IS_ERR(req)) {
392 		err = PTR_ERR(req);
393 		goto out_acl;
394 	}
395 	req->r_dentry = dget(dentry);
396 	req->r_num_caps = 2;
397 	if (flags & O_CREAT) {
398 		req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
399 		req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
400 		if (acls.pagelist) {
401 			req->r_pagelist = acls.pagelist;
402 			acls.pagelist = NULL;
403 		}
404 	}
405 
406        mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
407        if (ceph_security_xattr_wanted(dir))
408                mask |= CEPH_CAP_XATTR_SHARED;
409        req->r_args.open.mask = cpu_to_le32(mask);
410 
411 	req->r_parent = dir;
412 	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
413 	err = ceph_mdsc_do_request(mdsc,
414 				   (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
415 				   req);
416 	err = ceph_handle_snapdir(req, dentry, err);
417 	if (err)
418 		goto out_req;
419 
420 	if ((flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
421 		err = ceph_handle_notrace_create(dir, dentry);
422 
423 	if (d_in_lookup(dentry)) {
424 		dn = ceph_finish_lookup(req, dentry, err);
425 		if (IS_ERR(dn))
426 			err = PTR_ERR(dn);
427 	} else {
428 		/* we were given a hashed negative dentry */
429 		dn = NULL;
430 	}
431 	if (err)
432 		goto out_req;
433 	if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
434 		/* make vfs retry on splice, ENOENT, or symlink */
435 		dout("atomic_open finish_no_open on dn %p\n", dn);
436 		err = finish_no_open(file, dn);
437 	} else {
438 		dout("atomic_open finish_open on dn %p\n", dn);
439 		if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
440 			ceph_init_inode_acls(d_inode(dentry), &acls);
441 			*opened |= FILE_CREATED;
442 		}
443 		err = finish_open(file, dentry, ceph_open, opened);
444 	}
445 out_req:
446 	if (!req->r_err && req->r_target_inode)
447 		ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode);
448 	ceph_mdsc_put_request(req);
449 out_acl:
450 	ceph_release_acls_info(&acls);
451 	dout("atomic_open result=%d\n", err);
452 	return err;
453 }
454 
455 int ceph_release(struct inode *inode, struct file *file)
456 {
457 	struct ceph_inode_info *ci = ceph_inode(inode);
458 	struct ceph_file_info *cf = file->private_data;
459 
460 	dout("release inode %p file %p\n", inode, file);
461 	ceph_put_fmode(ci, cf->fmode);
462 	if (cf->last_readdir)
463 		ceph_mdsc_put_request(cf->last_readdir);
464 	kfree(cf->last_name);
465 	kfree(cf->dir_info);
466 	kmem_cache_free(ceph_file_cachep, cf);
467 
468 	/* wake up anyone waiting for caps on this inode */
469 	wake_up_all(&ci->i_cap_wq);
470 	return 0;
471 }
472 
473 enum {
474 	HAVE_RETRIED = 1,
475 	CHECK_EOF =    2,
476 	READ_INLINE =  3,
477 };
478 
479 /*
480  * Read a range of bytes striped over one or more objects.  Iterate over
481  * objects we stripe over.  (That's not atomic, but good enough for now.)
482  *
483  * If we get a short result from the OSD, check against i_size; we need to
484  * only return a short read to the caller if we hit EOF.
485  */
486 static int striped_read(struct inode *inode,
487 			u64 pos, u64 len,
488 			struct page **pages, int num_pages,
489 			int page_align, int *checkeof)
490 {
491 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
492 	struct ceph_inode_info *ci = ceph_inode(inode);
493 	u64 this_len;
494 	loff_t i_size;
495 	int page_idx;
496 	int ret, read = 0;
497 	bool hit_stripe, was_short;
498 
499 	/*
500 	 * we may need to do multiple reads.  not atomic, unfortunately.
501 	 */
502 more:
503 	this_len = len;
504 	page_idx = (page_align + read) >> PAGE_SHIFT;
505 	ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
506 				  &ci->i_layout, pos, &this_len,
507 				  ci->i_truncate_seq, ci->i_truncate_size,
508 				  pages + page_idx, num_pages - page_idx,
509 				  ((page_align + read) & ~PAGE_MASK));
510 	if (ret == -ENOENT)
511 		ret = 0;
512 	hit_stripe = this_len < len;
513 	was_short = ret >= 0 && ret < this_len;
514 	dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, len, read,
515 	     ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
516 
517 	i_size = i_size_read(inode);
518 	if (ret >= 0) {
519 		if (was_short && (pos + ret < i_size)) {
520 			int zlen = min(this_len - ret, i_size - pos - ret);
521 			int zoff = page_align + read + ret;
522 			dout(" zero gap %llu to %llu\n",
523 			     pos + ret, pos + ret + zlen);
524 			ceph_zero_page_vector_range(zoff, zlen, pages);
525 			ret += zlen;
526 		}
527 
528 		read += ret;
529 		pos += ret;
530 		len -= ret;
531 
532 		/* hit stripe and need continue*/
533 		if (len && hit_stripe && pos < i_size)
534 			goto more;
535 	}
536 
537 	if (read > 0) {
538 		ret = read;
539 		/* did we bounce off eof? */
540 		if (pos + len > i_size)
541 			*checkeof = CHECK_EOF;
542 	}
543 
544 	dout("striped_read returns %d\n", ret);
545 	return ret;
546 }
547 
548 /*
549  * Completely synchronous read and write methods.  Direct from __user
550  * buffer to osd, or directly to user pages (if O_DIRECT).
551  *
552  * If the read spans object boundary, just do multiple reads.
553  */
554 static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
555 			      int *checkeof)
556 {
557 	struct file *file = iocb->ki_filp;
558 	struct inode *inode = file_inode(file);
559 	struct page **pages;
560 	u64 off = iocb->ki_pos;
561 	int num_pages;
562 	ssize_t ret;
563 	size_t len = iov_iter_count(to);
564 
565 	dout("sync_read on file %p %llu~%u %s\n", file, off, (unsigned)len,
566 	     (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
567 
568 	if (!len)
569 		return 0;
570 	/*
571 	 * flush any page cache pages in this range.  this
572 	 * will make concurrent normal and sync io slow,
573 	 * but it will at least behave sensibly when they are
574 	 * in sequence.
575 	 */
576 	ret = filemap_write_and_wait_range(inode->i_mapping, off,
577 						off + len);
578 	if (ret < 0)
579 		return ret;
580 
581 	if (unlikely(to->type & ITER_PIPE)) {
582 		size_t page_off;
583 		ret = iov_iter_get_pages_alloc(to, &pages, len,
584 					       &page_off);
585 		if (ret <= 0)
586 			return -ENOMEM;
587 		num_pages = DIV_ROUND_UP(ret + page_off, PAGE_SIZE);
588 
589 		ret = striped_read(inode, off, ret, pages, num_pages,
590 				   page_off, checkeof);
591 		if (ret > 0) {
592 			iov_iter_advance(to, ret);
593 			off += ret;
594 		} else {
595 			iov_iter_advance(to, 0);
596 		}
597 		ceph_put_page_vector(pages, num_pages, false);
598 	} else {
599 		num_pages = calc_pages_for(off, len);
600 		pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
601 		if (IS_ERR(pages))
602 			return PTR_ERR(pages);
603 
604 		ret = striped_read(inode, off, len, pages, num_pages,
605 				   (off & ~PAGE_MASK), checkeof);
606 		if (ret > 0) {
607 			int l, k = 0;
608 			size_t left = ret;
609 
610 			while (left) {
611 				size_t page_off = off & ~PAGE_MASK;
612 				size_t copy = min_t(size_t, left,
613 						    PAGE_SIZE - page_off);
614 				l = copy_page_to_iter(pages[k++], page_off,
615 						      copy, to);
616 				off += l;
617 				left -= l;
618 				if (l < copy)
619 					break;
620 			}
621 		}
622 		ceph_release_page_vector(pages, num_pages);
623 	}
624 
625 	if (off > iocb->ki_pos) {
626 		ret = off - iocb->ki_pos;
627 		iocb->ki_pos = off;
628 	}
629 
630 	dout("sync_read result %zd\n", ret);
631 	return ret;
632 }
633 
634 struct ceph_aio_request {
635 	struct kiocb *iocb;
636 	size_t total_len;
637 	int write;
638 	int error;
639 	struct list_head osd_reqs;
640 	unsigned num_reqs;
641 	atomic_t pending_reqs;
642 	struct timespec mtime;
643 	struct ceph_cap_flush *prealloc_cf;
644 };
645 
646 struct ceph_aio_work {
647 	struct work_struct work;
648 	struct ceph_osd_request *req;
649 };
650 
651 static void ceph_aio_retry_work(struct work_struct *work);
652 
653 static void ceph_aio_complete(struct inode *inode,
654 			      struct ceph_aio_request *aio_req)
655 {
656 	struct ceph_inode_info *ci = ceph_inode(inode);
657 	int ret;
658 
659 	if (!atomic_dec_and_test(&aio_req->pending_reqs))
660 		return;
661 
662 	ret = aio_req->error;
663 	if (!ret)
664 		ret = aio_req->total_len;
665 
666 	dout("ceph_aio_complete %p rc %d\n", inode, ret);
667 
668 	if (ret >= 0 && aio_req->write) {
669 		int dirty;
670 
671 		loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
672 		if (endoff > i_size_read(inode)) {
673 			if (ceph_inode_set_size(inode, endoff))
674 				ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
675 		}
676 
677 		spin_lock(&ci->i_ceph_lock);
678 		ci->i_inline_version = CEPH_INLINE_NONE;
679 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
680 					       &aio_req->prealloc_cf);
681 		spin_unlock(&ci->i_ceph_lock);
682 		if (dirty)
683 			__mark_inode_dirty(inode, dirty);
684 
685 	}
686 
687 	ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
688 						CEPH_CAP_FILE_RD));
689 
690 	aio_req->iocb->ki_complete(aio_req->iocb, ret, 0);
691 
692 	ceph_free_cap_flush(aio_req->prealloc_cf);
693 	kfree(aio_req);
694 }
695 
696 static void ceph_aio_complete_req(struct ceph_osd_request *req)
697 {
698 	int rc = req->r_result;
699 	struct inode *inode = req->r_inode;
700 	struct ceph_aio_request *aio_req = req->r_priv;
701 	struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
702 	int num_pages = calc_pages_for((u64)osd_data->alignment,
703 				       osd_data->length);
704 
705 	dout("ceph_aio_complete_req %p rc %d bytes %llu\n",
706 	     inode, rc, osd_data->length);
707 
708 	if (rc == -EOLDSNAPC) {
709 		struct ceph_aio_work *aio_work;
710 		BUG_ON(!aio_req->write);
711 
712 		aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
713 		if (aio_work) {
714 			INIT_WORK(&aio_work->work, ceph_aio_retry_work);
715 			aio_work->req = req;
716 			queue_work(ceph_inode_to_client(inode)->wb_wq,
717 				   &aio_work->work);
718 			return;
719 		}
720 		rc = -ENOMEM;
721 	} else if (!aio_req->write) {
722 		if (rc == -ENOENT)
723 			rc = 0;
724 		if (rc >= 0 && osd_data->length > rc) {
725 			int zoff = osd_data->alignment + rc;
726 			int zlen = osd_data->length - rc;
727 			/*
728 			 * If read is satisfied by single OSD request,
729 			 * it can pass EOF. Otherwise read is within
730 			 * i_size.
731 			 */
732 			if (aio_req->num_reqs == 1) {
733 				loff_t i_size = i_size_read(inode);
734 				loff_t endoff = aio_req->iocb->ki_pos + rc;
735 				if (endoff < i_size)
736 					zlen = min_t(size_t, zlen,
737 						     i_size - endoff);
738 				aio_req->total_len = rc + zlen;
739 			}
740 
741 			if (zlen > 0)
742 				ceph_zero_page_vector_range(zoff, zlen,
743 							    osd_data->pages);
744 		}
745 	}
746 
747 	ceph_put_page_vector(osd_data->pages, num_pages, !aio_req->write);
748 	ceph_osdc_put_request(req);
749 
750 	if (rc < 0)
751 		cmpxchg(&aio_req->error, 0, rc);
752 
753 	ceph_aio_complete(inode, aio_req);
754 	return;
755 }
756 
757 static void ceph_aio_retry_work(struct work_struct *work)
758 {
759 	struct ceph_aio_work *aio_work =
760 		container_of(work, struct ceph_aio_work, work);
761 	struct ceph_osd_request *orig_req = aio_work->req;
762 	struct ceph_aio_request *aio_req = orig_req->r_priv;
763 	struct inode *inode = orig_req->r_inode;
764 	struct ceph_inode_info *ci = ceph_inode(inode);
765 	struct ceph_snap_context *snapc;
766 	struct ceph_osd_request *req;
767 	int ret;
768 
769 	spin_lock(&ci->i_ceph_lock);
770 	if (__ceph_have_pending_cap_snap(ci)) {
771 		struct ceph_cap_snap *capsnap =
772 			list_last_entry(&ci->i_cap_snaps,
773 					struct ceph_cap_snap,
774 					ci_item);
775 		snapc = ceph_get_snap_context(capsnap->context);
776 	} else {
777 		BUG_ON(!ci->i_head_snapc);
778 		snapc = ceph_get_snap_context(ci->i_head_snapc);
779 	}
780 	spin_unlock(&ci->i_ceph_lock);
781 
782 	req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 2,
783 			false, GFP_NOFS);
784 	if (!req) {
785 		ret = -ENOMEM;
786 		req = orig_req;
787 		goto out;
788 	}
789 
790 	req->r_flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
791 	ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
792 	ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
793 
794 	ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
795 	if (ret) {
796 		ceph_osdc_put_request(req);
797 		req = orig_req;
798 		goto out;
799 	}
800 
801 	req->r_ops[0] = orig_req->r_ops[0];
802 
803 	req->r_mtime = aio_req->mtime;
804 	req->r_data_offset = req->r_ops[0].extent.offset;
805 
806 	ceph_osdc_put_request(orig_req);
807 
808 	req->r_callback = ceph_aio_complete_req;
809 	req->r_inode = inode;
810 	req->r_priv = aio_req;
811 	req->r_abort_on_full = true;
812 
813 	ret = ceph_osdc_start_request(req->r_osdc, req, false);
814 out:
815 	if (ret < 0) {
816 		req->r_result = ret;
817 		ceph_aio_complete_req(req);
818 	}
819 
820 	ceph_put_snap_context(snapc);
821 	kfree(aio_work);
822 }
823 
824 static ssize_t
825 ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
826 		       struct ceph_snap_context *snapc,
827 		       struct ceph_cap_flush **pcf)
828 {
829 	struct file *file = iocb->ki_filp;
830 	struct inode *inode = file_inode(file);
831 	struct ceph_inode_info *ci = ceph_inode(inode);
832 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
833 	struct ceph_vino vino;
834 	struct ceph_osd_request *req;
835 	struct page **pages;
836 	struct ceph_aio_request *aio_req = NULL;
837 	int num_pages = 0;
838 	int flags;
839 	int ret;
840 	struct timespec mtime = current_time(inode);
841 	size_t count = iov_iter_count(iter);
842 	loff_t pos = iocb->ki_pos;
843 	bool write = iov_iter_rw(iter) == WRITE;
844 
845 	if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
846 		return -EROFS;
847 
848 	dout("sync_direct_%s on file %p %lld~%u snapc %p seq %lld\n",
849 	     (write ? "write" : "read"), file, pos, (unsigned)count,
850 	     snapc, snapc->seq);
851 
852 	ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
853 	if (ret < 0)
854 		return ret;
855 
856 	if (write) {
857 		int ret2 = invalidate_inode_pages2_range(inode->i_mapping,
858 					pos >> PAGE_SHIFT,
859 					(pos + count) >> PAGE_SHIFT);
860 		if (ret2 < 0)
861 			dout("invalidate_inode_pages2_range returned %d\n", ret2);
862 
863 		flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
864 	} else {
865 		flags = CEPH_OSD_FLAG_READ;
866 	}
867 
868 	while (iov_iter_count(iter) > 0) {
869 		u64 size = dio_get_pagev_size(iter);
870 		size_t start = 0;
871 		ssize_t len;
872 
873 		vino = ceph_vino(inode);
874 		req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
875 					    vino, pos, &size, 0,
876 					    1,
877 					    write ? CEPH_OSD_OP_WRITE :
878 						    CEPH_OSD_OP_READ,
879 					    flags, snapc,
880 					    ci->i_truncate_seq,
881 					    ci->i_truncate_size,
882 					    false);
883 		if (IS_ERR(req)) {
884 			ret = PTR_ERR(req);
885 			break;
886 		}
887 
888 		if (write)
889 			size = min_t(u64, size, fsc->mount_options->wsize);
890 		else
891 			size = min_t(u64, size, fsc->mount_options->rsize);
892 
893 		len = size;
894 		pages = dio_get_pages_alloc(iter, len, &start, &num_pages);
895 		if (IS_ERR(pages)) {
896 			ceph_osdc_put_request(req);
897 			ret = PTR_ERR(pages);
898 			break;
899 		}
900 
901 		/*
902 		 * To simplify error handling, allow AIO when IO within i_size
903 		 * or IO can be satisfied by single OSD request.
904 		 */
905 		if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
906 		    (len == count || pos + count <= i_size_read(inode))) {
907 			aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
908 			if (aio_req) {
909 				aio_req->iocb = iocb;
910 				aio_req->write = write;
911 				INIT_LIST_HEAD(&aio_req->osd_reqs);
912 				if (write) {
913 					aio_req->mtime = mtime;
914 					swap(aio_req->prealloc_cf, *pcf);
915 				}
916 			}
917 			/* ignore error */
918 		}
919 
920 		if (write) {
921 			/*
922 			 * throw out any page cache pages in this range. this
923 			 * may block.
924 			 */
925 			truncate_inode_pages_range(inode->i_mapping, pos,
926 					(pos+len) | (PAGE_SIZE - 1));
927 
928 			req->r_mtime = mtime;
929 		}
930 
931 		osd_req_op_extent_osd_data_pages(req, 0, pages, len, start,
932 						 false, false);
933 
934 		if (aio_req) {
935 			aio_req->total_len += len;
936 			aio_req->num_reqs++;
937 			atomic_inc(&aio_req->pending_reqs);
938 
939 			req->r_callback = ceph_aio_complete_req;
940 			req->r_inode = inode;
941 			req->r_priv = aio_req;
942 			list_add_tail(&req->r_unsafe_item, &aio_req->osd_reqs);
943 
944 			pos += len;
945 			iov_iter_advance(iter, len);
946 			continue;
947 		}
948 
949 		ret = ceph_osdc_start_request(req->r_osdc, req, false);
950 		if (!ret)
951 			ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
952 
953 		size = i_size_read(inode);
954 		if (!write) {
955 			if (ret == -ENOENT)
956 				ret = 0;
957 			if (ret >= 0 && ret < len && pos + ret < size) {
958 				int zlen = min_t(size_t, len - ret,
959 						 size - pos - ret);
960 				ceph_zero_page_vector_range(start + ret, zlen,
961 							    pages);
962 				ret += zlen;
963 			}
964 			if (ret >= 0)
965 				len = ret;
966 		}
967 
968 		ceph_put_page_vector(pages, num_pages, !write);
969 
970 		ceph_osdc_put_request(req);
971 		if (ret < 0)
972 			break;
973 
974 		pos += len;
975 		iov_iter_advance(iter, len);
976 
977 		if (!write && pos >= size)
978 			break;
979 
980 		if (write && pos > size) {
981 			if (ceph_inode_set_size(inode, pos))
982 				ceph_check_caps(ceph_inode(inode),
983 						CHECK_CAPS_AUTHONLY,
984 						NULL);
985 		}
986 	}
987 
988 	if (aio_req) {
989 		LIST_HEAD(osd_reqs);
990 
991 		if (aio_req->num_reqs == 0) {
992 			kfree(aio_req);
993 			return ret;
994 		}
995 
996 		ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
997 					      CEPH_CAP_FILE_RD);
998 
999 		list_splice(&aio_req->osd_reqs, &osd_reqs);
1000 		while (!list_empty(&osd_reqs)) {
1001 			req = list_first_entry(&osd_reqs,
1002 					       struct ceph_osd_request,
1003 					       r_unsafe_item);
1004 			list_del_init(&req->r_unsafe_item);
1005 			if (ret >= 0)
1006 				ret = ceph_osdc_start_request(req->r_osdc,
1007 							      req, false);
1008 			if (ret < 0) {
1009 				req->r_result = ret;
1010 				ceph_aio_complete_req(req);
1011 			}
1012 		}
1013 		return -EIOCBQUEUED;
1014 	}
1015 
1016 	if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
1017 		ret = pos - iocb->ki_pos;
1018 		iocb->ki_pos = pos;
1019 	}
1020 	return ret;
1021 }
1022 
1023 /*
1024  * Synchronous write, straight from __user pointer or user pages.
1025  *
1026  * If write spans object boundary, just do multiple writes.  (For a
1027  * correct atomic write, we should e.g. take write locks on all
1028  * objects, rollback on failure, etc.)
1029  */
1030 static ssize_t
1031 ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
1032 		struct ceph_snap_context *snapc)
1033 {
1034 	struct file *file = iocb->ki_filp;
1035 	struct inode *inode = file_inode(file);
1036 	struct ceph_inode_info *ci = ceph_inode(inode);
1037 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1038 	struct ceph_vino vino;
1039 	struct ceph_osd_request *req;
1040 	struct page **pages;
1041 	u64 len;
1042 	int num_pages;
1043 	int written = 0;
1044 	int flags;
1045 	int ret;
1046 	bool check_caps = false;
1047 	struct timespec mtime = current_time(inode);
1048 	size_t count = iov_iter_count(from);
1049 
1050 	if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
1051 		return -EROFS;
1052 
1053 	dout("sync_write on file %p %lld~%u snapc %p seq %lld\n",
1054 	     file, pos, (unsigned)count, snapc, snapc->seq);
1055 
1056 	ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
1057 	if (ret < 0)
1058 		return ret;
1059 
1060 	ret = invalidate_inode_pages2_range(inode->i_mapping,
1061 					    pos >> PAGE_SHIFT,
1062 					    (pos + count) >> PAGE_SHIFT);
1063 	if (ret < 0)
1064 		dout("invalidate_inode_pages2_range returned %d\n", ret);
1065 
1066 	flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
1067 
1068 	while ((len = iov_iter_count(from)) > 0) {
1069 		size_t left;
1070 		int n;
1071 
1072 		vino = ceph_vino(inode);
1073 		req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1074 					    vino, pos, &len, 0, 1,
1075 					    CEPH_OSD_OP_WRITE, flags, snapc,
1076 					    ci->i_truncate_seq,
1077 					    ci->i_truncate_size,
1078 					    false);
1079 		if (IS_ERR(req)) {
1080 			ret = PTR_ERR(req);
1081 			break;
1082 		}
1083 
1084 		/*
1085 		 * write from beginning of first page,
1086 		 * regardless of io alignment
1087 		 */
1088 		num_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1089 
1090 		pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1091 		if (IS_ERR(pages)) {
1092 			ret = PTR_ERR(pages);
1093 			goto out;
1094 		}
1095 
1096 		left = len;
1097 		for (n = 0; n < num_pages; n++) {
1098 			size_t plen = min_t(size_t, left, PAGE_SIZE);
1099 			ret = copy_page_from_iter(pages[n], 0, plen, from);
1100 			if (ret != plen) {
1101 				ret = -EFAULT;
1102 				break;
1103 			}
1104 			left -= ret;
1105 		}
1106 
1107 		if (ret < 0) {
1108 			ceph_release_page_vector(pages, num_pages);
1109 			goto out;
1110 		}
1111 
1112 		req->r_inode = inode;
1113 
1114 		osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
1115 						false, true);
1116 
1117 		req->r_mtime = mtime;
1118 		ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1119 		if (!ret)
1120 			ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1121 
1122 out:
1123 		ceph_osdc_put_request(req);
1124 		if (ret != 0) {
1125 			ceph_set_error_write(ci);
1126 			break;
1127 		}
1128 
1129 		ceph_clear_error_write(ci);
1130 		pos += len;
1131 		written += len;
1132 		if (pos > i_size_read(inode)) {
1133 			check_caps = ceph_inode_set_size(inode, pos);
1134 			if (check_caps)
1135 				ceph_check_caps(ceph_inode(inode),
1136 						CHECK_CAPS_AUTHONLY,
1137 						NULL);
1138 		}
1139 
1140 	}
1141 
1142 	if (ret != -EOLDSNAPC && written > 0) {
1143 		ret = written;
1144 		iocb->ki_pos = pos;
1145 	}
1146 	return ret;
1147 }
1148 
1149 /*
1150  * Wrap generic_file_aio_read with checks for cap bits on the inode.
1151  * Atomically grab references, so that those bits are not released
1152  * back to the MDS mid-read.
1153  *
1154  * Hmm, the sync read case isn't actually async... should it be?
1155  */
1156 static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
1157 {
1158 	struct file *filp = iocb->ki_filp;
1159 	struct ceph_file_info *fi = filp->private_data;
1160 	size_t len = iov_iter_count(to);
1161 	struct inode *inode = file_inode(filp);
1162 	struct ceph_inode_info *ci = ceph_inode(inode);
1163 	struct page *pinned_page = NULL;
1164 	ssize_t ret;
1165 	int want, got = 0;
1166 	int retry_op = 0, read = 0;
1167 
1168 again:
1169 	dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
1170 	     inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
1171 
1172 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
1173 		want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1174 	else
1175 		want = CEPH_CAP_FILE_CACHE;
1176 	ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
1177 	if (ret < 0)
1178 		return ret;
1179 
1180 	if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
1181 	    (iocb->ki_flags & IOCB_DIRECT) ||
1182 	    (fi->flags & CEPH_F_SYNC)) {
1183 
1184 		dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
1185 		     inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
1186 		     ceph_cap_string(got));
1187 
1188 		if (ci->i_inline_version == CEPH_INLINE_NONE) {
1189 			if (!retry_op && (iocb->ki_flags & IOCB_DIRECT)) {
1190 				ret = ceph_direct_read_write(iocb, to,
1191 							     NULL, NULL);
1192 				if (ret >= 0 && ret < len)
1193 					retry_op = CHECK_EOF;
1194 			} else {
1195 				ret = ceph_sync_read(iocb, to, &retry_op);
1196 			}
1197 		} else {
1198 			retry_op = READ_INLINE;
1199 		}
1200 	} else {
1201 		dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
1202 		     inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
1203 		     ceph_cap_string(got));
1204 		current->journal_info = filp;
1205 		ret = generic_file_read_iter(iocb, to);
1206 		current->journal_info = NULL;
1207 	}
1208 	dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
1209 	     inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
1210 	if (pinned_page) {
1211 		put_page(pinned_page);
1212 		pinned_page = NULL;
1213 	}
1214 	ceph_put_cap_refs(ci, got);
1215 	if (retry_op > HAVE_RETRIED && ret >= 0) {
1216 		int statret;
1217 		struct page *page = NULL;
1218 		loff_t i_size;
1219 		if (retry_op == READ_INLINE) {
1220 			page = __page_cache_alloc(GFP_KERNEL);
1221 			if (!page)
1222 				return -ENOMEM;
1223 		}
1224 
1225 		statret = __ceph_do_getattr(inode, page,
1226 					    CEPH_STAT_CAP_INLINE_DATA, !!page);
1227 		if (statret < 0) {
1228 			if (page)
1229 				__free_page(page);
1230 			if (statret == -ENODATA) {
1231 				BUG_ON(retry_op != READ_INLINE);
1232 				goto again;
1233 			}
1234 			return statret;
1235 		}
1236 
1237 		i_size = i_size_read(inode);
1238 		if (retry_op == READ_INLINE) {
1239 			BUG_ON(ret > 0 || read > 0);
1240 			if (iocb->ki_pos < i_size &&
1241 			    iocb->ki_pos < PAGE_SIZE) {
1242 				loff_t end = min_t(loff_t, i_size,
1243 						   iocb->ki_pos + len);
1244 				end = min_t(loff_t, end, PAGE_SIZE);
1245 				if (statret < end)
1246 					zero_user_segment(page, statret, end);
1247 				ret = copy_page_to_iter(page,
1248 						iocb->ki_pos & ~PAGE_MASK,
1249 						end - iocb->ki_pos, to);
1250 				iocb->ki_pos += ret;
1251 				read += ret;
1252 			}
1253 			if (iocb->ki_pos < i_size && read < len) {
1254 				size_t zlen = min_t(size_t, len - read,
1255 						    i_size - iocb->ki_pos);
1256 				ret = iov_iter_zero(zlen, to);
1257 				iocb->ki_pos += ret;
1258 				read += ret;
1259 			}
1260 			__free_pages(page, 0);
1261 			return read;
1262 		}
1263 
1264 		/* hit EOF or hole? */
1265 		if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
1266 		    ret < len) {
1267 			dout("sync_read hit hole, ppos %lld < size %lld"
1268 			     ", reading more\n", iocb->ki_pos, i_size);
1269 
1270 			read += ret;
1271 			len -= ret;
1272 			retry_op = HAVE_RETRIED;
1273 			goto again;
1274 		}
1275 	}
1276 
1277 	if (ret >= 0)
1278 		ret += read;
1279 
1280 	return ret;
1281 }
1282 
1283 /*
1284  * Take cap references to avoid releasing caps to MDS mid-write.
1285  *
1286  * If we are synchronous, and write with an old snap context, the OSD
1287  * may return EOLDSNAPC.  In that case, retry the write.. _after_
1288  * dropping our cap refs and allowing the pending snap to logically
1289  * complete _before_ this write occurs.
1290  *
1291  * If we are near ENOSPC, write synchronously.
1292  */
1293 static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
1294 {
1295 	struct file *file = iocb->ki_filp;
1296 	struct ceph_file_info *fi = file->private_data;
1297 	struct inode *inode = file_inode(file);
1298 	struct ceph_inode_info *ci = ceph_inode(inode);
1299 	struct ceph_osd_client *osdc =
1300 		&ceph_sb_to_client(inode->i_sb)->client->osdc;
1301 	struct ceph_cap_flush *prealloc_cf;
1302 	ssize_t count, written = 0;
1303 	int err, want, got;
1304 	loff_t pos;
1305 
1306 	if (ceph_snap(inode) != CEPH_NOSNAP)
1307 		return -EROFS;
1308 
1309 	prealloc_cf = ceph_alloc_cap_flush();
1310 	if (!prealloc_cf)
1311 		return -ENOMEM;
1312 
1313 retry_snap:
1314 	inode_lock(inode);
1315 
1316 	/* We can write back this queue in page reclaim */
1317 	current->backing_dev_info = inode_to_bdi(inode);
1318 
1319 	if (iocb->ki_flags & IOCB_APPEND) {
1320 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
1321 		if (err < 0)
1322 			goto out;
1323 	}
1324 
1325 	err = generic_write_checks(iocb, from);
1326 	if (err <= 0)
1327 		goto out;
1328 
1329 	pos = iocb->ki_pos;
1330 	count = iov_iter_count(from);
1331 	err = file_remove_privs(file);
1332 	if (err)
1333 		goto out;
1334 
1335 	err = file_update_time(file);
1336 	if (err)
1337 		goto out;
1338 
1339 	if (ci->i_inline_version != CEPH_INLINE_NONE) {
1340 		err = ceph_uninline_data(file, NULL);
1341 		if (err < 0)
1342 			goto out;
1343 	}
1344 
1345 	/* FIXME: not complete since it doesn't account for being at quota */
1346 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL)) {
1347 		err = -ENOSPC;
1348 		goto out;
1349 	}
1350 
1351 	dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
1352 	     inode, ceph_vinop(inode), pos, count, i_size_read(inode));
1353 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
1354 		want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1355 	else
1356 		want = CEPH_CAP_FILE_BUFFER;
1357 	got = 0;
1358 	err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, pos + count,
1359 			    &got, NULL);
1360 	if (err < 0)
1361 		goto out;
1362 
1363 	dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
1364 	     inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
1365 
1366 	if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
1367 	    (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC) ||
1368 	    (ci->i_ceph_flags & CEPH_I_ERROR_WRITE)) {
1369 		struct ceph_snap_context *snapc;
1370 		struct iov_iter data;
1371 		inode_unlock(inode);
1372 
1373 		spin_lock(&ci->i_ceph_lock);
1374 		if (__ceph_have_pending_cap_snap(ci)) {
1375 			struct ceph_cap_snap *capsnap =
1376 					list_last_entry(&ci->i_cap_snaps,
1377 							struct ceph_cap_snap,
1378 							ci_item);
1379 			snapc = ceph_get_snap_context(capsnap->context);
1380 		} else {
1381 			BUG_ON(!ci->i_head_snapc);
1382 			snapc = ceph_get_snap_context(ci->i_head_snapc);
1383 		}
1384 		spin_unlock(&ci->i_ceph_lock);
1385 
1386 		/* we might need to revert back to that point */
1387 		data = *from;
1388 		if (iocb->ki_flags & IOCB_DIRECT)
1389 			written = ceph_direct_read_write(iocb, &data, snapc,
1390 							 &prealloc_cf);
1391 		else
1392 			written = ceph_sync_write(iocb, &data, pos, snapc);
1393 		if (written > 0)
1394 			iov_iter_advance(from, written);
1395 		ceph_put_snap_context(snapc);
1396 	} else {
1397 		/*
1398 		 * No need to acquire the i_truncate_mutex. Because
1399 		 * the MDS revokes Fwb caps before sending truncate
1400 		 * message to us. We can't get Fwb cap while there
1401 		 * are pending vmtruncate. So write and vmtruncate
1402 		 * can not run at the same time
1403 		 */
1404 		written = generic_perform_write(file, from, pos);
1405 		if (likely(written >= 0))
1406 			iocb->ki_pos = pos + written;
1407 		inode_unlock(inode);
1408 	}
1409 
1410 	if (written >= 0) {
1411 		int dirty;
1412 		spin_lock(&ci->i_ceph_lock);
1413 		ci->i_inline_version = CEPH_INLINE_NONE;
1414 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1415 					       &prealloc_cf);
1416 		spin_unlock(&ci->i_ceph_lock);
1417 		if (dirty)
1418 			__mark_inode_dirty(inode, dirty);
1419 	}
1420 
1421 	dout("aio_write %p %llx.%llx %llu~%u  dropping cap refs on %s\n",
1422 	     inode, ceph_vinop(inode), pos, (unsigned)count,
1423 	     ceph_cap_string(got));
1424 	ceph_put_cap_refs(ci, got);
1425 
1426 	if (written == -EOLDSNAPC) {
1427 		dout("aio_write %p %llx.%llx %llu~%u" "got EOLDSNAPC, retrying\n",
1428 		     inode, ceph_vinop(inode), pos, (unsigned)count);
1429 		goto retry_snap;
1430 	}
1431 
1432 	if (written >= 0) {
1433 		if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_NEARFULL))
1434 			iocb->ki_flags |= IOCB_DSYNC;
1435 		written = generic_write_sync(iocb, written);
1436 	}
1437 
1438 	goto out_unlocked;
1439 
1440 out:
1441 	inode_unlock(inode);
1442 out_unlocked:
1443 	ceph_free_cap_flush(prealloc_cf);
1444 	current->backing_dev_info = NULL;
1445 	return written ? written : err;
1446 }
1447 
1448 /*
1449  * llseek.  be sure to verify file size on SEEK_END.
1450  */
1451 static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
1452 {
1453 	struct inode *inode = file->f_mapping->host;
1454 	loff_t i_size;
1455 	loff_t ret;
1456 
1457 	inode_lock(inode);
1458 
1459 	if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
1460 		ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
1461 		if (ret < 0)
1462 			goto out;
1463 	}
1464 
1465 	i_size = i_size_read(inode);
1466 	switch (whence) {
1467 	case SEEK_END:
1468 		offset += i_size;
1469 		break;
1470 	case SEEK_CUR:
1471 		/*
1472 		 * Here we special-case the lseek(fd, 0, SEEK_CUR)
1473 		 * position-querying operation.  Avoid rewriting the "same"
1474 		 * f_pos value back to the file because a concurrent read(),
1475 		 * write() or lseek() might have altered it
1476 		 */
1477 		if (offset == 0) {
1478 			ret = file->f_pos;
1479 			goto out;
1480 		}
1481 		offset += file->f_pos;
1482 		break;
1483 	case SEEK_DATA:
1484 		if (offset < 0 || offset >= i_size) {
1485 			ret = -ENXIO;
1486 			goto out;
1487 		}
1488 		break;
1489 	case SEEK_HOLE:
1490 		if (offset < 0 || offset >= i_size) {
1491 			ret = -ENXIO;
1492 			goto out;
1493 		}
1494 		offset = i_size;
1495 		break;
1496 	}
1497 
1498 	ret = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1499 
1500 out:
1501 	inode_unlock(inode);
1502 	return ret;
1503 }
1504 
1505 static inline void ceph_zero_partial_page(
1506 	struct inode *inode, loff_t offset, unsigned size)
1507 {
1508 	struct page *page;
1509 	pgoff_t index = offset >> PAGE_SHIFT;
1510 
1511 	page = find_lock_page(inode->i_mapping, index);
1512 	if (page) {
1513 		wait_on_page_writeback(page);
1514 		zero_user(page, offset & (PAGE_SIZE - 1), size);
1515 		unlock_page(page);
1516 		put_page(page);
1517 	}
1518 }
1519 
1520 static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
1521 				      loff_t length)
1522 {
1523 	loff_t nearly = round_up(offset, PAGE_SIZE);
1524 	if (offset < nearly) {
1525 		loff_t size = nearly - offset;
1526 		if (length < size)
1527 			size = length;
1528 		ceph_zero_partial_page(inode, offset, size);
1529 		offset += size;
1530 		length -= size;
1531 	}
1532 	if (length >= PAGE_SIZE) {
1533 		loff_t size = round_down(length, PAGE_SIZE);
1534 		truncate_pagecache_range(inode, offset, offset + size - 1);
1535 		offset += size;
1536 		length -= size;
1537 	}
1538 	if (length)
1539 		ceph_zero_partial_page(inode, offset, length);
1540 }
1541 
1542 static int ceph_zero_partial_object(struct inode *inode,
1543 				    loff_t offset, loff_t *length)
1544 {
1545 	struct ceph_inode_info *ci = ceph_inode(inode);
1546 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1547 	struct ceph_osd_request *req;
1548 	int ret = 0;
1549 	loff_t zero = 0;
1550 	int op;
1551 
1552 	if (!length) {
1553 		op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
1554 		length = &zero;
1555 	} else {
1556 		op = CEPH_OSD_OP_ZERO;
1557 	}
1558 
1559 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1560 					ceph_vino(inode),
1561 					offset, length,
1562 					0, 1, op,
1563 					CEPH_OSD_FLAG_WRITE,
1564 					NULL, 0, 0, false);
1565 	if (IS_ERR(req)) {
1566 		ret = PTR_ERR(req);
1567 		goto out;
1568 	}
1569 
1570 	req->r_mtime = inode->i_mtime;
1571 	ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1572 	if (!ret) {
1573 		ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1574 		if (ret == -ENOENT)
1575 			ret = 0;
1576 	}
1577 	ceph_osdc_put_request(req);
1578 
1579 out:
1580 	return ret;
1581 }
1582 
1583 static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
1584 {
1585 	int ret = 0;
1586 	struct ceph_inode_info *ci = ceph_inode(inode);
1587 	s32 stripe_unit = ci->i_layout.stripe_unit;
1588 	s32 stripe_count = ci->i_layout.stripe_count;
1589 	s32 object_size = ci->i_layout.object_size;
1590 	u64 object_set_size = object_size * stripe_count;
1591 	u64 nearly, t;
1592 
1593 	/* round offset up to next period boundary */
1594 	nearly = offset + object_set_size - 1;
1595 	t = nearly;
1596 	nearly -= do_div(t, object_set_size);
1597 
1598 	while (length && offset < nearly) {
1599 		loff_t size = length;
1600 		ret = ceph_zero_partial_object(inode, offset, &size);
1601 		if (ret < 0)
1602 			return ret;
1603 		offset += size;
1604 		length -= size;
1605 	}
1606 	while (length >= object_set_size) {
1607 		int i;
1608 		loff_t pos = offset;
1609 		for (i = 0; i < stripe_count; ++i) {
1610 			ret = ceph_zero_partial_object(inode, pos, NULL);
1611 			if (ret < 0)
1612 				return ret;
1613 			pos += stripe_unit;
1614 		}
1615 		offset += object_set_size;
1616 		length -= object_set_size;
1617 	}
1618 	while (length) {
1619 		loff_t size = length;
1620 		ret = ceph_zero_partial_object(inode, offset, &size);
1621 		if (ret < 0)
1622 			return ret;
1623 		offset += size;
1624 		length -= size;
1625 	}
1626 	return ret;
1627 }
1628 
1629 static long ceph_fallocate(struct file *file, int mode,
1630 				loff_t offset, loff_t length)
1631 {
1632 	struct ceph_file_info *fi = file->private_data;
1633 	struct inode *inode = file_inode(file);
1634 	struct ceph_inode_info *ci = ceph_inode(inode);
1635 	struct ceph_osd_client *osdc =
1636 		&ceph_inode_to_client(inode)->client->osdc;
1637 	struct ceph_cap_flush *prealloc_cf;
1638 	int want, got = 0;
1639 	int dirty;
1640 	int ret = 0;
1641 	loff_t endoff = 0;
1642 	loff_t size;
1643 
1644 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
1645 		return -EOPNOTSUPP;
1646 
1647 	if (!S_ISREG(inode->i_mode))
1648 		return -EOPNOTSUPP;
1649 
1650 	prealloc_cf = ceph_alloc_cap_flush();
1651 	if (!prealloc_cf)
1652 		return -ENOMEM;
1653 
1654 	inode_lock(inode);
1655 
1656 	if (ceph_snap(inode) != CEPH_NOSNAP) {
1657 		ret = -EROFS;
1658 		goto unlock;
1659 	}
1660 
1661 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) &&
1662 	    !(mode & FALLOC_FL_PUNCH_HOLE)) {
1663 		ret = -ENOSPC;
1664 		goto unlock;
1665 	}
1666 
1667 	if (ci->i_inline_version != CEPH_INLINE_NONE) {
1668 		ret = ceph_uninline_data(file, NULL);
1669 		if (ret < 0)
1670 			goto unlock;
1671 	}
1672 
1673 	size = i_size_read(inode);
1674 	if (!(mode & FALLOC_FL_KEEP_SIZE)) {
1675 		endoff = offset + length;
1676 		ret = inode_newsize_ok(inode, endoff);
1677 		if (ret)
1678 			goto unlock;
1679 	}
1680 
1681 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
1682 		want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1683 	else
1684 		want = CEPH_CAP_FILE_BUFFER;
1685 
1686 	ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, endoff, &got, NULL);
1687 	if (ret < 0)
1688 		goto unlock;
1689 
1690 	if (mode & FALLOC_FL_PUNCH_HOLE) {
1691 		if (offset < size)
1692 			ceph_zero_pagecache_range(inode, offset, length);
1693 		ret = ceph_zero_objects(inode, offset, length);
1694 	} else if (endoff > size) {
1695 		truncate_pagecache_range(inode, size, -1);
1696 		if (ceph_inode_set_size(inode, endoff))
1697 			ceph_check_caps(ceph_inode(inode),
1698 				CHECK_CAPS_AUTHONLY, NULL);
1699 	}
1700 
1701 	if (!ret) {
1702 		spin_lock(&ci->i_ceph_lock);
1703 		ci->i_inline_version = CEPH_INLINE_NONE;
1704 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1705 					       &prealloc_cf);
1706 		spin_unlock(&ci->i_ceph_lock);
1707 		if (dirty)
1708 			__mark_inode_dirty(inode, dirty);
1709 	}
1710 
1711 	ceph_put_cap_refs(ci, got);
1712 unlock:
1713 	inode_unlock(inode);
1714 	ceph_free_cap_flush(prealloc_cf);
1715 	return ret;
1716 }
1717 
1718 const struct file_operations ceph_file_fops = {
1719 	.open = ceph_open,
1720 	.release = ceph_release,
1721 	.llseek = ceph_llseek,
1722 	.read_iter = ceph_read_iter,
1723 	.write_iter = ceph_write_iter,
1724 	.mmap = ceph_mmap,
1725 	.fsync = ceph_fsync,
1726 	.lock = ceph_lock,
1727 	.flock = ceph_flock,
1728 	.splice_read = generic_file_splice_read,
1729 	.splice_write = iter_file_splice_write,
1730 	.unlocked_ioctl = ceph_ioctl,
1731 	.compat_ioctl	= ceph_ioctl,
1732 	.fallocate	= ceph_fallocate,
1733 };
1734 
1735