1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
3503f82a9SLuis Henriques #include <linux/ceph/striper.h>
4124e68e7SSage Weil
53d14c5d2SYehuda Sadeh #include <linux/module.h>
6124e68e7SSage Weil #include <linux/sched.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
8124e68e7SSage Weil #include <linux/file.h>
95ef50c3bSSage Weil #include <linux/mount.h>
10124e68e7SSage Weil #include <linux/namei.h>
11124e68e7SSage Weil #include <linux/writeback.h>
12ad7a60deSLi Wang #include <linux/falloc.h>
135c308356SJeff Layton #include <linux/iversion.h>
1497e27aaaSXiubo Li #include <linux/ktime.h>
15124e68e7SSage Weil
16124e68e7SSage Weil #include "super.h"
17124e68e7SSage Weil #include "mds_client.h"
1899ccbd22SMilosz Tanski #include "cache.h"
19321fe13cSJeff Layton #include "io.h"
2097e27aaaSXiubo Li #include "metric.h"
21124e68e7SSage Weil
ceph_flags_sys2wire(u32 flags)22f775ff7dSAlexander Graf static __le32 ceph_flags_sys2wire(u32 flags)
23f775ff7dSAlexander Graf {
24f775ff7dSAlexander Graf u32 wire_flags = 0;
25f775ff7dSAlexander Graf
26f775ff7dSAlexander Graf switch (flags & O_ACCMODE) {
27f775ff7dSAlexander Graf case O_RDONLY:
28f775ff7dSAlexander Graf wire_flags |= CEPH_O_RDONLY;
29f775ff7dSAlexander Graf break;
30f775ff7dSAlexander Graf case O_WRONLY:
31f775ff7dSAlexander Graf wire_flags |= CEPH_O_WRONLY;
32f775ff7dSAlexander Graf break;
33f775ff7dSAlexander Graf case O_RDWR:
34f775ff7dSAlexander Graf wire_flags |= CEPH_O_RDWR;
35f775ff7dSAlexander Graf break;
36f775ff7dSAlexander Graf }
37f775ff7dSAlexander Graf
3851b10f3fSChengguang Xu flags &= ~O_ACCMODE;
3951b10f3fSChengguang Xu
40f775ff7dSAlexander Graf #define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; }
41f775ff7dSAlexander Graf
42f775ff7dSAlexander Graf ceph_sys2wire(O_CREAT);
43f775ff7dSAlexander Graf ceph_sys2wire(O_EXCL);
44f775ff7dSAlexander Graf ceph_sys2wire(O_TRUNC);
45f775ff7dSAlexander Graf ceph_sys2wire(O_DIRECTORY);
46f775ff7dSAlexander Graf ceph_sys2wire(O_NOFOLLOW);
47f775ff7dSAlexander Graf
48f775ff7dSAlexander Graf #undef ceph_sys2wire
49f775ff7dSAlexander Graf
50f775ff7dSAlexander Graf if (flags)
514c069a58SChengguang Xu dout("unused open flags: %x\n", flags);
52f775ff7dSAlexander Graf
53f775ff7dSAlexander Graf return cpu_to_le32(wire_flags);
54f775ff7dSAlexander Graf }
55f775ff7dSAlexander Graf
56124e68e7SSage Weil /*
57124e68e7SSage Weil * Ceph file operations
58124e68e7SSage Weil *
59124e68e7SSage Weil * Implement basic open/close functionality, and implement
60124e68e7SSage Weil * read/write.
61124e68e7SSage Weil *
62124e68e7SSage Weil * We implement three modes of file I/O:
63124e68e7SSage Weil * - buffered uses the generic_file_aio_{read,write} helpers
64124e68e7SSage Weil *
65124e68e7SSage Weil * - synchronous is used when there is multi-client read/write
66124e68e7SSage Weil * sharing, avoids the page cache, and synchronously waits for an
67124e68e7SSage Weil * ack from the OSD.
68124e68e7SSage Weil *
69124e68e7SSage Weil * - direct io takes the variant of the sync path that references
70124e68e7SSage Weil * user pages directly.
71124e68e7SSage Weil *
72124e68e7SSage Weil * fsync() flushes and waits on dirty pages, but just queues metadata
73124e68e7SSage Weil * for writeback: since the MDS can recover size and mtime there is no
74124e68e7SSage Weil * need to wait for MDS acknowledgement.
75124e68e7SSage Weil */
76124e68e7SSage Weil
77b5b98989SZhu, Caifeng /*
78fc218544SIlya Dryomov * How many pages to get in one call to iov_iter_get_pages(). This
79fc218544SIlya Dryomov * determines the size of the on-stack array used as a buffer.
80b5b98989SZhu, Caifeng */
81fc218544SIlya Dryomov #define ITER_GET_BVECS_PAGES 64
82b5b98989SZhu, Caifeng
__iter_get_bvecs(struct iov_iter * iter,size_t maxsize,struct bio_vec * bvecs)83fc218544SIlya Dryomov static ssize_t __iter_get_bvecs(struct iov_iter *iter, size_t maxsize,
84fc218544SIlya Dryomov struct bio_vec *bvecs)
85fc218544SIlya Dryomov {
86fc218544SIlya Dryomov size_t size = 0;
87fc218544SIlya Dryomov int bvec_idx = 0;
88fc218544SIlya Dryomov
89fc218544SIlya Dryomov if (maxsize > iov_iter_count(iter))
90fc218544SIlya Dryomov maxsize = iov_iter_count(iter);
91fc218544SIlya Dryomov
92fc218544SIlya Dryomov while (size < maxsize) {
93fc218544SIlya Dryomov struct page *pages[ITER_GET_BVECS_PAGES];
94fc218544SIlya Dryomov ssize_t bytes;
95fc218544SIlya Dryomov size_t start;
96fc218544SIlya Dryomov int idx = 0;
97fc218544SIlya Dryomov
981ef255e2SAl Viro bytes = iov_iter_get_pages2(iter, pages, maxsize - size,
99fc218544SIlya Dryomov ITER_GET_BVECS_PAGES, &start);
100fc218544SIlya Dryomov if (bytes < 0)
101fc218544SIlya Dryomov return size ?: bytes;
102fc218544SIlya Dryomov
103fc218544SIlya Dryomov size += bytes;
104fc218544SIlya Dryomov
105fc218544SIlya Dryomov for ( ; bytes; idx++, bvec_idx++) {
1065c6542b6SChristoph Hellwig int len = min_t(int, bytes, PAGE_SIZE - start);
107fc218544SIlya Dryomov
1085c6542b6SChristoph Hellwig bvec_set_page(&bvecs[bvec_idx], pages[idx], len, start);
1095c6542b6SChristoph Hellwig bytes -= len;
110fc218544SIlya Dryomov start = 0;
111b5b98989SZhu, Caifeng }
112fc218544SIlya Dryomov }
113fc218544SIlya Dryomov
114b5b98989SZhu, Caifeng return size;
115b5b98989SZhu, Caifeng }
116b5b98989SZhu, Caifeng
117b5b98989SZhu, Caifeng /*
118fc218544SIlya Dryomov * iov_iter_get_pages() only considers one iov_iter segment, no matter
119fc218544SIlya Dryomov * what maxsize or maxpages are given. For ITER_BVEC that is a single
120fc218544SIlya Dryomov * page.
121fc218544SIlya Dryomov *
122fc218544SIlya Dryomov * Attempt to get up to @maxsize bytes worth of pages from @iter.
123fc218544SIlya Dryomov * Return the number of bytes in the created bio_vec array, or an error.
124b5b98989SZhu, Caifeng */
iter_get_bvecs_alloc(struct iov_iter * iter,size_t maxsize,struct bio_vec ** bvecs,int * num_bvecs)125fc218544SIlya Dryomov static ssize_t iter_get_bvecs_alloc(struct iov_iter *iter, size_t maxsize,
126fc218544SIlya Dryomov struct bio_vec **bvecs, int *num_bvecs)
127b5b98989SZhu, Caifeng {
128fc218544SIlya Dryomov struct bio_vec *bv;
129fc218544SIlya Dryomov size_t orig_count = iov_iter_count(iter);
130fc218544SIlya Dryomov ssize_t bytes;
131fc218544SIlya Dryomov int npages;
132b5b98989SZhu, Caifeng
133fc218544SIlya Dryomov iov_iter_truncate(iter, maxsize);
134fc218544SIlya Dryomov npages = iov_iter_npages(iter, INT_MAX);
135fc218544SIlya Dryomov iov_iter_reexpand(iter, orig_count);
136b5b98989SZhu, Caifeng
137fc218544SIlya Dryomov /*
138fc218544SIlya Dryomov * __iter_get_bvecs() may populate only part of the array -- zero it
139fc218544SIlya Dryomov * out.
140fc218544SIlya Dryomov */
141fc218544SIlya Dryomov bv = kvmalloc_array(npages, sizeof(*bv), GFP_KERNEL | __GFP_ZERO);
142fc218544SIlya Dryomov if (!bv)
143fc218544SIlya Dryomov return -ENOMEM;
144b5b98989SZhu, Caifeng
145fc218544SIlya Dryomov bytes = __iter_get_bvecs(iter, maxsize, bv);
146fc218544SIlya Dryomov if (bytes < 0) {
147fc218544SIlya Dryomov /*
148fc218544SIlya Dryomov * No pages were pinned -- just free the array.
149fc218544SIlya Dryomov */
150fc218544SIlya Dryomov kvfree(bv);
151fc218544SIlya Dryomov return bytes;
152b5b98989SZhu, Caifeng }
153b5b98989SZhu, Caifeng
154fc218544SIlya Dryomov *bvecs = bv;
155fc218544SIlya Dryomov *num_bvecs = npages;
156fc218544SIlya Dryomov return bytes;
157fc218544SIlya Dryomov }
158fc218544SIlya Dryomov
put_bvecs(struct bio_vec * bvecs,int num_bvecs,bool should_dirty)159fc218544SIlya Dryomov static void put_bvecs(struct bio_vec *bvecs, int num_bvecs, bool should_dirty)
160fc218544SIlya Dryomov {
161fc218544SIlya Dryomov int i;
162fc218544SIlya Dryomov
163fc218544SIlya Dryomov for (i = 0; i < num_bvecs; i++) {
164fc218544SIlya Dryomov if (bvecs[i].bv_page) {
165fc218544SIlya Dryomov if (should_dirty)
166fc218544SIlya Dryomov set_page_dirty_lock(bvecs[i].bv_page);
167fc218544SIlya Dryomov put_page(bvecs[i].bv_page);
168fc218544SIlya Dryomov }
169fc218544SIlya Dryomov }
170fc218544SIlya Dryomov kvfree(bvecs);
171b5b98989SZhu, Caifeng }
172124e68e7SSage Weil
173124e68e7SSage Weil /*
174124e68e7SSage Weil * Prepare an open request. Preallocate ceph_cap to avoid an
175124e68e7SSage Weil * inopportune ENOMEM later.
176124e68e7SSage Weil */
177124e68e7SSage Weil static struct ceph_mds_request *
prepare_open_request(struct super_block * sb,int flags,int create_mode)178124e68e7SSage Weil prepare_open_request(struct super_block *sb, int flags, int create_mode)
179124e68e7SSage Weil {
1802678da88SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
181124e68e7SSage Weil struct ceph_mds_request *req;
182124e68e7SSage Weil int want_auth = USE_ANY_MDS;
183124e68e7SSage Weil int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
184124e68e7SSage Weil
185124e68e7SSage Weil if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
186124e68e7SSage Weil want_auth = USE_AUTH_MDS;
187124e68e7SSage Weil
188124e68e7SSage Weil req = ceph_mdsc_create_request(mdsc, op, want_auth);
189124e68e7SSage Weil if (IS_ERR(req))
190124e68e7SSage Weil goto out;
191124e68e7SSage Weil req->r_fmode = ceph_flags_to_mode(flags);
192f775ff7dSAlexander Graf req->r_args.open.flags = ceph_flags_sys2wire(flags);
193124e68e7SSage Weil req->r_args.open.mode = cpu_to_le32(create_mode);
194124e68e7SSage Weil out:
195124e68e7SSage Weil return req;
196124e68e7SSage Weil }
197124e68e7SSage Weil
ceph_init_file_info(struct inode * inode,struct file * file,int fmode,bool isdir)198bb48bd4dSChengguang Xu static int ceph_init_file_info(struct inode *inode, struct file *file,
199bb48bd4dSChengguang Xu int fmode, bool isdir)
200bb48bd4dSChengguang Xu {
201f4b97866SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
20294cc0877SJeff Layton struct ceph_mount_options *opt =
203985b9ee8SXiubo Li ceph_inode_to_fs_client(&ci->netfs.inode)->mount_options;
204bb48bd4dSChengguang Xu struct ceph_file_info *fi;
205083db6fdSDavid Howells int ret;
206bb48bd4dSChengguang Xu
207bb48bd4dSChengguang Xu dout("%s %p %p 0%o (%s)\n", __func__, inode, file,
208bb48bd4dSChengguang Xu inode->i_mode, isdir ? "dir" : "regular");
209bb48bd4dSChengguang Xu BUG_ON(inode->i_fop->release != ceph_release);
210bb48bd4dSChengguang Xu
211bb48bd4dSChengguang Xu if (isdir) {
212bb48bd4dSChengguang Xu struct ceph_dir_file_info *dfi =
213bb48bd4dSChengguang Xu kmem_cache_zalloc(ceph_dir_file_cachep, GFP_KERNEL);
214135e671eSYan, Zheng if (!dfi)
215bb48bd4dSChengguang Xu return -ENOMEM;
216bb48bd4dSChengguang Xu
217bb48bd4dSChengguang Xu file->private_data = dfi;
218bb48bd4dSChengguang Xu fi = &dfi->file_info;
219bb48bd4dSChengguang Xu dfi->next_offset = 2;
220bb48bd4dSChengguang Xu dfi->readdir_cache_idx = -1;
221bb48bd4dSChengguang Xu } else {
222bb48bd4dSChengguang Xu fi = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
223135e671eSYan, Zheng if (!fi)
224bb48bd4dSChengguang Xu return -ENOMEM;
225bb48bd4dSChengguang Xu
22694cc0877SJeff Layton if (opt->flags & CEPH_MOUNT_OPT_NOPAGECACHE)
22794cc0877SJeff Layton fi->flags |= CEPH_F_SYNC;
22894cc0877SJeff Layton
229bb48bd4dSChengguang Xu file->private_data = fi;
230bb48bd4dSChengguang Xu }
231bb48bd4dSChengguang Xu
232135e671eSYan, Zheng ceph_get_fmode(ci, fmode, 1);
233bb48bd4dSChengguang Xu fi->fmode = fmode;
234135e671eSYan, Zheng
235bb48bd4dSChengguang Xu spin_lock_init(&fi->rw_contexts_lock);
236bb48bd4dSChengguang Xu INIT_LIST_HEAD(&fi->rw_contexts);
237985b9ee8SXiubo Li fi->filp_gen = READ_ONCE(ceph_inode_to_fs_client(inode)->filp_gen);
238bb48bd4dSChengguang Xu
23948490776SXiubo Li if ((file->f_mode & FMODE_WRITE) && ceph_has_inline_data(ci)) {
240083db6fdSDavid Howells ret = ceph_uninline_data(file);
241083db6fdSDavid Howells if (ret < 0)
242083db6fdSDavid Howells goto error;
243083db6fdSDavid Howells }
244083db6fdSDavid Howells
245bb48bd4dSChengguang Xu return 0;
246083db6fdSDavid Howells
247083db6fdSDavid Howells error:
248083db6fdSDavid Howells ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE);
249083db6fdSDavid Howells ceph_put_fmode(ci, fi->fmode, 1);
250083db6fdSDavid Howells kmem_cache_free(ceph_file_cachep, fi);
251083db6fdSDavid Howells /* wake up anyone waiting for caps on this inode */
252083db6fdSDavid Howells wake_up_all(&ci->i_cap_wq);
253083db6fdSDavid Howells return ret;
254bb48bd4dSChengguang Xu }
255bb48bd4dSChengguang Xu
256124e68e7SSage Weil /*
257124e68e7SSage Weil * initialize private struct file data.
258124e68e7SSage Weil * if we fail, clean up by dropping fmode reference on the ceph_inode
259124e68e7SSage Weil */
ceph_init_file(struct inode * inode,struct file * file,int fmode)260124e68e7SSage Weil static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
261124e68e7SSage Weil {
262124e68e7SSage Weil int ret = 0;
263124e68e7SSage Weil
264124e68e7SSage Weil switch (inode->i_mode & S_IFMT) {
265124e68e7SSage Weil case S_IFREG:
266400e1286SJeff Layton ceph_fscache_use_cookie(inode, file->f_mode & FMODE_WRITE);
267df561f66SGustavo A. R. Silva fallthrough;
268124e68e7SSage Weil case S_IFDIR:
269bb48bd4dSChengguang Xu ret = ceph_init_file_info(inode, file, fmode,
270bb48bd4dSChengguang Xu S_ISDIR(inode->i_mode));
271124e68e7SSage Weil break;
272124e68e7SSage Weil
273124e68e7SSage Weil case S_IFLNK:
274124e68e7SSage Weil dout("init_file %p %p 0%o (symlink)\n", inode, file,
275124e68e7SSage Weil inode->i_mode);
276124e68e7SSage Weil break;
277124e68e7SSage Weil
278124e68e7SSage Weil default:
279124e68e7SSage Weil dout("init_file %p %p 0%o (special)\n", inode, file,
280124e68e7SSage Weil inode->i_mode);
281124e68e7SSage Weil /*
282124e68e7SSage Weil * we need to drop the open ref now, since we don't
283124e68e7SSage Weil * have .release set to ceph_release.
284124e68e7SSage Weil */
285124e68e7SSage Weil BUG_ON(inode->i_fop->release == ceph_release);
286124e68e7SSage Weil
287124e68e7SSage Weil /* call the proper open fop */
288124e68e7SSage Weil ret = inode->i_fop->open(inode, file);
289124e68e7SSage Weil }
290124e68e7SSage Weil return ret;
291124e68e7SSage Weil }
292124e68e7SSage Weil
293124e68e7SSage Weil /*
29477310320SYan, Zheng * try renew caps after session gets killed.
29577310320SYan, Zheng */
ceph_renew_caps(struct inode * inode,int fmode)296719a2514SYan, Zheng int ceph_renew_caps(struct inode *inode, int fmode)
29777310320SYan, Zheng {
2982678da88SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
29977310320SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
30077310320SYan, Zheng struct ceph_mds_request *req;
30177310320SYan, Zheng int err, flags, wanted;
30277310320SYan, Zheng
30377310320SYan, Zheng spin_lock(&ci->i_ceph_lock);
304719a2514SYan, Zheng __ceph_touch_fmode(ci, mdsc, fmode);
30577310320SYan, Zheng wanted = __ceph_caps_file_wanted(ci);
30677310320SYan, Zheng if (__ceph_is_any_real_caps(ci) &&
3078242c9f3SYan, Zheng (!(wanted & CEPH_CAP_ANY_WR) || ci->i_auth_cap)) {
30877310320SYan, Zheng int issued = __ceph_caps_issued(ci, NULL);
30977310320SYan, Zheng spin_unlock(&ci->i_ceph_lock);
31077310320SYan, Zheng dout("renew caps %p want %s issued %s updating mds_wanted\n",
31177310320SYan, Zheng inode, ceph_cap_string(wanted), ceph_cap_string(issued));
312e4b731ccSXiubo Li ceph_check_caps(ci, 0);
31377310320SYan, Zheng return 0;
31477310320SYan, Zheng }
31577310320SYan, Zheng spin_unlock(&ci->i_ceph_lock);
31677310320SYan, Zheng
31777310320SYan, Zheng flags = 0;
31877310320SYan, Zheng if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
31977310320SYan, Zheng flags = O_RDWR;
32077310320SYan, Zheng else if (wanted & CEPH_CAP_FILE_RD)
32177310320SYan, Zheng flags = O_RDONLY;
32277310320SYan, Zheng else if (wanted & CEPH_CAP_FILE_WR)
32377310320SYan, Zheng flags = O_WRONLY;
32477310320SYan, Zheng #ifdef O_LAZY
32577310320SYan, Zheng if (wanted & CEPH_CAP_FILE_LAZYIO)
32677310320SYan, Zheng flags |= O_LAZY;
32777310320SYan, Zheng #endif
32877310320SYan, Zheng
32977310320SYan, Zheng req = prepare_open_request(inode->i_sb, flags, 0);
33077310320SYan, Zheng if (IS_ERR(req)) {
33177310320SYan, Zheng err = PTR_ERR(req);
33277310320SYan, Zheng goto out;
33377310320SYan, Zheng }
33477310320SYan, Zheng
33577310320SYan, Zheng req->r_inode = inode;
33677310320SYan, Zheng ihold(inode);
33777310320SYan, Zheng req->r_num_caps = 1;
33877310320SYan, Zheng
33977310320SYan, Zheng err = ceph_mdsc_do_request(mdsc, NULL, req);
34077310320SYan, Zheng ceph_mdsc_put_request(req);
34177310320SYan, Zheng out:
34277310320SYan, Zheng dout("renew caps %p open result=%d\n", inode, err);
34377310320SYan, Zheng return err < 0 ? err : 0;
34477310320SYan, Zheng }
34577310320SYan, Zheng
34677310320SYan, Zheng /*
347124e68e7SSage Weil * If we already have the requisite capabilities, we can satisfy
348124e68e7SSage Weil * the open request locally (no need to request new caps from the
349124e68e7SSage Weil * MDS). We do, however, need to inform the MDS (asynchronously)
350124e68e7SSage Weil * if our wanted caps set expands.
351124e68e7SSage Weil */
ceph_open(struct inode * inode,struct file * file)352124e68e7SSage Weil int ceph_open(struct inode *inode, struct file *file)
353124e68e7SSage Weil {
354124e68e7SSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
355985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
3563d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc;
357124e68e7SSage Weil struct ceph_mds_request *req;
35873737682SChengguang Xu struct ceph_file_info *fi = file->private_data;
359124e68e7SSage Weil int err;
360124e68e7SSage Weil int flags, fmode, wanted;
361124e68e7SSage Weil
36273737682SChengguang Xu if (fi) {
363124e68e7SSage Weil dout("open file %p is already opened\n", file);
364124e68e7SSage Weil return 0;
365124e68e7SSage Weil }
366124e68e7SSage Weil
367124e68e7SSage Weil /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
368124e68e7SSage Weil flags = file->f_flags & ~(O_CREAT|O_EXCL);
36994af0470SJeff Layton if (S_ISDIR(inode->i_mode)) {
370124e68e7SSage Weil flags = O_DIRECTORY; /* mds likes to know */
37194af0470SJeff Layton } else if (S_ISREG(inode->i_mode)) {
37294af0470SJeff Layton err = fscrypt_file_open(inode, file);
37394af0470SJeff Layton if (err)
37494af0470SJeff Layton return err;
37594af0470SJeff Layton }
376124e68e7SSage Weil
377124e68e7SSage Weil dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
378124e68e7SSage Weil ceph_vinop(inode), file, flags, file->f_flags);
379124e68e7SSage Weil fmode = ceph_flags_to_mode(flags);
380124e68e7SSage Weil wanted = ceph_caps_for_mode(fmode);
381124e68e7SSage Weil
382124e68e7SSage Weil /* snapped files are read-only */
383124e68e7SSage Weil if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
384124e68e7SSage Weil return -EROFS;
385124e68e7SSage Weil
386124e68e7SSage Weil /* trivially open snapdir */
387124e68e7SSage Weil if (ceph_snap(inode) == CEPH_SNAPDIR) {
388124e68e7SSage Weil return ceph_init_file(inode, file, fmode);
389124e68e7SSage Weil }
390124e68e7SSage Weil
391124e68e7SSage Weil /*
3927421ab80SSage Weil * No need to block if we have caps on the auth MDS (for
3937421ab80SSage Weil * write) or any MDS (for read). Update wanted set
394124e68e7SSage Weil * asynchronously.
395124e68e7SSage Weil */
396be655596SSage Weil spin_lock(&ci->i_ceph_lock);
3977421ab80SSage Weil if (__ceph_is_any_real_caps(ci) &&
3987421ab80SSage Weil (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
399c1944fedSYan, Zheng int mds_wanted = __ceph_caps_mds_wanted(ci, true);
400124e68e7SSage Weil int issued = __ceph_caps_issued(ci, NULL);
401124e68e7SSage Weil
402124e68e7SSage Weil dout("open %p fmode %d want %s issued %s using existing\n",
403124e68e7SSage Weil inode, fmode, ceph_cap_string(wanted),
404124e68e7SSage Weil ceph_cap_string(issued));
405135e671eSYan, Zheng __ceph_touch_fmode(ci, mdsc, fmode);
406be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
407124e68e7SSage Weil
408124e68e7SSage Weil /* adjust wanted? */
409124e68e7SSage Weil if ((issued & wanted) != wanted &&
410124e68e7SSage Weil (mds_wanted & wanted) != wanted &&
411124e68e7SSage Weil ceph_snap(inode) != CEPH_SNAPDIR)
412e4b731ccSXiubo Li ceph_check_caps(ci, 0);
413124e68e7SSage Weil
414124e68e7SSage Weil return ceph_init_file(inode, file, fmode);
415124e68e7SSage Weil } else if (ceph_snap(inode) != CEPH_NOSNAP &&
416124e68e7SSage Weil (ci->i_snap_caps & wanted) == wanted) {
417719a2514SYan, Zheng __ceph_touch_fmode(ci, mdsc, fmode);
418be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
419124e68e7SSage Weil return ceph_init_file(inode, file, fmode);
420124e68e7SSage Weil }
42199ccbd22SMilosz Tanski
422be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
423124e68e7SSage Weil
424124e68e7SSage Weil dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
425124e68e7SSage Weil req = prepare_open_request(inode->i_sb, flags, 0);
426124e68e7SSage Weil if (IS_ERR(req)) {
427124e68e7SSage Weil err = PTR_ERR(req);
428124e68e7SSage Weil goto out;
429124e68e7SSage Weil }
43070b666c3SSage Weil req->r_inode = inode;
43170b666c3SSage Weil ihold(inode);
43299ccbd22SMilosz Tanski
433124e68e7SSage Weil req->r_num_caps = 1;
434e36d571dSJianpeng Ma err = ceph_mdsc_do_request(mdsc, NULL, req);
435124e68e7SSage Weil if (!err)
436124e68e7SSage Weil err = ceph_init_file(inode, file, req->r_fmode);
437124e68e7SSage Weil ceph_mdsc_put_request(req);
438124e68e7SSage Weil dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
439124e68e7SSage Weil out:
440124e68e7SSage Weil return err;
441124e68e7SSage Weil }
442124e68e7SSage Weil
443785892feSJeff Layton /* Clone the layout from a synchronous create, if the dir now has Dc caps */
444785892feSJeff Layton static void
cache_file_layout(struct inode * dst,struct inode * src)445785892feSJeff Layton cache_file_layout(struct inode *dst, struct inode *src)
446785892feSJeff Layton {
447785892feSJeff Layton struct ceph_inode_info *cdst = ceph_inode(dst);
448785892feSJeff Layton struct ceph_inode_info *csrc = ceph_inode(src);
449785892feSJeff Layton
450785892feSJeff Layton spin_lock(&cdst->i_ceph_lock);
451785892feSJeff Layton if ((__ceph_caps_issued(cdst, NULL) & CEPH_CAP_DIR_CREATE) &&
452785892feSJeff Layton !ceph_file_layout_is_valid(&cdst->i_cached_layout)) {
453785892feSJeff Layton memcpy(&cdst->i_cached_layout, &csrc->i_layout,
454785892feSJeff Layton sizeof(cdst->i_cached_layout));
455785892feSJeff Layton rcu_assign_pointer(cdst->i_cached_layout.pool_ns,
456785892feSJeff Layton ceph_try_get_string(csrc->i_layout.pool_ns));
457785892feSJeff Layton }
458785892feSJeff Layton spin_unlock(&cdst->i_ceph_lock);
459785892feSJeff Layton }
460124e68e7SSage Weil
461124e68e7SSage Weil /*
4629a8d03caSJeff Layton * Try to set up an async create. We need caps, a file layout, and inode number,
4639a8d03caSJeff Layton * and either a lease on the dentry or complete dir info. If any of those
4649a8d03caSJeff Layton * criteria are not satisfied, then return false and the caller can go
4659a8d03caSJeff Layton * synchronous.
4669a8d03caSJeff Layton */
try_prep_async_create(struct inode * dir,struct dentry * dentry,struct ceph_file_layout * lo,u64 * pino)4679a8d03caSJeff Layton static int try_prep_async_create(struct inode *dir, struct dentry *dentry,
4689a8d03caSJeff Layton struct ceph_file_layout *lo, u64 *pino)
4699a8d03caSJeff Layton {
4709a8d03caSJeff Layton struct ceph_inode_info *ci = ceph_inode(dir);
4719a8d03caSJeff Layton struct ceph_dentry_info *di = ceph_dentry(dentry);
4729a8d03caSJeff Layton int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_CREATE;
4739a8d03caSJeff Layton u64 ino;
4749a8d03caSJeff Layton
4759a8d03caSJeff Layton spin_lock(&ci->i_ceph_lock);
4769a8d03caSJeff Layton /* No auth cap means no chance for Dc caps */
4779a8d03caSJeff Layton if (!ci->i_auth_cap)
4789a8d03caSJeff Layton goto no_async;
4799a8d03caSJeff Layton
4809a8d03caSJeff Layton /* Any delegated inos? */
4819a8d03caSJeff Layton if (xa_empty(&ci->i_auth_cap->session->s_delegated_inos))
4829a8d03caSJeff Layton goto no_async;
4839a8d03caSJeff Layton
4849a8d03caSJeff Layton if (!ceph_file_layout_is_valid(&ci->i_cached_layout))
4859a8d03caSJeff Layton goto no_async;
4869a8d03caSJeff Layton
4879a8d03caSJeff Layton if ((__ceph_caps_issued(ci, NULL) & want) != want)
4889a8d03caSJeff Layton goto no_async;
4899a8d03caSJeff Layton
4909a8d03caSJeff Layton if (d_in_lookup(dentry)) {
4919a8d03caSJeff Layton if (!__ceph_dir_is_complete(ci))
4929a8d03caSJeff Layton goto no_async;
4933313f66aSYan, Zheng spin_lock(&dentry->d_lock);
4943313f66aSYan, Zheng di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
4953313f66aSYan, Zheng spin_unlock(&dentry->d_lock);
4969a8d03caSJeff Layton } else if (atomic_read(&ci->i_shared_gen) !=
4979a8d03caSJeff Layton READ_ONCE(di->lease_shared_gen)) {
4989a8d03caSJeff Layton goto no_async;
4999a8d03caSJeff Layton }
5009a8d03caSJeff Layton
5019a8d03caSJeff Layton ino = ceph_get_deleg_ino(ci->i_auth_cap->session);
5029a8d03caSJeff Layton if (!ino)
5039a8d03caSJeff Layton goto no_async;
5049a8d03caSJeff Layton
5059a8d03caSJeff Layton *pino = ino;
5069a8d03caSJeff Layton ceph_take_cap_refs(ci, want, false);
5079a8d03caSJeff Layton memcpy(lo, &ci->i_cached_layout, sizeof(*lo));
5089a8d03caSJeff Layton rcu_assign_pointer(lo->pool_ns,
5099a8d03caSJeff Layton ceph_try_get_string(ci->i_cached_layout.pool_ns));
5109a8d03caSJeff Layton got = want;
5119a8d03caSJeff Layton no_async:
5129a8d03caSJeff Layton spin_unlock(&ci->i_ceph_lock);
5139a8d03caSJeff Layton return got;
5149a8d03caSJeff Layton }
5159a8d03caSJeff Layton
restore_deleg_ino(struct inode * dir,u64 ino)5169a8d03caSJeff Layton static void restore_deleg_ino(struct inode *dir, u64 ino)
5179a8d03caSJeff Layton {
5189a8d03caSJeff Layton struct ceph_inode_info *ci = ceph_inode(dir);
5199a8d03caSJeff Layton struct ceph_mds_session *s = NULL;
5209a8d03caSJeff Layton
5219a8d03caSJeff Layton spin_lock(&ci->i_ceph_lock);
5229a8d03caSJeff Layton if (ci->i_auth_cap)
5239a8d03caSJeff Layton s = ceph_get_mds_session(ci->i_auth_cap->session);
5249a8d03caSJeff Layton spin_unlock(&ci->i_ceph_lock);
5259a8d03caSJeff Layton if (s) {
5269a8d03caSJeff Layton int err = ceph_restore_deleg_ino(s, ino);
5279a8d03caSJeff Layton if (err)
5289a8d03caSJeff Layton pr_warn("ceph: unable to restore delegated ino 0x%llx to session: %d\n",
5299a8d03caSJeff Layton ino, err);
5309a8d03caSJeff Layton ceph_put_mds_session(s);
5319a8d03caSJeff Layton }
5329a8d03caSJeff Layton }
5339a8d03caSJeff Layton
wake_async_create_waiters(struct inode * inode,struct ceph_mds_session * session)5344d9513cfSJeff Layton static void wake_async_create_waiters(struct inode *inode,
5354d9513cfSJeff Layton struct ceph_mds_session *session)
5369a8d03caSJeff Layton {
5374d9513cfSJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
53868c62beeSXiubo Li bool check_cap = false;
5399a8d03caSJeff Layton
5409a8d03caSJeff Layton spin_lock(&ci->i_ceph_lock);
5419a8d03caSJeff Layton if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
5429a8d03caSJeff Layton ci->i_ceph_flags &= ~CEPH_I_ASYNC_CREATE;
5439a8d03caSJeff Layton wake_up_bit(&ci->i_ceph_flags, CEPH_ASYNC_CREATE_BIT);
54468c62beeSXiubo Li
54568c62beeSXiubo Li if (ci->i_ceph_flags & CEPH_I_ASYNC_CHECK_CAPS) {
54668c62beeSXiubo Li ci->i_ceph_flags &= ~CEPH_I_ASYNC_CHECK_CAPS;
54768c62beeSXiubo Li check_cap = true;
54868c62beeSXiubo Li }
5499a8d03caSJeff Layton }
5504d9513cfSJeff Layton ceph_kick_flushing_inode_caps(session, ci);
5519a8d03caSJeff Layton spin_unlock(&ci->i_ceph_lock);
55268c62beeSXiubo Li
55368c62beeSXiubo Li if (check_cap)
55468c62beeSXiubo Li ceph_check_caps(ci, CHECK_CAPS_FLUSH);
5554d9513cfSJeff Layton }
5564d9513cfSJeff Layton
ceph_async_create_cb(struct ceph_mds_client * mdsc,struct ceph_mds_request * req)5574d9513cfSJeff Layton static void ceph_async_create_cb(struct ceph_mds_client *mdsc,
5584d9513cfSJeff Layton struct ceph_mds_request *req)
5594d9513cfSJeff Layton {
5604d9513cfSJeff Layton struct dentry *dentry = req->r_dentry;
5614d9513cfSJeff Layton struct inode *dinode = d_inode(dentry);
5624d9513cfSJeff Layton struct inode *tinode = req->r_target_inode;
5634d9513cfSJeff Layton int result = req->r_err ? req->r_err :
5644d9513cfSJeff Layton le32_to_cpu(req->r_reply_info.head->result);
5654d9513cfSJeff Layton
5664d9513cfSJeff Layton WARN_ON_ONCE(dinode && tinode && dinode != tinode);
5674d9513cfSJeff Layton
5684d9513cfSJeff Layton /* MDS changed -- caller must resubmit */
5694d9513cfSJeff Layton if (result == -EJUKEBOX)
5704d9513cfSJeff Layton goto out;
5714d9513cfSJeff Layton
5724d9513cfSJeff Layton mapping_set_error(req->r_parent->i_mapping, result);
5734d9513cfSJeff Layton
5744d9513cfSJeff Layton if (result) {
5754d9513cfSJeff Layton int pathlen = 0;
5764d9513cfSJeff Layton u64 base = 0;
5772e2023e9SXiubo Li char *path = ceph_mdsc_build_path(mdsc, req->r_dentry, &pathlen,
5784d9513cfSJeff Layton &base, 0);
5794d9513cfSJeff Layton
5804868e537SXiubo Li pr_warn("async create failure path=(%llx)%s result=%d!\n",
5814d9513cfSJeff Layton base, IS_ERR(path) ? "<<bad>>" : path, result);
5824d9513cfSJeff Layton ceph_mdsc_free_path(path, pathlen);
5834d9513cfSJeff Layton
5844d9513cfSJeff Layton ceph_dir_clear_complete(req->r_parent);
5854d9513cfSJeff Layton if (!d_unhashed(dentry))
5864d9513cfSJeff Layton d_drop(dentry);
5874d9513cfSJeff Layton
5884d9513cfSJeff Layton if (dinode) {
5894d9513cfSJeff Layton mapping_set_error(dinode->i_mapping, result);
5904d9513cfSJeff Layton ceph_inode_shutdown(dinode);
5914d9513cfSJeff Layton wake_async_create_waiters(dinode, req->r_session);
5924d9513cfSJeff Layton }
5934d9513cfSJeff Layton }
5944d9513cfSJeff Layton
5954d9513cfSJeff Layton if (tinode) {
5964d9513cfSJeff Layton u64 ino = ceph_vino(tinode).ino;
5974d9513cfSJeff Layton
5984d9513cfSJeff Layton if (req->r_deleg_ino != ino)
5994d9513cfSJeff Layton pr_warn("%s: inode number mismatch! err=%d deleg_ino=0x%llx target=0x%llx\n",
6004d9513cfSJeff Layton __func__, req->r_err, req->r_deleg_ino, ino);
6014d9513cfSJeff Layton
6024d9513cfSJeff Layton mapping_set_error(tinode->i_mapping, result);
6034d9513cfSJeff Layton wake_async_create_waiters(tinode, req->r_session);
6046407fbb9SJeff Layton } else if (!result) {
6059a8d03caSJeff Layton pr_warn("%s: no req->r_target_inode for 0x%llx\n", __func__,
6069a8d03caSJeff Layton req->r_deleg_ino);
6079a8d03caSJeff Layton }
6089a8d03caSJeff Layton out:
6099a8d03caSJeff Layton ceph_mdsc_release_dir_caps(req);
6109a8d03caSJeff Layton }
6119a8d03caSJeff Layton
ceph_finish_async_create(struct inode * dir,struct inode * inode,struct dentry * dentry,struct file * file,umode_t mode,struct ceph_mds_request * req,struct ceph_acl_sec_ctx * as_ctx,struct ceph_file_layout * lo)612ec9595c0SJeff Layton static int ceph_finish_async_create(struct inode *dir, struct inode *inode,
613ec9595c0SJeff Layton struct dentry *dentry,
6149a8d03caSJeff Layton struct file *file, umode_t mode,
6159a8d03caSJeff Layton struct ceph_mds_request *req,
6169a8d03caSJeff Layton struct ceph_acl_sec_ctx *as_ctx,
6179a8d03caSJeff Layton struct ceph_file_layout *lo)
6189a8d03caSJeff Layton {
6199a8d03caSJeff Layton int ret;
6209a8d03caSJeff Layton char xattr_buf[4];
6219a8d03caSJeff Layton struct ceph_mds_reply_inode in = { };
6229a8d03caSJeff Layton struct ceph_mds_reply_info_in iinfo = { .in = &in };
6239a8d03caSJeff Layton struct ceph_inode_info *ci = ceph_inode(dir);
62400061645SXiubo Li struct ceph_dentry_info *di = ceph_dentry(dentry);
6259a8d03caSJeff Layton struct timespec64 now;
6264584a768SJeff Layton struct ceph_string *pool_ns;
62727171ae6SJeff Layton struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
6289a8d03caSJeff Layton struct ceph_vino vino = { .ino = req->r_deleg_ino,
6299a8d03caSJeff Layton .snap = CEPH_NOSNAP };
6309a8d03caSJeff Layton
6319a8d03caSJeff Layton ktime_get_real_ts64(&now);
6329a8d03caSJeff Layton
6339a8d03caSJeff Layton iinfo.inline_version = CEPH_INLINE_NONE;
6349a8d03caSJeff Layton iinfo.change_attr = 1;
6359a8d03caSJeff Layton ceph_encode_timespec64(&iinfo.btime, &now);
6369a8d03caSJeff Layton
637620239d9SJeff Layton if (req->r_pagelist) {
638620239d9SJeff Layton iinfo.xattr_len = req->r_pagelist->length;
639620239d9SJeff Layton iinfo.xattr_data = req->r_pagelist->mapped_tail;
640620239d9SJeff Layton } else {
641620239d9SJeff Layton /* fake it */
6429a8d03caSJeff Layton iinfo.xattr_len = ARRAY_SIZE(xattr_buf);
6439a8d03caSJeff Layton iinfo.xattr_data = xattr_buf;
6449a8d03caSJeff Layton memset(iinfo.xattr_data, 0, iinfo.xattr_len);
645620239d9SJeff Layton }
6469a8d03caSJeff Layton
6479a8d03caSJeff Layton in.ino = cpu_to_le64(vino.ino);
6489a8d03caSJeff Layton in.snapid = cpu_to_le64(CEPH_NOSNAP);
6499a8d03caSJeff Layton in.version = cpu_to_le64(1); // ???
6509a8d03caSJeff Layton in.cap.caps = in.cap.wanted = cpu_to_le32(CEPH_CAP_ALL_FILE);
6519a8d03caSJeff Layton in.cap.cap_id = cpu_to_le64(1);
6529a8d03caSJeff Layton in.cap.realm = cpu_to_le64(ci->i_snap_realm->ino);
6539a8d03caSJeff Layton in.cap.flags = CEPH_CAP_FLAG_AUTH;
6549a8d03caSJeff Layton in.ctime = in.mtime = in.atime = iinfo.btime;
6559a8d03caSJeff Layton in.truncate_seq = cpu_to_le32(1);
6569a8d03caSJeff Layton in.truncate_size = cpu_to_le64(-1ULL);
6579a8d03caSJeff Layton in.xattr_version = cpu_to_le64(1);
6589a8d03caSJeff Layton in.uid = cpu_to_le32(from_kuid(&init_user_ns, current_fsuid()));
659fd84bfddSChristian Brauner if (dir->i_mode & S_ISGID) {
660fd84bfddSChristian Brauner in.gid = cpu_to_le32(from_kgid(&init_user_ns, dir->i_gid));
661fd84bfddSChristian Brauner
662fd84bfddSChristian Brauner /* Directories always inherit the setgid bit. */
663fd84bfddSChristian Brauner if (S_ISDIR(mode))
664fd84bfddSChristian Brauner mode |= S_ISGID;
665fd84bfddSChristian Brauner } else {
666fd84bfddSChristian Brauner in.gid = cpu_to_le32(from_kgid(&init_user_ns, current_fsgid()));
667fd84bfddSChristian Brauner }
668fd84bfddSChristian Brauner in.mode = cpu_to_le32((u32)mode);
669fd84bfddSChristian Brauner
6709a8d03caSJeff Layton in.nlink = cpu_to_le32(1);
6719a8d03caSJeff Layton in.max_size = cpu_to_le64(lo->stripe_unit);
6729a8d03caSJeff Layton
6739a8d03caSJeff Layton ceph_file_layout_to_legacy(lo, &in.layout);
6744584a768SJeff Layton /* lo is private, so pool_ns can't change */
6754584a768SJeff Layton pool_ns = rcu_dereference_raw(lo->pool_ns);
6764584a768SJeff Layton if (pool_ns) {
6774584a768SJeff Layton iinfo.pool_ns_len = pool_ns->len;
6784584a768SJeff Layton iinfo.pool_ns_data = pool_ns->str;
6794584a768SJeff Layton }
6809a8d03caSJeff Layton
68127171ae6SJeff Layton down_read(&mdsc->snap_rwsem);
6829a8d03caSJeff Layton ret = ceph_fill_inode(inode, NULL, &iinfo, NULL, req->r_session,
6839a8d03caSJeff Layton req->r_fmode, NULL);
68427171ae6SJeff Layton up_read(&mdsc->snap_rwsem);
6859a8d03caSJeff Layton if (ret) {
6869a8d03caSJeff Layton dout("%s failed to fill inode: %d\n", __func__, ret);
6879a8d03caSJeff Layton ceph_dir_clear_complete(dir);
6889a8d03caSJeff Layton if (!d_unhashed(dentry))
6899a8d03caSJeff Layton d_drop(dentry);
6909a8d03caSJeff Layton discard_new_inode(inode);
6919a8d03caSJeff Layton } else {
6929a8d03caSJeff Layton struct dentry *dn;
6939a8d03caSJeff Layton
694ebce3eb2SJeff Layton dout("%s d_adding new inode 0x%llx to 0x%llx/%s\n", __func__,
695ebce3eb2SJeff Layton vino.ino, ceph_ino(dir), dentry->d_name.name);
6969a8d03caSJeff Layton ceph_dir_clear_ordered(dir);
6979a8d03caSJeff Layton ceph_init_inode_acls(inode, as_ctx);
6989a8d03caSJeff Layton if (inode->i_state & I_NEW) {
6999a8d03caSJeff Layton /*
7009a8d03caSJeff Layton * If it's not I_NEW, then someone created this before
7019a8d03caSJeff Layton * we got here. Assume the server is aware of it at
7029a8d03caSJeff Layton * that point and don't worry about setting
7039a8d03caSJeff Layton * CEPH_I_ASYNC_CREATE.
7049a8d03caSJeff Layton */
7059a8d03caSJeff Layton ceph_inode(inode)->i_ceph_flags = CEPH_I_ASYNC_CREATE;
7069a8d03caSJeff Layton unlock_new_inode(inode);
7079a8d03caSJeff Layton }
7089a8d03caSJeff Layton if (d_in_lookup(dentry) || d_really_is_negative(dentry)) {
7099a8d03caSJeff Layton if (!d_unhashed(dentry))
7109a8d03caSJeff Layton d_drop(dentry);
7119a8d03caSJeff Layton dn = d_splice_alias(inode, dentry);
7129a8d03caSJeff Layton WARN_ON_ONCE(dn && dn != dentry);
7139a8d03caSJeff Layton }
7149a8d03caSJeff Layton file->f_mode |= FMODE_CREATED;
7159a8d03caSJeff Layton ret = finish_open(file, dentry, ceph_open);
7169a8d03caSJeff Layton }
71700061645SXiubo Li
71800061645SXiubo Li spin_lock(&dentry->d_lock);
71900061645SXiubo Li di->flags &= ~CEPH_DENTRY_ASYNC_CREATE;
72000061645SXiubo Li wake_up_bit(&di->flags, CEPH_DENTRY_ASYNC_CREATE_BIT);
72100061645SXiubo Li spin_unlock(&dentry->d_lock);
72200061645SXiubo Li
7239a8d03caSJeff Layton return ret;
7249a8d03caSJeff Layton }
7259a8d03caSJeff Layton
7269a8d03caSJeff Layton /*
7275ef50c3bSSage Weil * Do a lookup + open with a single request. If we get a non-existent
7285ef50c3bSSage Weil * file or symlink, return 1 so the VFS can retry.
729124e68e7SSage Weil */
ceph_atomic_open(struct inode * dir,struct dentry * dentry,struct file * file,unsigned flags,umode_t mode)7305ef50c3bSSage Weil int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
73144907d79SAl Viro struct file *file, unsigned flags, umode_t mode)
732124e68e7SSage Weil {
733985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb);
7343d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc;
735124e68e7SSage Weil struct ceph_mds_request *req;
736ec9595c0SJeff Layton struct inode *new_inode = NULL;
7375ef50c3bSSage Weil struct dentry *dn;
7385c31e92dSYan, Zheng struct ceph_acl_sec_ctx as_ctx = {};
7399a8d03caSJeff Layton bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
740315f2408SYan, Zheng int mask;
741124e68e7SSage Weil int err;
742124e68e7SSage Weil
743a455589fSAl Viro dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
744a455589fSAl Viro dir, dentry, dentry,
7455ef50c3bSSage Weil d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
7465ef50c3bSSage Weil
7475ef50c3bSSage Weil if (dentry->d_name.len > NAME_MAX)
7485ef50c3bSSage Weil return -ENAMETOOLONG;
7495ef50c3bSSage Weil
7504868e537SXiubo Li err = ceph_wait_on_conflict_unlink(dentry);
7514868e537SXiubo Li if (err)
7524868e537SXiubo Li return err;
7537cb99947SHu Weiwen /*
7547cb99947SHu Weiwen * Do not truncate the file, since atomic_open is called before the
7557cb99947SHu Weiwen * permission check. The caller will do the truncation afterward.
7567cb99947SHu Weiwen */
7577cb99947SHu Weiwen flags &= ~O_TRUNC;
7584868e537SXiubo Li
759ec9595c0SJeff Layton retry:
760b1ee94aaSYan, Zheng if (flags & O_CREAT) {
761b7a29217SLuis Henriques if (ceph_quota_is_max_files_exceeded(dir))
762b7a29217SLuis Henriques return -EDQUOT;
763ec9595c0SJeff Layton
764ec9595c0SJeff Layton new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx);
765ec9595c0SJeff Layton if (IS_ERR(new_inode)) {
766ec9595c0SJeff Layton err = PTR_ERR(new_inode);
767ac6713ccSYan, Zheng goto out_ctx;
768ec9595c0SJeff Layton }
769620239d9SJeff Layton /* Async create can't handle more than a page of xattrs */
770620239d9SJeff Layton if (as_ctx.pagelist &&
771620239d9SJeff Layton !list_is_singular(&as_ctx.pagelist->head))
772620239d9SJeff Layton try_async = false;
7735bb5e6eeSJeff Layton } else if (!d_in_lookup(dentry)) {
7745bb5e6eeSJeff Layton /* If it's not being looked up, it's negative */
7755bb5e6eeSJeff Layton return -ENOENT;
776b1ee94aaSYan, Zheng }
777ec9595c0SJeff Layton
778124e68e7SSage Weil /* do the open */
779124e68e7SSage Weil req = prepare_open_request(dir->i_sb, flags, mode);
780b1ee94aaSYan, Zheng if (IS_ERR(req)) {
781b1ee94aaSYan, Zheng err = PTR_ERR(req);
7825c31e92dSYan, Zheng goto out_ctx;
783b1ee94aaSYan, Zheng }
784124e68e7SSage Weil req->r_dentry = dget(dentry);
785124e68e7SSage Weil req->r_num_caps = 2;
7869a8d03caSJeff Layton mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
7879a8d03caSJeff Layton if (ceph_security_xattr_wanted(dir))
7889a8d03caSJeff Layton mask |= CEPH_CAP_XATTR_SHARED;
7899a8d03caSJeff Layton req->r_args.open.mask = cpu_to_le32(mask);
7909a8d03caSJeff Layton req->r_parent = dir;
7914c183472SJeff Layton ihold(dir);
792cb3524a8SJeff Layton if (IS_ENCRYPTED(dir)) {
79316be62fcSJeff Layton set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
794d9ae977dSLuís Henriques err = fscrypt_prepare_lookup_partial(dir, dentry);
795d9ae977dSLuís Henriques if (err < 0)
796d9ae977dSLuís Henriques goto out_req;
797cb3524a8SJeff Layton }
7989a8d03caSJeff Layton
799124e68e7SSage Weil if (flags & O_CREAT) {
8009a8d03caSJeff Layton struct ceph_file_layout lo;
8019a8d03caSJeff Layton
802d9d00f71SXiubo Li req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL |
803d9d00f71SXiubo Li CEPH_CAP_XATTR_EXCL;
804124e68e7SSage Weil req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
805ec9595c0SJeff Layton
806ec9595c0SJeff Layton ceph_as_ctx_to_req(req, &as_ctx);
807ec9595c0SJeff Layton
808ec9595c0SJeff Layton if (try_async && (req->r_dir_caps =
8099a8d03caSJeff Layton try_prep_async_create(dir, dentry, &lo,
8109a8d03caSJeff Layton &req->r_deleg_ino))) {
811ec9595c0SJeff Layton struct ceph_vino vino = { .ino = req->r_deleg_ino,
812ec9595c0SJeff Layton .snap = CEPH_NOSNAP };
81300061645SXiubo Li struct ceph_dentry_info *di = ceph_dentry(dentry);
81400061645SXiubo Li
8159a8d03caSJeff Layton set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
8169a8d03caSJeff Layton req->r_args.open.flags |= cpu_to_le32(CEPH_O_EXCL);
8179a8d03caSJeff Layton req->r_callback = ceph_async_create_cb;
81800061645SXiubo Li
819ec9595c0SJeff Layton /* Hash inode before RPC */
820ec9595c0SJeff Layton new_inode = ceph_get_inode(dir->i_sb, vino, new_inode);
821ec9595c0SJeff Layton if (IS_ERR(new_inode)) {
822ec9595c0SJeff Layton err = PTR_ERR(new_inode);
823ec9595c0SJeff Layton new_inode = NULL;
824ec9595c0SJeff Layton goto out_req;
825ec9595c0SJeff Layton }
826ec9595c0SJeff Layton WARN_ON_ONCE(!(new_inode->i_state & I_NEW));
827ec9595c0SJeff Layton
82800061645SXiubo Li spin_lock(&dentry->d_lock);
82900061645SXiubo Li di->flags |= CEPH_DENTRY_ASYNC_CREATE;
83000061645SXiubo Li spin_unlock(&dentry->d_lock);
83100061645SXiubo Li
8329a8d03caSJeff Layton err = ceph_mdsc_submit_request(mdsc, dir, req);
8339a8d03caSJeff Layton if (!err) {
834ec9595c0SJeff Layton err = ceph_finish_async_create(dir, new_inode,
835ec9595c0SJeff Layton dentry, file,
836ec9595c0SJeff Layton mode, req,
8379a8d03caSJeff Layton &as_ctx, &lo);
838ec9595c0SJeff Layton new_inode = NULL;
8399a8d03caSJeff Layton } else if (err == -EJUKEBOX) {
8409a8d03caSJeff Layton restore_deleg_ino(dir, req->r_deleg_ino);
8419a8d03caSJeff Layton ceph_mdsc_put_request(req);
842ec9595c0SJeff Layton discard_new_inode(new_inode);
843ec9595c0SJeff Layton ceph_release_acl_sec_ctx(&as_ctx);
844ec9595c0SJeff Layton memset(&as_ctx, 0, sizeof(as_ctx));
845ec9595c0SJeff Layton new_inode = NULL;
8469a8d03caSJeff Layton try_async = false;
847932a9b58SJeff Layton ceph_put_string(rcu_dereference_raw(lo.pool_ns));
8489a8d03caSJeff Layton goto retry;
8499a8d03caSJeff Layton }
850932a9b58SJeff Layton ceph_put_string(rcu_dereference_raw(lo.pool_ns));
8519a8d03caSJeff Layton goto out_req;
8529a8d03caSJeff Layton }
853124e68e7SSage Weil }
854315f2408SYan, Zheng
8553dd69aabSJeff Layton set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
856ec9595c0SJeff Layton req->r_new_inode = new_inode;
857ec9595c0SJeff Layton new_inode = NULL;
8587cb99947SHu Weiwen err = ceph_mdsc_do_request(mdsc, (flags & O_CREAT) ? dir : NULL, req);
8597a971e2cSJeff Layton if (err == -ENOENT) {
8607a971e2cSJeff Layton dentry = ceph_handle_snapdir(req, dentry);
861aa60cfc3SJeff Layton if (IS_ERR(dentry)) {
862aa60cfc3SJeff Layton err = PTR_ERR(dentry);
863b1ee94aaSYan, Zheng goto out_req;
864aa60cfc3SJeff Layton }
865aa60cfc3SJeff Layton err = 0;
8667a971e2cSJeff Layton }
86779aec984SSam Lang
8687a971e2cSJeff Layton if (!err && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
869124e68e7SSage Weil err = ceph_handle_notrace_create(dir, dentry);
8705ef50c3bSSage Weil
87100699ad8SAl Viro if (d_in_lookup(dentry)) {
8725ef50c3bSSage Weil dn = ceph_finish_lookup(req, dentry, err);
8735ef50c3bSSage Weil if (IS_ERR(dn))
8745ef50c3bSSage Weil err = PTR_ERR(dn);
8755ef50c3bSSage Weil } else {
8765ef50c3bSSage Weil /* we were given a hashed negative dentry */
8775ef50c3bSSage Weil dn = NULL;
8785ef50c3bSSage Weil }
879468640e3SSage Weil if (err)
880b1ee94aaSYan, Zheng goto out_req;
8812b0143b5SDavid Howells if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
8825ef50c3bSSage Weil /* make vfs retry on splice, ENOENT, or symlink */
8835ef50c3bSSage Weil dout("atomic_open finish_no_open on dn %p\n", dn);
8845ef50c3bSSage Weil err = finish_no_open(file, dn);
8855ef50c3bSSage Weil } else {
88694af0470SJeff Layton if (IS_ENCRYPTED(dir) &&
88794af0470SJeff Layton !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
88894af0470SJeff Layton pr_warn("Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
88994af0470SJeff Layton ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
89094af0470SJeff Layton goto out_req;
89194af0470SJeff Layton }
89294af0470SJeff Layton
8935ef50c3bSSage Weil dout("atomic_open finish_open on dn %p\n", dn);
8946e8575faSSam Lang if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
895785892feSJeff Layton struct inode *newino = d_inode(dentry);
896785892feSJeff Layton
897785892feSJeff Layton cache_file_layout(dir, newino);
898785892feSJeff Layton ceph_init_inode_acls(newino, &as_ctx);
89973a09dd9SAl Viro file->f_mode |= FMODE_CREATED;
9006e8575faSSam Lang }
901be12af3eSAl Viro err = finish_open(file, dentry, ceph_open);
9025ef50c3bSSage Weil }
903b1ee94aaSYan, Zheng out_req:
904124e68e7SSage Weil ceph_mdsc_put_request(req);
905ec9595c0SJeff Layton iput(new_inode);
9065c31e92dSYan, Zheng out_ctx:
9075c31e92dSYan, Zheng ceph_release_acl_sec_ctx(&as_ctx);
9085ef50c3bSSage Weil dout("atomic_open result=%d\n", err);
909d9585277SAl Viro return err;
910124e68e7SSage Weil }
911124e68e7SSage Weil
ceph_release(struct inode * inode,struct file * file)912124e68e7SSage Weil int ceph_release(struct inode *inode, struct file *file)
913124e68e7SSage Weil {
914124e68e7SSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
915124e68e7SSage Weil
916bb48bd4dSChengguang Xu if (S_ISDIR(inode->i_mode)) {
917bb48bd4dSChengguang Xu struct ceph_dir_file_info *dfi = file->private_data;
918bb48bd4dSChengguang Xu dout("release inode %p dir file %p\n", inode, file);
919bb48bd4dSChengguang Xu WARN_ON(!list_empty(&dfi->file_info.rw_contexts));
920bb48bd4dSChengguang Xu
921719a2514SYan, Zheng ceph_put_fmode(ci, dfi->file_info.fmode, 1);
922bb48bd4dSChengguang Xu
923bb48bd4dSChengguang Xu if (dfi->last_readdir)
924bb48bd4dSChengguang Xu ceph_mdsc_put_request(dfi->last_readdir);
925bb48bd4dSChengguang Xu kfree(dfi->last_name);
926bb48bd4dSChengguang Xu kfree(dfi->dir_info);
927bb48bd4dSChengguang Xu kmem_cache_free(ceph_dir_file_cachep, dfi);
928bb48bd4dSChengguang Xu } else {
929bb48bd4dSChengguang Xu struct ceph_file_info *fi = file->private_data;
930bb48bd4dSChengguang Xu dout("release inode %p regular file %p\n", inode, file);
93173737682SChengguang Xu WARN_ON(!list_empty(&fi->rw_contexts));
932bb48bd4dSChengguang Xu
933400e1286SJeff Layton ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE);
934719a2514SYan, Zheng ceph_put_fmode(ci, fi->fmode, 1);
935719a2514SYan, Zheng
93673737682SChengguang Xu kmem_cache_free(ceph_file_cachep, fi);
937bb48bd4dSChengguang Xu }
938195d3ce2SSage Weil
939195d3ce2SSage Weil /* wake up anyone waiting for caps on this inode */
94003066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq);
941124e68e7SSage Weil return 0;
942124e68e7SSage Weil }
943124e68e7SSage Weil
94483701246SYan, Zheng enum {
945c8fe9b17SYan, Zheng HAVE_RETRIED = 1,
946c8fe9b17SYan, Zheng CHECK_EOF = 2,
947c8fe9b17SYan, Zheng READ_INLINE = 3,
94883701246SYan, Zheng };
94983701246SYan, Zheng
950124e68e7SSage Weil /*
951fce7a974SYan, Zheng * Completely synchronous read and write methods. Direct from __user
952fce7a974SYan, Zheng * buffer to osd, or directly to user pages (if O_DIRECT).
953fce7a974SYan, Zheng *
954fce7a974SYan, Zheng * If the read spans object boundary, just do multiple reads. (That's not
955fce7a974SYan, Zheng * atomic, but good enough for now.)
956124e68e7SSage Weil *
957124e68e7SSage Weil * If we get a short result from the OSD, check against i_size; we need to
958124e68e7SSage Weil * only return a short read to the caller if we hit EOF.
959124e68e7SSage Weil */
__ceph_sync_read(struct inode * inode,loff_t * ki_pos,struct iov_iter * to,int * retry_op,u64 * last_objver)960d4d51887SXiubo Li ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
961d4d51887SXiubo Li struct iov_iter *to, int *retry_op,
962d4d51887SXiubo Li u64 *last_objver)
963124e68e7SSage Weil {
964fce7a974SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
965985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
966fce7a974SYan, Zheng struct ceph_osd_client *osdc = &fsc->client->osdc;
9677ce469a5SYan, Zheng ssize_t ret;
968d4d51887SXiubo Li u64 off = *ki_pos;
969fce7a974SYan, Zheng u64 len = iov_iter_count(to);
970e485d028SJeff Layton u64 i_size = i_size_read(inode);
971f0fe1e54SJeff Layton bool sparse = IS_ENCRYPTED(inode) || ceph_test_mount_opt(fsc, SPARSEREAD);
972d4d51887SXiubo Li u64 objver = 0;
973124e68e7SSage Weil
974d4d51887SXiubo Li dout("sync_read on inode %p %llx~%llx\n", inode, *ki_pos, len);
975d4d51887SXiubo Li
976d4d51887SXiubo Li if (ceph_inode_is_shutdown(inode))
977d4d51887SXiubo Li return -EIO;
978d0d0db22SYan, Zheng
979a07c0908SAlex Markuze if (!len || !i_size)
980d0d0db22SYan, Zheng return 0;
981e98b6fedSSage Weil /*
982e98b6fedSSage Weil * flush any page cache pages in this range. this
983e98b6fedSSage Weil * will make concurrent normal and sync io slow,
984e98b6fedSSage Weil * but it will at least behave sensibly when they are
985e98b6fedSSage Weil * in sequence.
986e98b6fedSSage Weil */
987e450f4d1Szhengbin ret = filemap_write_and_wait_range(inode->i_mapping,
988e450f4d1Szhengbin off, off + len - 1);
98929065a51SYehuda Sadeh if (ret < 0)
9908eb4efb0Smajianpeng return ret;
99129065a51SYehuda Sadeh
992fce7a974SYan, Zheng ret = 0;
993fce7a974SYan, Zheng while ((len = iov_iter_count(to)) > 0) {
994fce7a974SYan, Zheng struct ceph_osd_request *req;
995fce7a974SYan, Zheng struct page **pages;
996fce7a974SYan, Zheng int num_pages;
9977ce469a5SYan, Zheng size_t page_off;
998fce7a974SYan, Zheng bool more;
999a07c0908SAlex Markuze int idx = 0;
1000c5f575edSJeff Layton size_t left;
100103bc06c7SJeff Layton struct ceph_osd_req_op *op;
1002f0fe1e54SJeff Layton u64 read_off = off;
1003f0fe1e54SJeff Layton u64 read_len = len;
1004fb98248fSXiubo Li int extent_cnt;
1005f0fe1e54SJeff Layton
1006f0fe1e54SJeff Layton /* determine new offset/length if encrypted */
1007f0fe1e54SJeff Layton ceph_fscrypt_adjust_off_and_len(inode, &read_off, &read_len);
1008f0fe1e54SJeff Layton
1009f0fe1e54SJeff Layton dout("sync_read orig %llu~%llu reading %llu~%llu",
1010f0fe1e54SJeff Layton off, len, read_off, read_len);
1011fce7a974SYan, Zheng
1012fce7a974SYan, Zheng req = ceph_osdc_new_request(osdc, &ci->i_layout,
1013f0fe1e54SJeff Layton ci->i_vino, read_off, &read_len, 0, 1,
1014f0fe1e54SJeff Layton sparse ? CEPH_OSD_OP_SPARSE_READ :
1015f0fe1e54SJeff Layton CEPH_OSD_OP_READ,
101603bc06c7SJeff Layton CEPH_OSD_FLAG_READ,
1017fce7a974SYan, Zheng NULL, ci->i_truncate_seq,
1018fce7a974SYan, Zheng ci->i_truncate_size, false);
1019fce7a974SYan, Zheng if (IS_ERR(req)) {
1020fce7a974SYan, Zheng ret = PTR_ERR(req);
1021fce7a974SYan, Zheng break;
1022fce7a974SYan, Zheng }
1023fce7a974SYan, Zheng
1024f0fe1e54SJeff Layton /* adjust len downward if the request truncated the len */
1025f0fe1e54SJeff Layton if (off + len > read_off + read_len)
1026f0fe1e54SJeff Layton len = read_off + read_len - off;
1027fce7a974SYan, Zheng more = len < iov_iter_count(to);
1028fce7a974SYan, Zheng
102944e518abSIlya Dryomov op = &req->r_ops[0];
103044e518abSIlya Dryomov if (sparse) {
103144e518abSIlya Dryomov extent_cnt = __ceph_sparse_read_ext_count(inode, read_len);
103244e518abSIlya Dryomov ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
103344e518abSIlya Dryomov if (ret) {
103444e518abSIlya Dryomov ceph_osdc_put_request(req);
103544e518abSIlya Dryomov break;
103644e518abSIlya Dryomov }
103744e518abSIlya Dryomov }
103844e518abSIlya Dryomov
1039f0fe1e54SJeff Layton num_pages = calc_pages_for(read_off, read_len);
1040f0fe1e54SJeff Layton page_off = offset_in_page(off);
1041fce7a974SYan, Zheng pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1042fce7a974SYan, Zheng if (IS_ERR(pages)) {
1043fce7a974SYan, Zheng ceph_osdc_put_request(req);
1044fce7a974SYan, Zheng ret = PTR_ERR(pages);
1045fce7a974SYan, Zheng break;
1046fce7a974SYan, Zheng }
10477ce469a5SYan, Zheng
1048f0fe1e54SJeff Layton osd_req_op_extent_osd_data_pages(req, 0, pages, read_len,
1049f0fe1e54SJeff Layton offset_in_page(read_off),
1050b3ab1253SMax Kellermann false, true);
105103bc06c7SJeff Layton
1052a8af0d68SJeff Layton ceph_osdc_start_request(osdc, req);
1053fce7a974SYan, Zheng ret = ceph_osdc_wait_request(osdc, req);
105497e27aaaSXiubo Li
10558ae99ae2SXiubo Li ceph_update_read_metrics(&fsc->mdsc->metric,
105697e27aaaSXiubo Li req->r_start_latency,
105797e27aaaSXiubo Li req->r_end_latency,
1058f0fe1e54SJeff Layton read_len, ret);
105997e27aaaSXiubo Li
1060d4d51887SXiubo Li if (ret > 0)
1061d4d51887SXiubo Li objver = req->r_version;
1062d4d51887SXiubo Li
1063fce7a974SYan, Zheng i_size = i_size_read(inode);
1064fce7a974SYan, Zheng dout("sync_read %llu~%llu got %zd i_size %llu%s\n",
1065fce7a974SYan, Zheng off, len, ret, i_size, (more ? " MORE" : ""));
1066fce7a974SYan, Zheng
106703bc06c7SJeff Layton /* Fix it to go to end of extent map */
106803bc06c7SJeff Layton if (sparse && ret >= 0)
106903bc06c7SJeff Layton ret = ceph_sparse_ext_map_end(op);
107003bc06c7SJeff Layton else if (ret == -ENOENT)
1071fce7a974SYan, Zheng ret = 0;
107203bc06c7SJeff Layton
1073a07c0908SAlex Markuze if (ret < 0) {
1074a07c0908SAlex Markuze ceph_osdc_put_request(req);
1075a07c0908SAlex Markuze if (ret == -EBLOCKLISTED)
1076a07c0908SAlex Markuze fsc->blocklisted = true;
1077a07c0908SAlex Markuze break;
1078a07c0908SAlex Markuze }
1079a07c0908SAlex Markuze
1080a07c0908SAlex Markuze if (IS_ENCRYPTED(inode)) {
1081f0fe1e54SJeff Layton int fret;
1082f0fe1e54SJeff Layton
1083f0fe1e54SJeff Layton fret = ceph_fscrypt_decrypt_extents(inode, pages,
1084f0fe1e54SJeff Layton read_off, op->extent.sparse_ext,
1085f0fe1e54SJeff Layton op->extent.sparse_ext_cnt);
1086f0fe1e54SJeff Layton if (fret < 0) {
1087f0fe1e54SJeff Layton ret = fret;
1088f0fe1e54SJeff Layton ceph_osdc_put_request(req);
1089f0fe1e54SJeff Layton break;
1090f0fe1e54SJeff Layton }
1091f0fe1e54SJeff Layton
1092f0fe1e54SJeff Layton /* account for any partial block at the beginning */
1093f0fe1e54SJeff Layton fret -= (off - read_off);
1094f0fe1e54SJeff Layton
1095f0fe1e54SJeff Layton /*
1096f0fe1e54SJeff Layton * Short read after big offset adjustment?
1097f0fe1e54SJeff Layton * Nothing is usable, just call it a zero
1098f0fe1e54SJeff Layton * len read.
1099f0fe1e54SJeff Layton */
1100f0fe1e54SJeff Layton fret = max(fret, 0);
1101f0fe1e54SJeff Layton
1102f0fe1e54SJeff Layton /* account for partial block at the end */
1103f0fe1e54SJeff Layton ret = min_t(ssize_t, fret, len);
1104f0fe1e54SJeff Layton }
1105f0fe1e54SJeff Layton
1106f0fe1e54SJeff Layton /* Short read but not EOF? Zero out the remainder. */
1107a07c0908SAlex Markuze if (ret < len && (off + ret < i_size)) {
1108fce7a974SYan, Zheng int zlen = min(len - ret, i_size - off - ret);
1109fce7a974SYan, Zheng int zoff = page_off + ret;
111003bc06c7SJeff Layton
1111fce7a974SYan, Zheng dout("sync_read zero gap %llu~%llu\n",
1112fce7a974SYan, Zheng off + ret, off + ret + zlen);
1113fce7a974SYan, Zheng ceph_zero_page_vector_range(zoff, zlen, pages);
1114fce7a974SYan, Zheng ret += zlen;
1115fce7a974SYan, Zheng }
1116fce7a974SYan, Zheng
1117a07c0908SAlex Markuze if (off + ret > i_size)
1118a07c0908SAlex Markuze left = (i_size > off) ? i_size - off : 0;
11193006137eSXiubo Li else
11203006137eSXiubo Li left = ret;
1121a07c0908SAlex Markuze
1122fce7a974SYan, Zheng while (left > 0) {
1123f0fe1e54SJeff Layton size_t plen, copied;
1124f0fe1e54SJeff Layton
1125f0fe1e54SJeff Layton plen = min_t(size_t, left, PAGE_SIZE - page_off);
1126c5f575edSJeff Layton SetPageUptodate(pages[idx]);
1127fce7a974SYan, Zheng copied = copy_page_to_iter(pages[idx++],
1128f0fe1e54SJeff Layton page_off, plen, to);
1129fce7a974SYan, Zheng off += copied;
1130fce7a974SYan, Zheng left -= copied;
1131f0fe1e54SJeff Layton page_off = 0;
1132f0fe1e54SJeff Layton if (copied < plen) {
1133fce7a974SYan, Zheng ret = -EFAULT;
11348eb4efb0Smajianpeng break;
11358eb4efb0Smajianpeng }
11368eb4efb0Smajianpeng }
1137b3ab1253SMax Kellermann
1138b3ab1253SMax Kellermann ceph_osdc_put_request(req);
11398eb4efb0Smajianpeng
1140131d7eb4SYan, Zheng if (off >= i_size || !more)
1141fce7a974SYan, Zheng break;
1142fce7a974SYan, Zheng }
1143fce7a974SYan, Zheng
1144f0fe1e54SJeff Layton if (ret > 0) {
1145c3d8e0b5SXiubo Li if (off >= i_size) {
1146fce7a974SYan, Zheng *retry_op = CHECK_EOF;
1147d4d51887SXiubo Li ret = i_size - *ki_pos;
1148d4d51887SXiubo Li *ki_pos = i_size;
1149c3d8e0b5SXiubo Li } else {
1150d4d51887SXiubo Li ret = off - *ki_pos;
1151d4d51887SXiubo Li *ki_pos = off;
11528eb4efb0Smajianpeng }
11538eb4efb0Smajianpeng
1154f0fe1e54SJeff Layton if (last_objver)
1155d4d51887SXiubo Li *last_objver = objver;
1156f0fe1e54SJeff Layton }
1157fce7a974SYan, Zheng dout("sync_read result %zd retry_op %d\n", ret, *retry_op);
1158124e68e7SSage Weil return ret;
1159124e68e7SSage Weil }
1160124e68e7SSage Weil
ceph_sync_read(struct kiocb * iocb,struct iov_iter * to,int * retry_op)1161d4d51887SXiubo Li static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
1162d4d51887SXiubo Li int *retry_op)
1163d4d51887SXiubo Li {
1164d4d51887SXiubo Li struct file *file = iocb->ki_filp;
1165d4d51887SXiubo Li struct inode *inode = file_inode(file);
1166d4d51887SXiubo Li
1167d4d51887SXiubo Li dout("sync_read on file %p %llx~%zx %s\n", file, iocb->ki_pos,
1168d4d51887SXiubo Li iov_iter_count(to), (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
1169d4d51887SXiubo Li
1170d4d51887SXiubo Li return __ceph_sync_read(inode, &iocb->ki_pos, to, retry_op, NULL);
1171d4d51887SXiubo Li }
1172d4d51887SXiubo Li
1173c8fe9b17SYan, Zheng struct ceph_aio_request {
1174c8fe9b17SYan, Zheng struct kiocb *iocb;
1175c8fe9b17SYan, Zheng size_t total_len;
117685784f93SYan, Zheng bool write;
117785784f93SYan, Zheng bool should_dirty;
1178c8fe9b17SYan, Zheng int error;
1179c8fe9b17SYan, Zheng struct list_head osd_reqs;
1180c8fe9b17SYan, Zheng unsigned num_reqs;
1181c8fe9b17SYan, Zheng atomic_t pending_reqs;
1182fac02ddfSArnd Bergmann struct timespec64 mtime;
1183c8fe9b17SYan, Zheng struct ceph_cap_flush *prealloc_cf;
1184c8fe9b17SYan, Zheng };
1185c8fe9b17SYan, Zheng
11865be0389dSYan, Zheng struct ceph_aio_work {
11875be0389dSYan, Zheng struct work_struct work;
11885be0389dSYan, Zheng struct ceph_osd_request *req;
11895be0389dSYan, Zheng };
11905be0389dSYan, Zheng
11915be0389dSYan, Zheng static void ceph_aio_retry_work(struct work_struct *work);
11925be0389dSYan, Zheng
ceph_aio_complete(struct inode * inode,struct ceph_aio_request * aio_req)1193c8fe9b17SYan, Zheng static void ceph_aio_complete(struct inode *inode,
1194c8fe9b17SYan, Zheng struct ceph_aio_request *aio_req)
1195c8fe9b17SYan, Zheng {
1196c8fe9b17SYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
1197c8fe9b17SYan, Zheng int ret;
1198c8fe9b17SYan, Zheng
1199c8fe9b17SYan, Zheng if (!atomic_dec_and_test(&aio_req->pending_reqs))
1200c8fe9b17SYan, Zheng return;
1201c8fe9b17SYan, Zheng
12026a81749eSJeff Layton if (aio_req->iocb->ki_flags & IOCB_DIRECT)
12036a81749eSJeff Layton inode_dio_end(inode);
12046a81749eSJeff Layton
1205c8fe9b17SYan, Zheng ret = aio_req->error;
1206c8fe9b17SYan, Zheng if (!ret)
1207c8fe9b17SYan, Zheng ret = aio_req->total_len;
1208c8fe9b17SYan, Zheng
1209c8fe9b17SYan, Zheng dout("ceph_aio_complete %p rc %d\n", inode, ret);
1210c8fe9b17SYan, Zheng
1211c8fe9b17SYan, Zheng if (ret >= 0 && aio_req->write) {
1212c8fe9b17SYan, Zheng int dirty;
1213c8fe9b17SYan, Zheng
1214c8fe9b17SYan, Zheng loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
1215c8fe9b17SYan, Zheng if (endoff > i_size_read(inode)) {
1216c8fe9b17SYan, Zheng if (ceph_inode_set_size(inode, endoff))
1217e4b731ccSXiubo Li ceph_check_caps(ci, CHECK_CAPS_AUTHONLY);
1218c8fe9b17SYan, Zheng }
1219c8fe9b17SYan, Zheng
1220c8fe9b17SYan, Zheng spin_lock(&ci->i_ceph_lock);
1221c8fe9b17SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1222c8fe9b17SYan, Zheng &aio_req->prealloc_cf);
1223c8fe9b17SYan, Zheng spin_unlock(&ci->i_ceph_lock);
1224c8fe9b17SYan, Zheng if (dirty)
1225c8fe9b17SYan, Zheng __mark_inode_dirty(inode, dirty);
1226c8fe9b17SYan, Zheng
1227c8fe9b17SYan, Zheng }
1228c8fe9b17SYan, Zheng
1229c8fe9b17SYan, Zheng ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
1230c8fe9b17SYan, Zheng CEPH_CAP_FILE_RD));
1231c8fe9b17SYan, Zheng
12326b19b766SJens Axboe aio_req->iocb->ki_complete(aio_req->iocb, ret);
1233c8fe9b17SYan, Zheng
1234c8fe9b17SYan, Zheng ceph_free_cap_flush(aio_req->prealloc_cf);
1235c8fe9b17SYan, Zheng kfree(aio_req);
1236c8fe9b17SYan, Zheng }
1237c8fe9b17SYan, Zheng
ceph_aio_complete_req(struct ceph_osd_request * req)123885e084feSIlya Dryomov static void ceph_aio_complete_req(struct ceph_osd_request *req)
1239c8fe9b17SYan, Zheng {
1240c8fe9b17SYan, Zheng int rc = req->r_result;
1241c8fe9b17SYan, Zheng struct inode *inode = req->r_inode;
1242c8fe9b17SYan, Zheng struct ceph_aio_request *aio_req = req->r_priv;
1243c8fe9b17SYan, Zheng struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
124403bc06c7SJeff Layton struct ceph_osd_req_op *op = &req->r_ops[0];
12452678da88SXiubo Li struct ceph_client_metric *metric = &ceph_sb_to_mdsc(inode->i_sb)->metric;
1246903f4fecSXiubo Li unsigned int len = osd_data->bvec_pos.iter.bi_size;
124703bc06c7SJeff Layton bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ);
1248c8fe9b17SYan, Zheng
1249fc218544SIlya Dryomov BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_BVECS);
1250fc218544SIlya Dryomov BUG_ON(!osd_data->num_bvecs);
1251fc218544SIlya Dryomov
1252903f4fecSXiubo Li dout("ceph_aio_complete_req %p rc %d bytes %u\n", inode, rc, len);
1253c8fe9b17SYan, Zheng
1254c8fe9b17SYan, Zheng if (rc == -EOLDSNAPC) {
12555be0389dSYan, Zheng struct ceph_aio_work *aio_work;
12565be0389dSYan, Zheng BUG_ON(!aio_req->write);
1257c8fe9b17SYan, Zheng
12585be0389dSYan, Zheng aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
12595be0389dSYan, Zheng if (aio_work) {
12605be0389dSYan, Zheng INIT_WORK(&aio_work->work, ceph_aio_retry_work);
12615be0389dSYan, Zheng aio_work->req = req;
1262985b9ee8SXiubo Li queue_work(ceph_inode_to_fs_client(inode)->inode_wq,
12635be0389dSYan, Zheng &aio_work->work);
12645be0389dSYan, Zheng return;
12655be0389dSYan, Zheng }
12665be0389dSYan, Zheng rc = -ENOMEM;
12675be0389dSYan, Zheng } else if (!aio_req->write) {
126803bc06c7SJeff Layton if (sparse && rc >= 0)
126903bc06c7SJeff Layton rc = ceph_sparse_ext_map_end(op);
1270c8fe9b17SYan, Zheng if (rc == -ENOENT)
1271c8fe9b17SYan, Zheng rc = 0;
1272903f4fecSXiubo Li if (rc >= 0 && len > rc) {
1273fc218544SIlya Dryomov struct iov_iter i;
1274903f4fecSXiubo Li int zlen = len - rc;
1275fc218544SIlya Dryomov
1276c8fe9b17SYan, Zheng /*
1277c8fe9b17SYan, Zheng * If read is satisfied by single OSD request,
1278c8fe9b17SYan, Zheng * it can pass EOF. Otherwise read is within
1279c8fe9b17SYan, Zheng * i_size.
1280c8fe9b17SYan, Zheng */
1281c8fe9b17SYan, Zheng if (aio_req->num_reqs == 1) {
1282c8fe9b17SYan, Zheng loff_t i_size = i_size_read(inode);
1283c8fe9b17SYan, Zheng loff_t endoff = aio_req->iocb->ki_pos + rc;
1284c8fe9b17SYan, Zheng if (endoff < i_size)
1285c8fe9b17SYan, Zheng zlen = min_t(size_t, zlen,
1286c8fe9b17SYan, Zheng i_size - endoff);
1287c8fe9b17SYan, Zheng aio_req->total_len = rc + zlen;
1288c8fe9b17SYan, Zheng }
1289c8fe9b17SYan, Zheng
1290de4eda9dSAl Viro iov_iter_bvec(&i, ITER_DEST, osd_data->bvec_pos.bvecs,
1291903f4fecSXiubo Li osd_data->num_bvecs, len);
1292fc218544SIlya Dryomov iov_iter_advance(&i, rc);
1293fc218544SIlya Dryomov iov_iter_zero(zlen, &i);
1294c8fe9b17SYan, Zheng }
1295c8fe9b17SYan, Zheng }
1296c8fe9b17SYan, Zheng
1297fbd47ddcSXiubo Li /* r_start_latency == 0 means the request was not submitted */
1298fbd47ddcSXiubo Li if (req->r_start_latency) {
1299fbd47ddcSXiubo Li if (aio_req->write)
1300fbd47ddcSXiubo Li ceph_update_write_metrics(metric, req->r_start_latency,
1301903f4fecSXiubo Li req->r_end_latency, len, rc);
1302fbd47ddcSXiubo Li else
1303fbd47ddcSXiubo Li ceph_update_read_metrics(metric, req->r_start_latency,
1304903f4fecSXiubo Li req->r_end_latency, len, rc);
1305fbd47ddcSXiubo Li }
1306fbd47ddcSXiubo Li
1307fc218544SIlya Dryomov put_bvecs(osd_data->bvec_pos.bvecs, osd_data->num_bvecs,
1308fc218544SIlya Dryomov aio_req->should_dirty);
1309c8fe9b17SYan, Zheng ceph_osdc_put_request(req);
1310c8fe9b17SYan, Zheng
1311c8fe9b17SYan, Zheng if (rc < 0)
1312c8fe9b17SYan, Zheng cmpxchg(&aio_req->error, 0, rc);
1313c8fe9b17SYan, Zheng
1314c8fe9b17SYan, Zheng ceph_aio_complete(inode, aio_req);
1315c8fe9b17SYan, Zheng return;
1316c8fe9b17SYan, Zheng }
1317c8fe9b17SYan, Zheng
ceph_aio_retry_work(struct work_struct * work)13185be0389dSYan, Zheng static void ceph_aio_retry_work(struct work_struct *work)
13195be0389dSYan, Zheng {
13205be0389dSYan, Zheng struct ceph_aio_work *aio_work =
13215be0389dSYan, Zheng container_of(work, struct ceph_aio_work, work);
13225be0389dSYan, Zheng struct ceph_osd_request *orig_req = aio_work->req;
13235be0389dSYan, Zheng struct ceph_aio_request *aio_req = orig_req->r_priv;
13245be0389dSYan, Zheng struct inode *inode = orig_req->r_inode;
13255be0389dSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
13265be0389dSYan, Zheng struct ceph_snap_context *snapc;
13275be0389dSYan, Zheng struct ceph_osd_request *req;
13285be0389dSYan, Zheng int ret;
13295be0389dSYan, Zheng
13305be0389dSYan, Zheng spin_lock(&ci->i_ceph_lock);
13315be0389dSYan, Zheng if (__ceph_have_pending_cap_snap(ci)) {
13325be0389dSYan, Zheng struct ceph_cap_snap *capsnap =
13335be0389dSYan, Zheng list_last_entry(&ci->i_cap_snaps,
13345be0389dSYan, Zheng struct ceph_cap_snap,
13355be0389dSYan, Zheng ci_item);
13365be0389dSYan, Zheng snapc = ceph_get_snap_context(capsnap->context);
13375be0389dSYan, Zheng } else {
13385be0389dSYan, Zheng BUG_ON(!ci->i_head_snapc);
13395be0389dSYan, Zheng snapc = ceph_get_snap_context(ci->i_head_snapc);
13405be0389dSYan, Zheng }
13415be0389dSYan, Zheng spin_unlock(&ci->i_ceph_lock);
13425be0389dSYan, Zheng
134361d2f855SIlya Dryomov req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 1,
13445be0389dSYan, Zheng false, GFP_NOFS);
13451418bf07SDan Carpenter if (!req) {
13461418bf07SDan Carpenter ret = -ENOMEM;
13475be0389dSYan, Zheng req = orig_req;
13485be0389dSYan, Zheng goto out;
13495be0389dSYan, Zheng }
13505be0389dSYan, Zheng
1351b178cf43SYan, Zheng req->r_flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
135263244fa1SIlya Dryomov ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
1353d30291b9SIlya Dryomov ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
13545be0389dSYan, Zheng
135526f887e0SIlya Dryomov req->r_ops[0] = orig_req->r_ops[0];
135626f887e0SIlya Dryomov
135726f887e0SIlya Dryomov req->r_mtime = aio_req->mtime;
135826f887e0SIlya Dryomov req->r_data_offset = req->r_ops[0].extent.offset;
135926f887e0SIlya Dryomov
136013d1ad16SIlya Dryomov ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
136113d1ad16SIlya Dryomov if (ret) {
136213d1ad16SIlya Dryomov ceph_osdc_put_request(req);
136313d1ad16SIlya Dryomov req = orig_req;
136413d1ad16SIlya Dryomov goto out;
136513d1ad16SIlya Dryomov }
13665be0389dSYan, Zheng
13675be0389dSYan, Zheng ceph_osdc_put_request(orig_req);
13685be0389dSYan, Zheng
13695be0389dSYan, Zheng req->r_callback = ceph_aio_complete_req;
13705be0389dSYan, Zheng req->r_inode = inode;
13715be0389dSYan, Zheng req->r_priv = aio_req;
13725be0389dSYan, Zheng
1373a8af0d68SJeff Layton ceph_osdc_start_request(req->r_osdc, req);
13745be0389dSYan, Zheng out:
13755be0389dSYan, Zheng if (ret < 0) {
13765be0389dSYan, Zheng req->r_result = ret;
137785e084feSIlya Dryomov ceph_aio_complete_req(req);
13785be0389dSYan, Zheng }
13795be0389dSYan, Zheng
1380db6aed70SYan, Zheng ceph_put_snap_context(snapc);
13815be0389dSYan, Zheng kfree(aio_work);
13825be0389dSYan, Zheng }
13835be0389dSYan, Zheng
1384e8344e66Smajianpeng static ssize_t
ceph_direct_read_write(struct kiocb * iocb,struct iov_iter * iter,struct ceph_snap_context * snapc,struct ceph_cap_flush ** pcf)1385c8fe9b17SYan, Zheng ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
1386c8fe9b17SYan, Zheng struct ceph_snap_context *snapc,
1387c8fe9b17SYan, Zheng struct ceph_cap_flush **pcf)
1388124e68e7SSage Weil {
1389e8344e66Smajianpeng struct file *file = iocb->ki_filp;
1390496ad9aaSAl Viro struct inode *inode = file_inode(file);
1391124e68e7SSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
1392985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
139397e27aaaSXiubo Li struct ceph_client_metric *metric = &fsc->mdsc->metric;
1394acead002SAlex Elder struct ceph_vino vino;
1395124e68e7SSage Weil struct ceph_osd_request *req;
1396fc218544SIlya Dryomov struct bio_vec *bvecs;
1397c8fe9b17SYan, Zheng struct ceph_aio_request *aio_req = NULL;
1398c8fe9b17SYan, Zheng int num_pages = 0;
1399124e68e7SSage Weil int flags;
1400321fe13cSJeff Layton int ret = 0;
1401fac02ddfSArnd Bergmann struct timespec64 mtime = current_time(inode);
1402c8fe9b17SYan, Zheng size_t count = iov_iter_count(iter);
1403c8fe9b17SYan, Zheng loff_t pos = iocb->ki_pos;
1404c8fe9b17SYan, Zheng bool write = iov_iter_rw(iter) == WRITE;
1405fcb14cb1SAl Viro bool should_dirty = !write && user_backed_iter(iter);
140603bc06c7SJeff Layton bool sparse = ceph_test_mount_opt(fsc, SPARSEREAD);
1407124e68e7SSage Weil
1408c8fe9b17SYan, Zheng if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
1409124e68e7SSage Weil return -EROFS;
1410124e68e7SSage Weil
14111c0a9c2dSYan, Zheng dout("sync_direct_%s on file %p %lld~%u snapc %p seq %lld\n",
14121c0a9c2dSYan, Zheng (write ? "write" : "read"), file, pos, (unsigned)count,
141340e7e2c0SJeff Layton snapc, snapc ? snapc->seq : 0);
1414124e68e7SSage Weil
1415c8fe9b17SYan, Zheng if (write) {
1416400e1286SJeff Layton int ret2;
1417400e1286SJeff Layton
1418400e1286SJeff Layton ceph_fscache_invalidate(inode, true);
1419400e1286SJeff Layton
1420400e1286SJeff Layton ret2 = invalidate_inode_pages2_range(inode->i_mapping,
142109cbfeafSKirill A. Shutemov pos >> PAGE_SHIFT,
1422e450f4d1Szhengbin (pos + count - 1) >> PAGE_SHIFT);
14235d7eb1a3SNeilBrown if (ret2 < 0)
1424a380a031SZhi Zhang dout("invalidate_inode_pages2_range returned %d\n", ret2);
142529065a51SYehuda Sadeh
1426b178cf43SYan, Zheng flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
1427c8fe9b17SYan, Zheng } else {
1428c8fe9b17SYan, Zheng flags = CEPH_OSD_FLAG_READ;
1429c8fe9b17SYan, Zheng }
1430124e68e7SSage Weil
1431c8fe9b17SYan, Zheng while (iov_iter_count(iter) > 0) {
1432fc218544SIlya Dryomov u64 size = iov_iter_count(iter);
1433c8fe9b17SYan, Zheng ssize_t len;
143403bc06c7SJeff Layton struct ceph_osd_req_op *op;
143503bc06c7SJeff Layton int readop = sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ;
1436fb98248fSXiubo Li int extent_cnt;
14373a42b6c4SAlex Elder
14383a15b38fSIlya Dryomov if (write)
14393a15b38fSIlya Dryomov size = min_t(u64, size, fsc->mount_options->wsize);
14403a15b38fSIlya Dryomov else
14413a15b38fSIlya Dryomov size = min_t(u64, size, fsc->mount_options->rsize);
14423a15b38fSIlya Dryomov
1443acead002SAlex Elder vino = ceph_vino(inode);
14443d14c5d2SYehuda Sadeh req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1445c8fe9b17SYan, Zheng vino, pos, &size, 0,
14463fb99d48SYanhu Cao 1,
144703bc06c7SJeff Layton write ? CEPH_OSD_OP_WRITE : readop,
1448c8fe9b17SYan, Zheng flags, snapc,
1449e8344e66Smajianpeng ci->i_truncate_seq,
1450e8344e66Smajianpeng ci->i_truncate_size,
1451acead002SAlex Elder false);
1452e8344e66Smajianpeng if (IS_ERR(req)) {
1453e8344e66Smajianpeng ret = PTR_ERR(req);
1454eab87235SAl Viro break;
1455e8344e66Smajianpeng }
1456124e68e7SSage Weil
145744e518abSIlya Dryomov op = &req->r_ops[0];
1458*d41fa58fSIlya Dryomov if (!write && sparse) {
145944e518abSIlya Dryomov extent_cnt = __ceph_sparse_read_ext_count(inode, size);
146044e518abSIlya Dryomov ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
146144e518abSIlya Dryomov if (ret) {
146244e518abSIlya Dryomov ceph_osdc_put_request(req);
146344e518abSIlya Dryomov break;
146444e518abSIlya Dryomov }
146544e518abSIlya Dryomov }
146644e518abSIlya Dryomov
1467fc218544SIlya Dryomov len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages);
1468fc218544SIlya Dryomov if (len < 0) {
146964c31311SAl Viro ceph_osdc_put_request(req);
1470fc218544SIlya Dryomov ret = len;
147164c31311SAl Viro break;
1472124e68e7SSage Weil }
1473fc218544SIlya Dryomov if (len != size)
1474fc218544SIlya Dryomov osd_req_op_extent_update(req, 0, len);
1475124e68e7SSage Weil
147644e518abSIlya Dryomov osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len);
147744e518abSIlya Dryomov
1478124e68e7SSage Weil /*
1479c8fe9b17SYan, Zheng * To simplify error handling, allow AIO when IO within i_size
1480c8fe9b17SYan, Zheng * or IO can be satisfied by single OSD request.
1481c8fe9b17SYan, Zheng */
1482c8fe9b17SYan, Zheng if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
1483c8fe9b17SYan, Zheng (len == count || pos + count <= i_size_read(inode))) {
1484c8fe9b17SYan, Zheng aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
1485c8fe9b17SYan, Zheng if (aio_req) {
1486c8fe9b17SYan, Zheng aio_req->iocb = iocb;
1487c8fe9b17SYan, Zheng aio_req->write = write;
148885784f93SYan, Zheng aio_req->should_dirty = should_dirty;
1489c8fe9b17SYan, Zheng INIT_LIST_HEAD(&aio_req->osd_reqs);
1490c8fe9b17SYan, Zheng if (write) {
14915be0389dSYan, Zheng aio_req->mtime = mtime;
1492c8fe9b17SYan, Zheng swap(aio_req->prealloc_cf, *pcf);
1493c8fe9b17SYan, Zheng }
1494c8fe9b17SYan, Zheng }
1495c8fe9b17SYan, Zheng /* ignore error */
1496c8fe9b17SYan, Zheng }
1497c8fe9b17SYan, Zheng
1498c8fe9b17SYan, Zheng if (write) {
1499c8fe9b17SYan, Zheng /*
1500124e68e7SSage Weil * throw out any page cache pages in this range. this
1501124e68e7SSage Weil * may block.
1502124e68e7SSage Weil */
15035c6a2cdbSSage Weil truncate_inode_pages_range(inode->i_mapping, pos,
1504d31d07b9SLuis Henriques PAGE_ALIGN(pos + len) - 1);
1505c8fe9b17SYan, Zheng
1506bb873b53SIlya Dryomov req->r_mtime = mtime;
1507c8fe9b17SYan, Zheng }
1508c8fe9b17SYan, Zheng
1509c8fe9b17SYan, Zheng if (aio_req) {
1510c8fe9b17SYan, Zheng aio_req->total_len += len;
1511c8fe9b17SYan, Zheng aio_req->num_reqs++;
1512c8fe9b17SYan, Zheng atomic_inc(&aio_req->pending_reqs);
1513c8fe9b17SYan, Zheng
1514c8fe9b17SYan, Zheng req->r_callback = ceph_aio_complete_req;
1515c8fe9b17SYan, Zheng req->r_inode = inode;
1516c8fe9b17SYan, Zheng req->r_priv = aio_req;
151794e85771SIlya Dryomov list_add_tail(&req->r_private_item, &aio_req->osd_reqs);
1518c8fe9b17SYan, Zheng
1519c8fe9b17SYan, Zheng pos += len;
1520c8fe9b17SYan, Zheng continue;
1521c8fe9b17SYan, Zheng }
1522c8fe9b17SYan, Zheng
1523a8af0d68SJeff Layton ceph_osdc_start_request(req->r_osdc, req);
15243d14c5d2SYehuda Sadeh ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1525124e68e7SSage Weil
152697e27aaaSXiubo Li if (write)
15278ae99ae2SXiubo Li ceph_update_write_metrics(metric, req->r_start_latency,
1528903f4fecSXiubo Li req->r_end_latency, len, ret);
152997e27aaaSXiubo Li else
15308ae99ae2SXiubo Li ceph_update_read_metrics(metric, req->r_start_latency,
1531903f4fecSXiubo Li req->r_end_latency, len, ret);
153297e27aaaSXiubo Li
1533c8fe9b17SYan, Zheng size = i_size_read(inode);
1534c8fe9b17SYan, Zheng if (!write) {
153503bc06c7SJeff Layton if (sparse && ret >= 0)
153603bc06c7SJeff Layton ret = ceph_sparse_ext_map_end(op);
153703bc06c7SJeff Layton else if (ret == -ENOENT)
1538c8fe9b17SYan, Zheng ret = 0;
153903bc06c7SJeff Layton
1540c8fe9b17SYan, Zheng if (ret >= 0 && ret < len && pos + ret < size) {
1541fc218544SIlya Dryomov struct iov_iter i;
1542c8fe9b17SYan, Zheng int zlen = min_t(size_t, len - ret,
1543c8fe9b17SYan, Zheng size - pos - ret);
1544fc218544SIlya Dryomov
1545de4eda9dSAl Viro iov_iter_bvec(&i, ITER_DEST, bvecs, num_pages, len);
1546fc218544SIlya Dryomov iov_iter_advance(&i, ret);
1547fc218544SIlya Dryomov iov_iter_zero(zlen, &i);
1548c8fe9b17SYan, Zheng ret += zlen;
1549c8fe9b17SYan, Zheng }
1550c8fe9b17SYan, Zheng if (ret >= 0)
1551c8fe9b17SYan, Zheng len = ret;
1552c8fe9b17SYan, Zheng }
1553c8fe9b17SYan, Zheng
1554fc218544SIlya Dryomov put_bvecs(bvecs, num_pages, should_dirty);
1555124e68e7SSage Weil ceph_osdc_put_request(req);
1556c8fe9b17SYan, Zheng if (ret < 0)
155764c31311SAl Viro break;
1558124e68e7SSage Weil
1559c8fe9b17SYan, Zheng pos += len;
1560c8fe9b17SYan, Zheng if (!write && pos >= size)
1561c8fe9b17SYan, Zheng break;
1562c8fe9b17SYan, Zheng
1563c8fe9b17SYan, Zheng if (write && pos > size) {
1564c8fe9b17SYan, Zheng if (ceph_inode_set_size(inode, pos))
1565e8344e66Smajianpeng ceph_check_caps(ceph_inode(inode),
1566e4b731ccSXiubo Li CHECK_CAPS_AUTHONLY);
1567e8344e66Smajianpeng }
1568e8344e66Smajianpeng }
1569e8344e66Smajianpeng
1570c8fe9b17SYan, Zheng if (aio_req) {
1571fc8c3892SYan, Zheng LIST_HEAD(osd_reqs);
1572fc8c3892SYan, Zheng
1573c8fe9b17SYan, Zheng if (aio_req->num_reqs == 0) {
1574c8fe9b17SYan, Zheng kfree(aio_req);
1575124e68e7SSage Weil return ret;
1576124e68e7SSage Weil }
1577124e68e7SSage Weil
1578c8fe9b17SYan, Zheng ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
1579c8fe9b17SYan, Zheng CEPH_CAP_FILE_RD);
1580c8fe9b17SYan, Zheng
1581fc8c3892SYan, Zheng list_splice(&aio_req->osd_reqs, &osd_reqs);
15826a81749eSJeff Layton inode_dio_begin(inode);
1583fc8c3892SYan, Zheng while (!list_empty(&osd_reqs)) {
1584fc8c3892SYan, Zheng req = list_first_entry(&osd_reqs,
1585c8fe9b17SYan, Zheng struct ceph_osd_request,
158694e85771SIlya Dryomov r_private_item);
158794e85771SIlya Dryomov list_del_init(&req->r_private_item);
1588c8fe9b17SYan, Zheng if (ret >= 0)
1589a8af0d68SJeff Layton ceph_osdc_start_request(req->r_osdc, req);
1590c8fe9b17SYan, Zheng if (ret < 0) {
1591c8fe9b17SYan, Zheng req->r_result = ret;
159285e084feSIlya Dryomov ceph_aio_complete_req(req);
1593c8fe9b17SYan, Zheng }
1594c8fe9b17SYan, Zheng }
1595c8fe9b17SYan, Zheng return -EIOCBQUEUED;
1596c8fe9b17SYan, Zheng }
1597c8fe9b17SYan, Zheng
1598c8fe9b17SYan, Zheng if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
1599c8fe9b17SYan, Zheng ret = pos - iocb->ki_pos;
1600c8fe9b17SYan, Zheng iocb->ki_pos = pos;
1601c8fe9b17SYan, Zheng }
1602c8fe9b17SYan, Zheng return ret;
1603c8fe9b17SYan, Zheng }
1604e8344e66Smajianpeng
1605e8344e66Smajianpeng /*
1606e8344e66Smajianpeng * Synchronous write, straight from __user pointer or user pages.
1607e8344e66Smajianpeng *
1608e8344e66Smajianpeng * If write spans object boundary, just do multiple writes. (For a
1609e8344e66Smajianpeng * correct atomic write, we should e.g. take write locks on all
1610e8344e66Smajianpeng * objects, rollback on failure, etc.)
1611e8344e66Smajianpeng */
161206fee30fSYan, Zheng static ssize_t
ceph_sync_write(struct kiocb * iocb,struct iov_iter * from,loff_t pos,struct ceph_snap_context * snapc)16135dda377cSYan, Zheng ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
16145dda377cSYan, Zheng struct ceph_snap_context *snapc)
1615e8344e66Smajianpeng {
1616e8344e66Smajianpeng struct file *file = iocb->ki_filp;
1617e8344e66Smajianpeng struct inode *inode = file_inode(file);
1618e8344e66Smajianpeng struct ceph_inode_info *ci = ceph_inode(inode);
1619985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
162033a5f170SJeff Layton struct ceph_osd_client *osdc = &fsc->client->osdc;
1621e8344e66Smajianpeng struct ceph_osd_request *req;
1622e8344e66Smajianpeng struct page **pages;
1623e8344e66Smajianpeng u64 len;
1624e8344e66Smajianpeng int num_pages;
1625e8344e66Smajianpeng int written = 0;
1626e8344e66Smajianpeng int ret;
1627efb0ca76SYan, Zheng bool check_caps = false;
1628fac02ddfSArnd Bergmann struct timespec64 mtime = current_time(inode);
16294908b822SAl Viro size_t count = iov_iter_count(from);
1630e8344e66Smajianpeng
1631e8344e66Smajianpeng if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
1632e8344e66Smajianpeng return -EROFS;
1633e8344e66Smajianpeng
16341c0a9c2dSYan, Zheng dout("sync_write on file %p %lld~%u snapc %p seq %lld\n",
16351c0a9c2dSYan, Zheng file, pos, (unsigned)count, snapc, snapc->seq);
1636e8344e66Smajianpeng
1637e450f4d1Szhengbin ret = filemap_write_and_wait_range(inode->i_mapping,
1638e450f4d1Szhengbin pos, pos + count - 1);
1639e8344e66Smajianpeng if (ret < 0)
1640e8344e66Smajianpeng return ret;
1641e8344e66Smajianpeng
1642400e1286SJeff Layton ceph_fscache_invalidate(inode, false);
1643e8344e66Smajianpeng
16444908b822SAl Viro while ((len = iov_iter_count(from)) > 0) {
1645e8344e66Smajianpeng size_t left;
1646e8344e66Smajianpeng int n;
164733a5f170SJeff Layton u64 write_pos = pos;
164833a5f170SJeff Layton u64 write_len = len;
164933a5f170SJeff Layton u64 objnum, objoff;
165033a5f170SJeff Layton u32 xlen;
165133a5f170SJeff Layton u64 assert_ver = 0;
165233a5f170SJeff Layton bool rmw;
165333a5f170SJeff Layton bool first, last;
165433a5f170SJeff Layton struct iov_iter saved_iter = *from;
165533a5f170SJeff Layton size_t off;
1656e8344e66Smajianpeng
165733a5f170SJeff Layton ceph_fscrypt_adjust_off_and_len(inode, &write_pos, &write_len);
165833a5f170SJeff Layton
165933a5f170SJeff Layton /* clamp the length to the end of first object */
166033a5f170SJeff Layton ceph_calc_file_object_mapping(&ci->i_layout, write_pos,
166133a5f170SJeff Layton write_len, &objnum, &objoff,
166233a5f170SJeff Layton &xlen);
166333a5f170SJeff Layton write_len = xlen;
166433a5f170SJeff Layton
166533a5f170SJeff Layton /* adjust len downward if it goes beyond current object */
166633a5f170SJeff Layton if (pos + len > write_pos + write_len)
166733a5f170SJeff Layton len = write_pos + write_len - pos;
166833a5f170SJeff Layton
166933a5f170SJeff Layton /*
167033a5f170SJeff Layton * If we had to adjust the length or position to align with a
167133a5f170SJeff Layton * crypto block, then we must do a read/modify/write cycle. We
167233a5f170SJeff Layton * use a version assertion to redrive the thing if something
167333a5f170SJeff Layton * changes in between.
167433a5f170SJeff Layton */
167533a5f170SJeff Layton first = pos != write_pos;
167633a5f170SJeff Layton last = (pos + len) != (write_pos + write_len);
167733a5f170SJeff Layton rmw = first || last;
167833a5f170SJeff Layton
167933a5f170SJeff Layton dout("sync_write ino %llx %lld~%llu adjusted %lld~%llu -- %srmw\n",
168033a5f170SJeff Layton ci->i_vino.ino, pos, len, write_pos, write_len,
168133a5f170SJeff Layton rmw ? "" : "no ");
168233a5f170SJeff Layton
168333a5f170SJeff Layton /*
168433a5f170SJeff Layton * The data is emplaced into the page as it would be if it were
168533a5f170SJeff Layton * in an array of pagecache pages.
168633a5f170SJeff Layton */
168733a5f170SJeff Layton num_pages = calc_pages_for(write_pos, write_len);
168833a5f170SJeff Layton pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
168933a5f170SJeff Layton if (IS_ERR(pages)) {
169033a5f170SJeff Layton ret = PTR_ERR(pages);
169133a5f170SJeff Layton break;
169233a5f170SJeff Layton }
169333a5f170SJeff Layton
169433a5f170SJeff Layton /* Do we need to preload the pages? */
169533a5f170SJeff Layton if (rmw) {
169633a5f170SJeff Layton u64 first_pos = write_pos;
169733a5f170SJeff Layton u64 last_pos = (write_pos + write_len) - CEPH_FSCRYPT_BLOCK_SIZE;
169833a5f170SJeff Layton u64 read_len = CEPH_FSCRYPT_BLOCK_SIZE;
169933a5f170SJeff Layton struct ceph_osd_req_op *op;
170033a5f170SJeff Layton
170133a5f170SJeff Layton /* We should only need to do this for encrypted inodes */
170233a5f170SJeff Layton WARN_ON_ONCE(!IS_ENCRYPTED(inode));
170333a5f170SJeff Layton
170433a5f170SJeff Layton /* No need to do two reads if first and last blocks are same */
170533a5f170SJeff Layton if (first && last_pos == first_pos)
170633a5f170SJeff Layton last = false;
170733a5f170SJeff Layton
170833a5f170SJeff Layton /*
170933a5f170SJeff Layton * Allocate a read request for one or two extents,
171033a5f170SJeff Layton * depending on how the request was aligned.
171133a5f170SJeff Layton */
171233a5f170SJeff Layton req = ceph_osdc_new_request(osdc, &ci->i_layout,
171333a5f170SJeff Layton ci->i_vino, first ? first_pos : last_pos,
171433a5f170SJeff Layton &read_len, 0, (first && last) ? 2 : 1,
171533a5f170SJeff Layton CEPH_OSD_OP_SPARSE_READ, CEPH_OSD_FLAG_READ,
171633a5f170SJeff Layton NULL, ci->i_truncate_seq,
171733a5f170SJeff Layton ci->i_truncate_size, false);
1718e8344e66Smajianpeng if (IS_ERR(req)) {
171933a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
1720e8344e66Smajianpeng ret = PTR_ERR(req);
1721eab87235SAl Viro break;
1722e8344e66Smajianpeng }
1723e8344e66Smajianpeng
172433a5f170SJeff Layton /* Something is misaligned! */
172533a5f170SJeff Layton if (read_len != CEPH_FSCRYPT_BLOCK_SIZE) {
172633a5f170SJeff Layton ceph_osdc_put_request(req);
172733a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
172833a5f170SJeff Layton ret = -EIO;
172933a5f170SJeff Layton break;
173033a5f170SJeff Layton }
173133a5f170SJeff Layton
173233a5f170SJeff Layton /* Add extent for first block? */
173333a5f170SJeff Layton op = &req->r_ops[0];
173433a5f170SJeff Layton
173533a5f170SJeff Layton if (first) {
173633a5f170SJeff Layton osd_req_op_extent_osd_data_pages(req, 0, pages,
173733a5f170SJeff Layton CEPH_FSCRYPT_BLOCK_SIZE,
173833a5f170SJeff Layton offset_in_page(first_pos),
173933a5f170SJeff Layton false, false);
174033a5f170SJeff Layton /* We only expect a single extent here */
174133a5f170SJeff Layton ret = __ceph_alloc_sparse_ext_map(op, 1);
174233a5f170SJeff Layton if (ret) {
174333a5f170SJeff Layton ceph_osdc_put_request(req);
174433a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
174533a5f170SJeff Layton break;
174633a5f170SJeff Layton }
174733a5f170SJeff Layton }
174833a5f170SJeff Layton
174933a5f170SJeff Layton /* Add extent for last block */
175033a5f170SJeff Layton if (last) {
175133a5f170SJeff Layton /* Init the other extent if first extent has been used */
175233a5f170SJeff Layton if (first) {
175333a5f170SJeff Layton op = &req->r_ops[1];
175433a5f170SJeff Layton osd_req_op_extent_init(req, 1,
175533a5f170SJeff Layton CEPH_OSD_OP_SPARSE_READ,
175633a5f170SJeff Layton last_pos, CEPH_FSCRYPT_BLOCK_SIZE,
175733a5f170SJeff Layton ci->i_truncate_size,
175833a5f170SJeff Layton ci->i_truncate_seq);
175933a5f170SJeff Layton }
176033a5f170SJeff Layton
176133a5f170SJeff Layton ret = __ceph_alloc_sparse_ext_map(op, 1);
176233a5f170SJeff Layton if (ret) {
176333a5f170SJeff Layton ceph_osdc_put_request(req);
176433a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
176533a5f170SJeff Layton break;
176633a5f170SJeff Layton }
176733a5f170SJeff Layton
176833a5f170SJeff Layton osd_req_op_extent_osd_data_pages(req, first ? 1 : 0,
176933a5f170SJeff Layton &pages[num_pages - 1],
177033a5f170SJeff Layton CEPH_FSCRYPT_BLOCK_SIZE,
177133a5f170SJeff Layton offset_in_page(last_pos),
177233a5f170SJeff Layton false, false);
177333a5f170SJeff Layton }
177433a5f170SJeff Layton
177533a5f170SJeff Layton ceph_osdc_start_request(osdc, req);
177633a5f170SJeff Layton ret = ceph_osdc_wait_request(osdc, req);
177733a5f170SJeff Layton
177833a5f170SJeff Layton /* FIXME: length field is wrong if there are 2 extents */
177933a5f170SJeff Layton ceph_update_read_metrics(&fsc->mdsc->metric,
178033a5f170SJeff Layton req->r_start_latency,
178133a5f170SJeff Layton req->r_end_latency,
178233a5f170SJeff Layton read_len, ret);
178333a5f170SJeff Layton
178433a5f170SJeff Layton /* Ok if object is not already present */
178533a5f170SJeff Layton if (ret == -ENOENT) {
178633a5f170SJeff Layton /*
178733a5f170SJeff Layton * If there is no object, then we can't assert
178833a5f170SJeff Layton * on its version. Set it to 0, and we'll use an
178933a5f170SJeff Layton * exclusive create instead.
179033a5f170SJeff Layton */
179133a5f170SJeff Layton ceph_osdc_put_request(req);
179233a5f170SJeff Layton ret = 0;
179333a5f170SJeff Layton
179433a5f170SJeff Layton /*
179533a5f170SJeff Layton * zero out the soon-to-be uncopied parts of the
179633a5f170SJeff Layton * first and last pages.
179733a5f170SJeff Layton */
179833a5f170SJeff Layton if (first)
179933a5f170SJeff Layton zero_user_segment(pages[0], 0,
180033a5f170SJeff Layton offset_in_page(first_pos));
180133a5f170SJeff Layton if (last)
180233a5f170SJeff Layton zero_user_segment(pages[num_pages - 1],
180333a5f170SJeff Layton offset_in_page(last_pos),
180433a5f170SJeff Layton PAGE_SIZE);
180533a5f170SJeff Layton } else {
180633a5f170SJeff Layton if (ret < 0) {
180733a5f170SJeff Layton ceph_osdc_put_request(req);
180833a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
180933a5f170SJeff Layton break;
181033a5f170SJeff Layton }
181133a5f170SJeff Layton
181233a5f170SJeff Layton op = &req->r_ops[0];
181333a5f170SJeff Layton if (op->extent.sparse_ext_cnt == 0) {
181433a5f170SJeff Layton if (first)
181533a5f170SJeff Layton zero_user_segment(pages[0], 0,
181633a5f170SJeff Layton offset_in_page(first_pos));
181733a5f170SJeff Layton else
181833a5f170SJeff Layton zero_user_segment(pages[num_pages - 1],
181933a5f170SJeff Layton offset_in_page(last_pos),
182033a5f170SJeff Layton PAGE_SIZE);
182133a5f170SJeff Layton } else if (op->extent.sparse_ext_cnt != 1 ||
182233a5f170SJeff Layton ceph_sparse_ext_map_end(op) !=
182333a5f170SJeff Layton CEPH_FSCRYPT_BLOCK_SIZE) {
182433a5f170SJeff Layton ret = -EIO;
182533a5f170SJeff Layton ceph_osdc_put_request(req);
182633a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
182733a5f170SJeff Layton break;
182833a5f170SJeff Layton }
182933a5f170SJeff Layton
183033a5f170SJeff Layton if (first && last) {
183133a5f170SJeff Layton op = &req->r_ops[1];
183233a5f170SJeff Layton if (op->extent.sparse_ext_cnt == 0) {
183333a5f170SJeff Layton zero_user_segment(pages[num_pages - 1],
183433a5f170SJeff Layton offset_in_page(last_pos),
183533a5f170SJeff Layton PAGE_SIZE);
183633a5f170SJeff Layton } else if (op->extent.sparse_ext_cnt != 1 ||
183733a5f170SJeff Layton ceph_sparse_ext_map_end(op) !=
183833a5f170SJeff Layton CEPH_FSCRYPT_BLOCK_SIZE) {
183933a5f170SJeff Layton ret = -EIO;
184033a5f170SJeff Layton ceph_osdc_put_request(req);
184133a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
184233a5f170SJeff Layton break;
184333a5f170SJeff Layton }
184433a5f170SJeff Layton }
184533a5f170SJeff Layton
184633a5f170SJeff Layton /* Grab assert version. It must be non-zero. */
184733a5f170SJeff Layton assert_ver = req->r_version;
184833a5f170SJeff Layton WARN_ON_ONCE(ret > 0 && assert_ver == 0);
184933a5f170SJeff Layton
185033a5f170SJeff Layton ceph_osdc_put_request(req);
185133a5f170SJeff Layton if (first) {
185233a5f170SJeff Layton ret = ceph_fscrypt_decrypt_block_inplace(inode,
185333a5f170SJeff Layton pages[0], CEPH_FSCRYPT_BLOCK_SIZE,
185433a5f170SJeff Layton offset_in_page(first_pos),
185533a5f170SJeff Layton first_pos >> CEPH_FSCRYPT_BLOCK_SHIFT);
185633a5f170SJeff Layton if (ret < 0) {
185733a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
185833a5f170SJeff Layton break;
185933a5f170SJeff Layton }
186033a5f170SJeff Layton }
186133a5f170SJeff Layton if (last) {
186233a5f170SJeff Layton ret = ceph_fscrypt_decrypt_block_inplace(inode,
186333a5f170SJeff Layton pages[num_pages - 1],
186433a5f170SJeff Layton CEPH_FSCRYPT_BLOCK_SIZE,
186533a5f170SJeff Layton offset_in_page(last_pos),
186633a5f170SJeff Layton last_pos >> CEPH_FSCRYPT_BLOCK_SHIFT);
186733a5f170SJeff Layton if (ret < 0) {
186833a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
186933a5f170SJeff Layton break;
187033a5f170SJeff Layton }
187133a5f170SJeff Layton }
187233a5f170SJeff Layton }
1873e8344e66Smajianpeng }
1874e8344e66Smajianpeng
1875e8344e66Smajianpeng left = len;
1876b294fa29SJeff Layton off = offset_in_page(pos);
1877e8344e66Smajianpeng for (n = 0; n < num_pages; n++) {
1878b294fa29SJeff Layton size_t plen = min_t(size_t, left, PAGE_SIZE - off);
1879b294fa29SJeff Layton
188033a5f170SJeff Layton /* copy the data */
1881b294fa29SJeff Layton ret = copy_page_from_iter(pages[n], off, plen, from);
1882e8344e66Smajianpeng if (ret != plen) {
1883e8344e66Smajianpeng ret = -EFAULT;
1884e8344e66Smajianpeng break;
1885e8344e66Smajianpeng }
188633a5f170SJeff Layton off = 0;
1887e8344e66Smajianpeng left -= ret;
1888e8344e66Smajianpeng }
1889e8344e66Smajianpeng if (ret < 0) {
189033a5f170SJeff Layton dout("sync_write write failed with %d\n", ret);
1891e8344e66Smajianpeng ceph_release_page_vector(pages, num_pages);
189233a5f170SJeff Layton break;
1893e8344e66Smajianpeng }
1894e8344e66Smajianpeng
189533a5f170SJeff Layton if (IS_ENCRYPTED(inode)) {
189633a5f170SJeff Layton ret = ceph_fscrypt_encrypt_pages(inode, pages,
189733a5f170SJeff Layton write_pos, write_len,
189833a5f170SJeff Layton GFP_KERNEL);
189933a5f170SJeff Layton if (ret < 0) {
190033a5f170SJeff Layton dout("encryption failed with %d\n", ret);
190133a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
190233a5f170SJeff Layton break;
190333a5f170SJeff Layton }
190433a5f170SJeff Layton }
190533a5f170SJeff Layton
190633a5f170SJeff Layton req = ceph_osdc_new_request(osdc, &ci->i_layout,
190733a5f170SJeff Layton ci->i_vino, write_pos, &write_len,
190833a5f170SJeff Layton rmw ? 1 : 0, rmw ? 2 : 1,
190933a5f170SJeff Layton CEPH_OSD_OP_WRITE,
191033a5f170SJeff Layton CEPH_OSD_FLAG_WRITE,
191133a5f170SJeff Layton snapc, ci->i_truncate_seq,
191233a5f170SJeff Layton ci->i_truncate_size, false);
191333a5f170SJeff Layton if (IS_ERR(req)) {
191433a5f170SJeff Layton ret = PTR_ERR(req);
191533a5f170SJeff Layton ceph_release_page_vector(pages, num_pages);
191633a5f170SJeff Layton break;
191733a5f170SJeff Layton }
191833a5f170SJeff Layton
191933a5f170SJeff Layton dout("sync_write write op %lld~%llu\n", write_pos, write_len);
192033a5f170SJeff Layton osd_req_op_extent_osd_data_pages(req, rmw ? 1 : 0, pages, write_len,
192133a5f170SJeff Layton offset_in_page(write_pos), false,
192233a5f170SJeff Layton true);
1923e8344e66Smajianpeng req->r_inode = inode;
1924bb873b53SIlya Dryomov req->r_mtime = mtime;
192533a5f170SJeff Layton
192633a5f170SJeff Layton /* Set up the assertion */
192733a5f170SJeff Layton if (rmw) {
192833a5f170SJeff Layton /*
192933a5f170SJeff Layton * Set up the assertion. If we don't have a version
193033a5f170SJeff Layton * number, then the object doesn't exist yet. Use an
193133a5f170SJeff Layton * exclusive create instead of a version assertion in
193233a5f170SJeff Layton * that case.
193333a5f170SJeff Layton */
193433a5f170SJeff Layton if (assert_ver) {
193533a5f170SJeff Layton osd_req_op_init(req, 0, CEPH_OSD_OP_ASSERT_VER, 0);
193633a5f170SJeff Layton req->r_ops[0].assert_ver.ver = assert_ver;
193733a5f170SJeff Layton } else {
193833a5f170SJeff Layton osd_req_op_init(req, 0, CEPH_OSD_OP_CREATE,
193933a5f170SJeff Layton CEPH_OSD_OP_FLAG_EXCL);
194033a5f170SJeff Layton }
194133a5f170SJeff Layton }
194233a5f170SJeff Layton
194333a5f170SJeff Layton ceph_osdc_start_request(osdc, req);
194433a5f170SJeff Layton ret = ceph_osdc_wait_request(osdc, req);
1945e8344e66Smajianpeng
19468ae99ae2SXiubo Li ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
1947903f4fecSXiubo Li req->r_end_latency, len, ret);
1948e8344e66Smajianpeng ceph_osdc_put_request(req);
194926544c62SJeff Layton if (ret != 0) {
195033a5f170SJeff Layton dout("sync_write osd write returned %d\n", ret);
195133a5f170SJeff Layton /* Version changed! Must re-do the rmw cycle */
195233a5f170SJeff Layton if ((assert_ver && (ret == -ERANGE || ret == -EOVERFLOW)) ||
195333a5f170SJeff Layton (!assert_ver && ret == -EEXIST)) {
195433a5f170SJeff Layton /* We should only ever see this on a rmw */
195533a5f170SJeff Layton WARN_ON_ONCE(!rmw);
195633a5f170SJeff Layton
195733a5f170SJeff Layton /* The version should never go backward */
195833a5f170SJeff Layton WARN_ON_ONCE(ret == -EOVERFLOW);
195933a5f170SJeff Layton
196033a5f170SJeff Layton *from = saved_iter;
196133a5f170SJeff Layton
196233a5f170SJeff Layton /* FIXME: limit number of times we loop? */
196333a5f170SJeff Layton continue;
196433a5f170SJeff Layton }
196526544c62SJeff Layton ceph_set_error_write(ci);
196626544c62SJeff Layton break;
196726544c62SJeff Layton }
196826544c62SJeff Layton
196926544c62SJeff Layton ceph_clear_error_write(ci);
1970b422f115SLuís Henriques
1971b422f115SLuís Henriques /*
1972b422f115SLuís Henriques * We successfully wrote to a range of the file. Declare
1973b422f115SLuís Henriques * that region of the pagecache invalid.
1974b422f115SLuís Henriques */
1975b422f115SLuís Henriques ret = invalidate_inode_pages2_range(
1976b422f115SLuís Henriques inode->i_mapping,
1977b422f115SLuís Henriques pos >> PAGE_SHIFT,
1978b422f115SLuís Henriques (pos + len - 1) >> PAGE_SHIFT);
1979b422f115SLuís Henriques if (ret < 0) {
1980b422f115SLuís Henriques dout("invalidate_inode_pages2_range returned %d\n",
1981b422f115SLuís Henriques ret);
1982b422f115SLuís Henriques ret = 0;
1983b422f115SLuís Henriques }
1984e8344e66Smajianpeng pos += len;
1985e8344e66Smajianpeng written += len;
198633a5f170SJeff Layton dout("sync_write written %d\n", written);
1987e8344e66Smajianpeng if (pos > i_size_read(inode)) {
1988e8344e66Smajianpeng check_caps = ceph_inode_set_size(inode, pos);
1989e8344e66Smajianpeng if (check_caps)
1990e8344e66Smajianpeng ceph_check_caps(ceph_inode(inode),
1991e4b731ccSXiubo Li CHECK_CAPS_AUTHONLY);
1992e8344e66Smajianpeng }
199326544c62SJeff Layton
1994e8344e66Smajianpeng }
1995e8344e66Smajianpeng
1996e8344e66Smajianpeng if (ret != -EOLDSNAPC && written > 0) {
1997e8344e66Smajianpeng ret = written;
1998e8344e66Smajianpeng iocb->ki_pos = pos;
1999e8344e66Smajianpeng }
200033a5f170SJeff Layton dout("sync_write returning %d\n", ret);
2001e8344e66Smajianpeng return ret;
2002e8344e66Smajianpeng }
2003e8344e66Smajianpeng
2004124e68e7SSage Weil /*
2005124e68e7SSage Weil * Wrap generic_file_aio_read with checks for cap bits on the inode.
2006124e68e7SSage Weil * Atomically grab references, so that those bits are not released
2007124e68e7SSage Weil * back to the MDS mid-read.
2008124e68e7SSage Weil *
2009124e68e7SSage Weil * Hmm, the sync read case isn't actually async... should it be?
2010124e68e7SSage Weil */
ceph_read_iter(struct kiocb * iocb,struct iov_iter * to)20113644424dSAl Viro static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
2012124e68e7SSage Weil {
2013124e68e7SSage Weil struct file *filp = iocb->ki_filp;
20142962507cSSage Weil struct ceph_file_info *fi = filp->private_data;
201566ee59afSChristoph Hellwig size_t len = iov_iter_count(to);
2016496ad9aaSAl Viro struct inode *inode = file_inode(filp);
2017124e68e7SSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
2018d1d96550SXiubo Li bool direct_lock = iocb->ki_flags & IOCB_DIRECT;
2019124e68e7SSage Weil ssize_t ret;
202094cc0877SJeff Layton int want = 0, got = 0;
202183701246SYan, Zheng int retry_op = 0, read = 0;
2022124e68e7SSage Weil
20236a026589SSage Weil again:
20248eb4efb0Smajianpeng dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
20258eb4efb0Smajianpeng inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
20268eb4efb0Smajianpeng
20275d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode))
20285d6451b1SJeff Layton return -ESTALE;
20295d6451b1SJeff Layton
2030d1d96550SXiubo Li if (direct_lock)
2031a81bc310SJeff Layton ceph_start_io_direct(inode);
2032a81bc310SJeff Layton else
2033a81bc310SJeff Layton ceph_start_io_read(inode);
2034a81bc310SJeff Layton
203594cc0877SJeff Layton if (!(fi->flags & CEPH_F_SYNC) && !direct_lock)
203694cc0877SJeff Layton want |= CEPH_CAP_FILE_CACHE;
20372962507cSSage Weil if (fi->fmode & CEPH_FILE_MODE_LAZY)
203894cc0877SJeff Layton want |= CEPH_CAP_FILE_LAZYIO;
203994cc0877SJeff Layton
2040e72968e1SJeff Layton ret = ceph_get_caps(filp, CEPH_CAP_FILE_RD, want, -1, &got);
2041a81bc310SJeff Layton if (ret < 0) {
204294cc0877SJeff Layton if (direct_lock)
2043a81bc310SJeff Layton ceph_end_io_direct(inode);
2044a81bc310SJeff Layton else
2045a81bc310SJeff Layton ceph_end_io_read(inode);
20468eb4efb0Smajianpeng return ret;
2047a81bc310SJeff Layton }
20488eb4efb0Smajianpeng
20498eb4efb0Smajianpeng if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
20502ba48ce5SAl Viro (iocb->ki_flags & IOCB_DIRECT) ||
20518eb4efb0Smajianpeng (fi->flags & CEPH_F_SYNC)) {
20528eb4efb0Smajianpeng
20538eb4efb0Smajianpeng dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
20548eb4efb0Smajianpeng inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
20558eb4efb0Smajianpeng ceph_cap_string(got));
20568eb4efb0Smajianpeng
205748490776SXiubo Li if (!ceph_has_inline_data(ci)) {
20588cff8f53SJeff Layton if (!retry_op &&
20598cff8f53SJeff Layton (iocb->ki_flags & IOCB_DIRECT) &&
20608cff8f53SJeff Layton !IS_ENCRYPTED(inode)) {
2061c8fe9b17SYan, Zheng ret = ceph_direct_read_write(iocb, to,
2062c8fe9b17SYan, Zheng NULL, NULL);
2063c8fe9b17SYan, Zheng if (ret >= 0 && ret < len)
2064c8fe9b17SYan, Zheng retry_op = CHECK_EOF;
2065c8fe9b17SYan, Zheng } else {
206683701246SYan, Zheng ret = ceph_sync_read(iocb, to, &retry_op);
2067c8fe9b17SYan, Zheng }
206883701246SYan, Zheng } else {
206983701246SYan, Zheng retry_op = READ_INLINE;
207083701246SYan, Zheng }
20718eb4efb0Smajianpeng } else {
20725d988308SYan, Zheng CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
2073124e68e7SSage Weil dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
20743644424dSAl Viro inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
2075124e68e7SSage Weil ceph_cap_string(got));
20765d988308SYan, Zheng ceph_add_rw_context(fi, &rw_ctx);
20773644424dSAl Viro ret = generic_file_read_iter(iocb, to);
20785d988308SYan, Zheng ceph_del_rw_context(fi, &rw_ctx);
20798eb4efb0Smajianpeng }
2080a81bc310SJeff Layton
2081124e68e7SSage Weil dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
2082124e68e7SSage Weil inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
2083124e68e7SSage Weil ceph_put_cap_refs(ci, got);
2084a81bc310SJeff Layton
2085d1d96550SXiubo Li if (direct_lock)
2086a81bc310SJeff Layton ceph_end_io_direct(inode);
2087a81bc310SJeff Layton else
2088a81bc310SJeff Layton ceph_end_io_read(inode);
2089a81bc310SJeff Layton
2090c8fe9b17SYan, Zheng if (retry_op > HAVE_RETRIED && ret >= 0) {
209183701246SYan, Zheng int statret;
209283701246SYan, Zheng struct page *page = NULL;
209383701246SYan, Zheng loff_t i_size;
209483701246SYan, Zheng if (retry_op == READ_INLINE) {
2095687265e5SYan, Zheng page = __page_cache_alloc(GFP_KERNEL);
209683701246SYan, Zheng if (!page)
209783701246SYan, Zheng return -ENOMEM;
209883701246SYan, Zheng }
20996a026589SSage Weil
210083701246SYan, Zheng statret = __ceph_do_getattr(inode, page,
210183701246SYan, Zheng CEPH_STAT_CAP_INLINE_DATA, !!page);
210283701246SYan, Zheng if (statret < 0) {
21030d7718f6SNikolay Borisov if (page)
210483701246SYan, Zheng __free_page(page);
210583701246SYan, Zheng if (statret == -ENODATA) {
210683701246SYan, Zheng BUG_ON(retry_op != READ_INLINE);
210783701246SYan, Zheng goto again;
210883701246SYan, Zheng }
210983701246SYan, Zheng return statret;
211083701246SYan, Zheng }
211183701246SYan, Zheng
211283701246SYan, Zheng i_size = i_size_read(inode);
211383701246SYan, Zheng if (retry_op == READ_INLINE) {
2114fcc02d2aSYan, Zheng BUG_ON(ret > 0 || read > 0);
2115fcc02d2aSYan, Zheng if (iocb->ki_pos < i_size &&
211609cbfeafSKirill A. Shutemov iocb->ki_pos < PAGE_SIZE) {
211783701246SYan, Zheng loff_t end = min_t(loff_t, i_size,
211883701246SYan, Zheng iocb->ki_pos + len);
211909cbfeafSKirill A. Shutemov end = min_t(loff_t, end, PAGE_SIZE);
212083701246SYan, Zheng if (statret < end)
212183701246SYan, Zheng zero_user_segment(page, statret, end);
212283701246SYan, Zheng ret = copy_page_to_iter(page,
212383701246SYan, Zheng iocb->ki_pos & ~PAGE_MASK,
212483701246SYan, Zheng end - iocb->ki_pos, to);
212583701246SYan, Zheng iocb->ki_pos += ret;
2126fcc02d2aSYan, Zheng read += ret;
2127fcc02d2aSYan, Zheng }
2128fcc02d2aSYan, Zheng if (iocb->ki_pos < i_size && read < len) {
2129fcc02d2aSYan, Zheng size_t zlen = min_t(size_t, len - read,
2130fcc02d2aSYan, Zheng i_size - iocb->ki_pos);
2131fcc02d2aSYan, Zheng ret = iov_iter_zero(zlen, to);
2132fcc02d2aSYan, Zheng iocb->ki_pos += ret;
2133fcc02d2aSYan, Zheng read += ret;
213483701246SYan, Zheng }
213583701246SYan, Zheng __free_pages(page, 0);
2136fcc02d2aSYan, Zheng return read;
213783701246SYan, Zheng }
21386a026589SSage Weil
21396a026589SSage Weil /* hit EOF or hole? */
214083701246SYan, Zheng if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
21418eb4efb0Smajianpeng ret < len) {
21428eb4efb0Smajianpeng dout("sync_read hit hole, ppos %lld < size %lld"
214399c88e69SYan, Zheng ", reading more\n", iocb->ki_pos, i_size);
21448eb4efb0Smajianpeng
21456a026589SSage Weil read += ret;
21466a026589SSage Weil len -= ret;
2147c8fe9b17SYan, Zheng retry_op = HAVE_RETRIED;
21486a026589SSage Weil goto again;
21496a026589SSage Weil }
21506a026589SSage Weil }
21518eb4efb0Smajianpeng
21526a026589SSage Weil if (ret >= 0)
21536a026589SSage Weil ret += read;
21546a026589SSage Weil
2155124e68e7SSage Weil return ret;
2156124e68e7SSage Weil }
2157124e68e7SSage Weil
2158124e68e7SSage Weil /*
2159ccfdf7cbSDavid Howells * Wrap filemap_splice_read with checks for cap bits on the inode.
2160ccfdf7cbSDavid Howells * Atomically grab references, so that those bits are not released
2161ccfdf7cbSDavid Howells * back to the MDS mid-read.
2162ccfdf7cbSDavid Howells */
ceph_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)2163ccfdf7cbSDavid Howells static ssize_t ceph_splice_read(struct file *in, loff_t *ppos,
2164ccfdf7cbSDavid Howells struct pipe_inode_info *pipe,
2165ccfdf7cbSDavid Howells size_t len, unsigned int flags)
2166ccfdf7cbSDavid Howells {
2167ccfdf7cbSDavid Howells struct ceph_file_info *fi = in->private_data;
2168ccfdf7cbSDavid Howells struct inode *inode = file_inode(in);
2169ccfdf7cbSDavid Howells struct ceph_inode_info *ci = ceph_inode(inode);
2170ccfdf7cbSDavid Howells ssize_t ret;
2171ccfdf7cbSDavid Howells int want = 0, got = 0;
2172ccfdf7cbSDavid Howells CEPH_DEFINE_RW_CONTEXT(rw_ctx, 0);
2173ccfdf7cbSDavid Howells
2174ccfdf7cbSDavid Howells dout("splice_read %p %llx.%llx %llu~%zu trying to get caps on %p\n",
2175ccfdf7cbSDavid Howells inode, ceph_vinop(inode), *ppos, len, inode);
2176ccfdf7cbSDavid Howells
2177ccfdf7cbSDavid Howells if (ceph_inode_is_shutdown(inode))
2178ccfdf7cbSDavid Howells return -ESTALE;
2179ccfdf7cbSDavid Howells
2180ccfdf7cbSDavid Howells if (ceph_has_inline_data(ci) ||
2181ccfdf7cbSDavid Howells (fi->flags & CEPH_F_SYNC))
2182ccfdf7cbSDavid Howells return copy_splice_read(in, ppos, pipe, len, flags);
2183ccfdf7cbSDavid Howells
2184ccfdf7cbSDavid Howells ceph_start_io_read(inode);
2185ccfdf7cbSDavid Howells
2186ccfdf7cbSDavid Howells want = CEPH_CAP_FILE_CACHE;
2187ccfdf7cbSDavid Howells if (fi->fmode & CEPH_FILE_MODE_LAZY)
2188ccfdf7cbSDavid Howells want |= CEPH_CAP_FILE_LAZYIO;
2189ccfdf7cbSDavid Howells
2190ccfdf7cbSDavid Howells ret = ceph_get_caps(in, CEPH_CAP_FILE_RD, want, -1, &got);
2191ccfdf7cbSDavid Howells if (ret < 0)
2192ccfdf7cbSDavid Howells goto out_end;
2193ccfdf7cbSDavid Howells
2194ccfdf7cbSDavid Howells if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) == 0) {
2195ccfdf7cbSDavid Howells dout("splice_read/sync %p %llx.%llx %llu~%zu got cap refs on %s\n",
2196ccfdf7cbSDavid Howells inode, ceph_vinop(inode), *ppos, len,
2197ccfdf7cbSDavid Howells ceph_cap_string(got));
2198ccfdf7cbSDavid Howells
2199ccfdf7cbSDavid Howells ceph_put_cap_refs(ci, got);
2200ccfdf7cbSDavid Howells ceph_end_io_read(inode);
2201ccfdf7cbSDavid Howells return copy_splice_read(in, ppos, pipe, len, flags);
2202ccfdf7cbSDavid Howells }
2203ccfdf7cbSDavid Howells
2204ccfdf7cbSDavid Howells dout("splice_read %p %llx.%llx %llu~%zu got cap refs on %s\n",
2205ccfdf7cbSDavid Howells inode, ceph_vinop(inode), *ppos, len, ceph_cap_string(got));
2206ccfdf7cbSDavid Howells
2207ccfdf7cbSDavid Howells rw_ctx.caps = got;
2208ccfdf7cbSDavid Howells ceph_add_rw_context(fi, &rw_ctx);
2209ccfdf7cbSDavid Howells ret = filemap_splice_read(in, ppos, pipe, len, flags);
2210ccfdf7cbSDavid Howells ceph_del_rw_context(fi, &rw_ctx);
2211ccfdf7cbSDavid Howells
2212ccfdf7cbSDavid Howells dout("splice_read %p %llx.%llx dropping cap refs on %s = %zd\n",
2213ccfdf7cbSDavid Howells inode, ceph_vinop(inode), ceph_cap_string(got), ret);
2214ccfdf7cbSDavid Howells
2215ccfdf7cbSDavid Howells ceph_put_cap_refs(ci, got);
2216ccfdf7cbSDavid Howells out_end:
2217ccfdf7cbSDavid Howells ceph_end_io_read(inode);
2218ccfdf7cbSDavid Howells return ret;
2219ccfdf7cbSDavid Howells }
2220ccfdf7cbSDavid Howells
2221ccfdf7cbSDavid Howells /*
2222124e68e7SSage Weil * Take cap references to avoid releasing caps to MDS mid-write.
2223124e68e7SSage Weil *
2224124e68e7SSage Weil * If we are synchronous, and write with an old snap context, the OSD
2225124e68e7SSage Weil * may return EOLDSNAPC. In that case, retry the write.. _after_
2226124e68e7SSage Weil * dropping our cap refs and allowing the pending snap to logically
2227124e68e7SSage Weil * complete _before_ this write occurs.
2228124e68e7SSage Weil *
2229124e68e7SSage Weil * If we are near ENOSPC, write synchronously.
2230124e68e7SSage Weil */
ceph_write_iter(struct kiocb * iocb,struct iov_iter * from)22314908b822SAl Viro static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
2232124e68e7SSage Weil {
2233124e68e7SSage Weil struct file *file = iocb->ki_filp;
223433caad32SSage Weil struct ceph_file_info *fi = file->private_data;
2235496ad9aaSAl Viro struct inode *inode = file_inode(file);
2236124e68e7SSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
2237985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
223876142097SIlya Dryomov struct ceph_osd_client *osdc = &fsc->client->osdc;
2239f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf;
22403309dd04SAl Viro ssize_t count, written = 0;
224194cc0877SJeff Layton int err, want = 0, got;
22428e4473bbSXiubo Li bool direct_lock = false;
224376142097SIlya Dryomov u32 map_flags;
224476142097SIlya Dryomov u64 pool_flags;
22453309dd04SAl Viro loff_t pos;
22468687a3e2SChengguang Xu loff_t limit = max(i_size_read(inode), fsc->max_file_size);
2247124e68e7SSage Weil
22485d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode))
22495d6451b1SJeff Layton return -ESTALE;
22505d6451b1SJeff Layton
2251124e68e7SSage Weil if (ceph_snap(inode) != CEPH_NOSNAP)
2252124e68e7SSage Weil return -EROFS;
2253124e68e7SSage Weil
2254f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush();
2255f66fd9f0SYan, Zheng if (!prealloc_cf)
2256f66fd9f0SYan, Zheng return -ENOMEM;
2257f66fd9f0SYan, Zheng
22588e4473bbSXiubo Li if ((iocb->ki_flags & (IOCB_DIRECT | IOCB_APPEND)) == IOCB_DIRECT)
22598e4473bbSXiubo Li direct_lock = true;
22608e4473bbSXiubo Li
2261a5cd74adSYan, Zheng retry_snap:
22628e4473bbSXiubo Li if (direct_lock)
2263321fe13cSJeff Layton ceph_start_io_direct(inode);
2264321fe13cSJeff Layton else
2265321fe13cSJeff Layton ceph_start_io_write(inode);
226603d254edSYan, Zheng
226755b0b31cSYan, Zheng if (iocb->ki_flags & IOCB_APPEND) {
226855b0b31cSYan, Zheng err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
226955b0b31cSYan, Zheng if (err < 0)
227055b0b31cSYan, Zheng goto out;
227155b0b31cSYan, Zheng }
227255b0b31cSYan, Zheng
22733309dd04SAl Viro err = generic_write_checks(iocb, from);
22743309dd04SAl Viro if (err <= 0)
227503d254edSYan, Zheng goto out;
227603d254edSYan, Zheng
22773309dd04SAl Viro pos = iocb->ki_pos;
22788687a3e2SChengguang Xu if (unlikely(pos >= limit)) {
22798687a3e2SChengguang Xu err = -EFBIG;
22808687a3e2SChengguang Xu goto out;
22818687a3e2SChengguang Xu } else {
22828687a3e2SChengguang Xu iov_iter_truncate(from, limit - pos);
22838687a3e2SChengguang Xu }
22848687a3e2SChengguang Xu
22853309dd04SAl Viro count = iov_iter_count(from);
22862b83845fSLuis Henriques if (ceph_quota_is_max_bytes_exceeded(inode, pos + count)) {
22872b83845fSLuis Henriques err = -EDQUOT;
22882b83845fSLuis Henriques goto out;
22892b83845fSLuis Henriques }
22902b83845fSLuis Henriques
229176142097SIlya Dryomov down_read(&osdc->lock);
229276142097SIlya Dryomov map_flags = osdc->osdmap->flags;
229376142097SIlya Dryomov pool_flags = ceph_pg_pool_flags(osdc->osdmap, ci->i_layout.pool_id);
229476142097SIlya Dryomov up_read(&osdc->lock);
229576142097SIlya Dryomov if ((map_flags & CEPH_OSDMAP_FULL) ||
229676142097SIlya Dryomov (pool_flags & CEPH_POOL_FLAG_FULL)) {
229703d254edSYan, Zheng err = -ENOSPC;
22986070e0c1SYan, Zheng goto out;
22996070e0c1SYan, Zheng }
230003d254edSYan, Zheng
2301b11ed503SJeff Layton err = file_remove_privs(file);
2302b11ed503SJeff Layton if (err)
2303b11ed503SJeff Layton goto out;
2304b11ed503SJeff Layton
2305ac7f29bfSRandy Dunlap dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
230699c88e69SYan, Zheng inode, ceph_vinop(inode), pos, count, i_size_read(inode));
230794cc0877SJeff Layton if (!(fi->flags & CEPH_F_SYNC) && !direct_lock)
230894cc0877SJeff Layton want |= CEPH_CAP_FILE_BUFFER;
23097971bd92SSage Weil if (fi->fmode & CEPH_FILE_MODE_LAZY)
231094cc0877SJeff Layton want |= CEPH_CAP_FILE_LAZYIO;
231103d254edSYan, Zheng got = 0;
2312e72968e1SJeff Layton err = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, pos + count, &got);
231303d254edSYan, Zheng if (err < 0)
231437505d57SYan, Zheng goto out;
2315124e68e7SSage Weil
2316b11ed503SJeff Layton err = file_update_time(file);
2317b11ed503SJeff Layton if (err)
2318b11ed503SJeff Layton goto out_caps;
2319b11ed503SJeff Layton
2320b11ed503SJeff Layton inode_inc_iversion_raw(inode);
2321b11ed503SJeff Layton
2322ac7f29bfSRandy Dunlap dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
232303d254edSYan, Zheng inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
23247971bd92SSage Weil
23257971bd92SSage Weil if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
232626544c62SJeff Layton (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC) ||
232726544c62SJeff Layton (ci->i_ceph_flags & CEPH_I_ERROR_WRITE)) {
23285dda377cSYan, Zheng struct ceph_snap_context *snapc;
23294908b822SAl Viro struct iov_iter data;
23305dda377cSYan, Zheng
23315dda377cSYan, Zheng spin_lock(&ci->i_ceph_lock);
23325dda377cSYan, Zheng if (__ceph_have_pending_cap_snap(ci)) {
23335dda377cSYan, Zheng struct ceph_cap_snap *capsnap =
23345dda377cSYan, Zheng list_last_entry(&ci->i_cap_snaps,
23355dda377cSYan, Zheng struct ceph_cap_snap,
23365dda377cSYan, Zheng ci_item);
23375dda377cSYan, Zheng snapc = ceph_get_snap_context(capsnap->context);
23385dda377cSYan, Zheng } else {
23395dda377cSYan, Zheng BUG_ON(!ci->i_head_snapc);
23405dda377cSYan, Zheng snapc = ceph_get_snap_context(ci->i_head_snapc);
23415dda377cSYan, Zheng }
23425dda377cSYan, Zheng spin_unlock(&ci->i_ceph_lock);
23435dda377cSYan, Zheng
23444908b822SAl Viro /* we might need to revert back to that point */
23454908b822SAl Viro data = *from;
23468cff8f53SJeff Layton if ((iocb->ki_flags & IOCB_DIRECT) && !IS_ENCRYPTED(inode))
2347c8fe9b17SYan, Zheng written = ceph_direct_read_write(iocb, &data, snapc,
2348c8fe9b17SYan, Zheng &prealloc_cf);
23498e4473bbSXiubo Li else
23505dda377cSYan, Zheng written = ceph_sync_write(iocb, &data, pos, snapc);
23518e4473bbSXiubo Li if (direct_lock)
23528e4473bbSXiubo Li ceph_end_io_direct(inode);
23538e4473bbSXiubo Li else
2354321fe13cSJeff Layton ceph_end_io_write(inode);
23554908b822SAl Viro if (written > 0)
23564908b822SAl Viro iov_iter_advance(from, written);
23575dda377cSYan, Zheng ceph_put_snap_context(snapc);
23587971bd92SSage Weil } else {
2359b0d7c223SYan, Zheng /*
2360b0d7c223SYan, Zheng * No need to acquire the i_truncate_mutex. Because
2361b0d7c223SYan, Zheng * the MDS revokes Fwb caps before sending truncate
2362b0d7c223SYan, Zheng * message to us. We can't get Fwb cap while there
2363b0d7c223SYan, Zheng * are pending vmtruncate. So write and vmtruncate
2364b0d7c223SYan, Zheng * can not run at the same time
2365b0d7c223SYan, Zheng */
2366800ba295SMatthew Wilcox (Oracle) written = generic_perform_write(iocb, from);
2367321fe13cSJeff Layton ceph_end_io_write(inode);
2368124e68e7SSage Weil }
2369d8de9ab6SSage Weil
237003d254edSYan, Zheng if (written >= 0) {
2371fca65b4aSSage Weil int dirty;
23721ab302a0SLuis Henriques
2373be655596SSage Weil spin_lock(&ci->i_ceph_lock);
2374f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
2375f66fd9f0SYan, Zheng &prealloc_cf);
2376be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
2377fca65b4aSSage Weil if (dirty)
2378fca65b4aSSage Weil __mark_inode_dirty(inode, dirty);
23791ab302a0SLuis Henriques if (ceph_quota_is_max_bytes_approaching(inode, iocb->ki_pos))
2380e4b731ccSXiubo Li ceph_check_caps(ci, CHECK_CAPS_FLUSH);
2381124e68e7SSage Weil }
23827971bd92SSage Weil
2383124e68e7SSage Weil dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
23844908b822SAl Viro inode, ceph_vinop(inode), pos, (unsigned)count,
23857971bd92SSage Weil ceph_cap_string(got));
2386124e68e7SSage Weil ceph_put_cap_refs(ci, got);
23877971bd92SSage Weil
2388a5cd74adSYan, Zheng if (written == -EOLDSNAPC) {
2389a5cd74adSYan, Zheng dout("aio_write %p %llx.%llx %llu~%u" "got EOLDSNAPC, retrying\n",
2390a5cd74adSYan, Zheng inode, ceph_vinop(inode), pos, (unsigned)count);
2391a5cd74adSYan, Zheng goto retry_snap;
2392a5cd74adSYan, Zheng }
2393a5cd74adSYan, Zheng
23946aa657c8SChristoph Hellwig if (written >= 0) {
239576142097SIlya Dryomov if ((map_flags & CEPH_OSDMAP_NEARFULL) ||
239676142097SIlya Dryomov (pool_flags & CEPH_POOL_FLAG_NEARFULL))
23976aa657c8SChristoph Hellwig iocb->ki_flags |= IOCB_DSYNC;
23986aa657c8SChristoph Hellwig written = generic_write_sync(iocb, written);
23996070e0c1SYan, Zheng }
240003d254edSYan, Zheng
24012f75e9e1SSage Weil goto out_unlocked;
2402b11ed503SJeff Layton out_caps:
2403b11ed503SJeff Layton ceph_put_cap_refs(ci, got);
24042f75e9e1SSage Weil out:
24058e4473bbSXiubo Li if (direct_lock)
2406321fe13cSJeff Layton ceph_end_io_direct(inode);
2407321fe13cSJeff Layton else
2408321fe13cSJeff Layton ceph_end_io_write(inode);
24092f75e9e1SSage Weil out_unlocked:
2410f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf);
241103d254edSYan, Zheng return written ? written : err;
2412124e68e7SSage Weil }
2413124e68e7SSage Weil
2414124e68e7SSage Weil /*
2415124e68e7SSage Weil * llseek. be sure to verify file size on SEEK_END.
2416124e68e7SSage Weil */
ceph_llseek(struct file * file,loff_t offset,int whence)2417965c8e59SAndrew Morton static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
2418124e68e7SSage Weil {
2419965c8e59SAndrew Morton if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
2420e8214503SJeff Layton struct inode *inode = file_inode(file);
2421e8214503SJeff Layton int ret;
2422e8214503SJeff Layton
2423508b32d8SYan, Zheng ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
2424955818cdSPhil Turnbull if (ret < 0)
2425955818cdSPhil Turnbull return ret;
2426124e68e7SSage Weil }
2427e8214503SJeff Layton return generic_file_llseek(file, offset, whence);
2428e8214503SJeff Layton }
2429124e68e7SSage Weil
ceph_zero_partial_page(struct inode * inode,loff_t offset,unsigned size)2430ad7a60deSLi Wang static inline void ceph_zero_partial_page(
2431ad7a60deSLi Wang struct inode *inode, loff_t offset, unsigned size)
2432ad7a60deSLi Wang {
2433ad7a60deSLi Wang struct page *page;
243409cbfeafSKirill A. Shutemov pgoff_t index = offset >> PAGE_SHIFT;
2435ad7a60deSLi Wang
2436ad7a60deSLi Wang page = find_lock_page(inode->i_mapping, index);
2437ad7a60deSLi Wang if (page) {
2438ad7a60deSLi Wang wait_on_page_writeback(page);
243909cbfeafSKirill A. Shutemov zero_user(page, offset & (PAGE_SIZE - 1), size);
2440ad7a60deSLi Wang unlock_page(page);
244109cbfeafSKirill A. Shutemov put_page(page);
2442ad7a60deSLi Wang }
2443ad7a60deSLi Wang }
2444ad7a60deSLi Wang
ceph_zero_pagecache_range(struct inode * inode,loff_t offset,loff_t length)2445ad7a60deSLi Wang static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
2446ad7a60deSLi Wang loff_t length)
2447ad7a60deSLi Wang {
244809cbfeafSKirill A. Shutemov loff_t nearly = round_up(offset, PAGE_SIZE);
2449ad7a60deSLi Wang if (offset < nearly) {
2450ad7a60deSLi Wang loff_t size = nearly - offset;
2451ad7a60deSLi Wang if (length < size)
2452ad7a60deSLi Wang size = length;
2453ad7a60deSLi Wang ceph_zero_partial_page(inode, offset, size);
2454ad7a60deSLi Wang offset += size;
2455ad7a60deSLi Wang length -= size;
2456ad7a60deSLi Wang }
245709cbfeafSKirill A. Shutemov if (length >= PAGE_SIZE) {
245809cbfeafSKirill A. Shutemov loff_t size = round_down(length, PAGE_SIZE);
2459ad7a60deSLi Wang truncate_pagecache_range(inode, offset, offset + size - 1);
2460ad7a60deSLi Wang offset += size;
2461ad7a60deSLi Wang length -= size;
2462ad7a60deSLi Wang }
2463ad7a60deSLi Wang if (length)
2464ad7a60deSLi Wang ceph_zero_partial_page(inode, offset, length);
2465ad7a60deSLi Wang }
2466ad7a60deSLi Wang
ceph_zero_partial_object(struct inode * inode,loff_t offset,loff_t * length)2467ad7a60deSLi Wang static int ceph_zero_partial_object(struct inode *inode,
2468ad7a60deSLi Wang loff_t offset, loff_t *length)
2469ad7a60deSLi Wang {
2470ad7a60deSLi Wang struct ceph_inode_info *ci = ceph_inode(inode);
2471985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
2472ad7a60deSLi Wang struct ceph_osd_request *req;
2473ad7a60deSLi Wang int ret = 0;
2474ad7a60deSLi Wang loff_t zero = 0;
2475ad7a60deSLi Wang int op;
2476ad7a60deSLi Wang
2477a68e564aSXiubo Li if (ceph_inode_is_shutdown(inode))
2478a68e564aSXiubo Li return -EIO;
2479a68e564aSXiubo Li
2480ad7a60deSLi Wang if (!length) {
2481ad7a60deSLi Wang op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
2482ad7a60deSLi Wang length = &zero;
2483ad7a60deSLi Wang } else {
2484ad7a60deSLi Wang op = CEPH_OSD_OP_ZERO;
2485ad7a60deSLi Wang }
2486ad7a60deSLi Wang
2487ad7a60deSLi Wang req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
2488ad7a60deSLi Wang ceph_vino(inode),
2489ad7a60deSLi Wang offset, length,
2490715e4cd4SYan, Zheng 0, 1, op,
249154ea0046SIlya Dryomov CEPH_OSD_FLAG_WRITE,
2492ad7a60deSLi Wang NULL, 0, 0, false);
2493ad7a60deSLi Wang if (IS_ERR(req)) {
2494ad7a60deSLi Wang ret = PTR_ERR(req);
2495ad7a60deSLi Wang goto out;
2496ad7a60deSLi Wang }
2497ad7a60deSLi Wang
2498fac02ddfSArnd Bergmann req->r_mtime = inode->i_mtime;
2499a8af0d68SJeff Layton ceph_osdc_start_request(&fsc->client->osdc, req);
2500ad7a60deSLi Wang ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
2501ad7a60deSLi Wang if (ret == -ENOENT)
2502ad7a60deSLi Wang ret = 0;
2503ad7a60deSLi Wang ceph_osdc_put_request(req);
2504ad7a60deSLi Wang
2505ad7a60deSLi Wang out:
2506ad7a60deSLi Wang return ret;
2507ad7a60deSLi Wang }
2508ad7a60deSLi Wang
ceph_zero_objects(struct inode * inode,loff_t offset,loff_t length)2509ad7a60deSLi Wang static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
2510ad7a60deSLi Wang {
2511ad7a60deSLi Wang int ret = 0;
2512ad7a60deSLi Wang struct ceph_inode_info *ci = ceph_inode(inode);
25137627151eSYan, Zheng s32 stripe_unit = ci->i_layout.stripe_unit;
25147627151eSYan, Zheng s32 stripe_count = ci->i_layout.stripe_count;
25157627151eSYan, Zheng s32 object_size = ci->i_layout.object_size;
2516b314a90dSSage Weil u64 object_set_size = object_size * stripe_count;
2517b314a90dSSage Weil u64 nearly, t;
2518ad7a60deSLi Wang
2519b314a90dSSage Weil /* round offset up to next period boundary */
2520b314a90dSSage Weil nearly = offset + object_set_size - 1;
2521b314a90dSSage Weil t = nearly;
2522b314a90dSSage Weil nearly -= do_div(t, object_set_size);
2523b314a90dSSage Weil
2524ad7a60deSLi Wang while (length && offset < nearly) {
2525ad7a60deSLi Wang loff_t size = length;
2526ad7a60deSLi Wang ret = ceph_zero_partial_object(inode, offset, &size);
2527ad7a60deSLi Wang if (ret < 0)
2528ad7a60deSLi Wang return ret;
2529ad7a60deSLi Wang offset += size;
2530ad7a60deSLi Wang length -= size;
2531ad7a60deSLi Wang }
2532ad7a60deSLi Wang while (length >= object_set_size) {
2533ad7a60deSLi Wang int i;
2534ad7a60deSLi Wang loff_t pos = offset;
2535ad7a60deSLi Wang for (i = 0; i < stripe_count; ++i) {
2536ad7a60deSLi Wang ret = ceph_zero_partial_object(inode, pos, NULL);
2537ad7a60deSLi Wang if (ret < 0)
2538ad7a60deSLi Wang return ret;
2539ad7a60deSLi Wang pos += stripe_unit;
2540ad7a60deSLi Wang }
2541ad7a60deSLi Wang offset += object_set_size;
2542ad7a60deSLi Wang length -= object_set_size;
2543ad7a60deSLi Wang }
2544ad7a60deSLi Wang while (length) {
2545ad7a60deSLi Wang loff_t size = length;
2546ad7a60deSLi Wang ret = ceph_zero_partial_object(inode, offset, &size);
2547ad7a60deSLi Wang if (ret < 0)
2548ad7a60deSLi Wang return ret;
2549ad7a60deSLi Wang offset += size;
2550ad7a60deSLi Wang length -= size;
2551ad7a60deSLi Wang }
2552ad7a60deSLi Wang return ret;
2553ad7a60deSLi Wang }
2554ad7a60deSLi Wang
ceph_fallocate(struct file * file,int mode,loff_t offset,loff_t length)2555ad7a60deSLi Wang static long ceph_fallocate(struct file *file, int mode,
2556ad7a60deSLi Wang loff_t offset, loff_t length)
2557ad7a60deSLi Wang {
2558ad7a60deSLi Wang struct ceph_file_info *fi = file->private_data;
2559aa8b60e0SLibo Chen struct inode *inode = file_inode(file);
2560ad7a60deSLi Wang struct ceph_inode_info *ci = ceph_inode(inode);
2561f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf;
2562ad7a60deSLi Wang int want, got = 0;
2563ad7a60deSLi Wang int dirty;
2564ad7a60deSLi Wang int ret = 0;
2565ad7a60deSLi Wang loff_t endoff = 0;
2566ad7a60deSLi Wang loff_t size;
2567ad7a60deSLi Wang
2568e027253cSXiubo Li dout("%s %p %llx.%llx mode %x, offset %llu length %llu\n", __func__,
2569e027253cSXiubo Li inode, ceph_vinop(inode), mode, offset, length);
2570e027253cSXiubo Li
2571bddff633SLuis Henriques if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2572494d77bfSYan, Zheng return -EOPNOTSUPP;
2573494d77bfSYan, Zheng
2574ad7a60deSLi Wang if (!S_ISREG(inode->i_mode))
2575ad7a60deSLi Wang return -EOPNOTSUPP;
2576ad7a60deSLi Wang
257794af0470SJeff Layton if (IS_ENCRYPTED(inode))
257894af0470SJeff Layton return -EOPNOTSUPP;
257994af0470SJeff Layton
2580f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush();
2581f66fd9f0SYan, Zheng if (!prealloc_cf)
2582f66fd9f0SYan, Zheng return -ENOMEM;
2583f66fd9f0SYan, Zheng
25845955102cSAl Viro inode_lock(inode);
2585ad7a60deSLi Wang
2586ad7a60deSLi Wang if (ceph_snap(inode) != CEPH_NOSNAP) {
2587ad7a60deSLi Wang ret = -EROFS;
2588ad7a60deSLi Wang goto unlock;
2589ad7a60deSLi Wang }
2590ad7a60deSLi Wang
2591ad7a60deSLi Wang size = i_size_read(inode);
2592bddff633SLuis Henriques
2593bddff633SLuis Henriques /* Are we punching a hole beyond EOF? */
2594bddff633SLuis Henriques if (offset >= size)
259542c99fc4SLuis Henriques goto unlock;
2596bddff633SLuis Henriques if ((offset + length) > size)
2597bddff633SLuis Henriques length = size - offset;
2598ad7a60deSLi Wang
2599ad7a60deSLi Wang if (fi->fmode & CEPH_FILE_MODE_LAZY)
2600ad7a60deSLi Wang want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
2601ad7a60deSLi Wang else
2602ad7a60deSLi Wang want = CEPH_CAP_FILE_BUFFER;
2603ad7a60deSLi Wang
2604e72968e1SJeff Layton ret = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, endoff, &got);
2605ad7a60deSLi Wang if (ret < 0)
2606ad7a60deSLi Wang goto unlock;
2607ad7a60deSLi Wang
2608e027253cSXiubo Li ret = file_modified(file);
2609e027253cSXiubo Li if (ret)
2610e027253cSXiubo Li goto put_caps;
2611e027253cSXiubo Li
2612057ba5b2SJan Kara filemap_invalidate_lock(inode->i_mapping);
2613400e1286SJeff Layton ceph_fscache_invalidate(inode, false);
2614ad7a60deSLi Wang ceph_zero_pagecache_range(inode, offset, length);
2615ad7a60deSLi Wang ret = ceph_zero_objects(inode, offset, length);
2616ad7a60deSLi Wang
2617ad7a60deSLi Wang if (!ret) {
2618ad7a60deSLi Wang spin_lock(&ci->i_ceph_lock);
2619f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
2620f66fd9f0SYan, Zheng &prealloc_cf);
2621ad7a60deSLi Wang spin_unlock(&ci->i_ceph_lock);
2622ad7a60deSLi Wang if (dirty)
2623ad7a60deSLi Wang __mark_inode_dirty(inode, dirty);
2624ad7a60deSLi Wang }
2625057ba5b2SJan Kara filemap_invalidate_unlock(inode->i_mapping);
2626ad7a60deSLi Wang
2627e027253cSXiubo Li put_caps:
2628ad7a60deSLi Wang ceph_put_cap_refs(ci, got);
2629ad7a60deSLi Wang unlock:
26305955102cSAl Viro inode_unlock(inode);
2631f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf);
2632ad7a60deSLi Wang return ret;
2633ad7a60deSLi Wang }
2634ad7a60deSLi Wang
2635503f82a9SLuis Henriques /*
2636503f82a9SLuis Henriques * This function tries to get FILE_WR capabilities for dst_ci and FILE_RD for
2637503f82a9SLuis Henriques * src_ci. Two attempts are made to obtain both caps, and an error is return if
2638503f82a9SLuis Henriques * this fails; zero is returned on success.
2639503f82a9SLuis Henriques */
get_rd_wr_caps(struct file * src_filp,int * src_got,struct file * dst_filp,loff_t dst_endoff,int * dst_got)26405e3ded1bSYan, Zheng static int get_rd_wr_caps(struct file *src_filp, int *src_got,
26415e3ded1bSYan, Zheng struct file *dst_filp,
2642503f82a9SLuis Henriques loff_t dst_endoff, int *dst_got)
2643503f82a9SLuis Henriques {
2644503f82a9SLuis Henriques int ret = 0;
2645503f82a9SLuis Henriques bool retrying = false;
2646503f82a9SLuis Henriques
2647503f82a9SLuis Henriques retry_caps:
26485e3ded1bSYan, Zheng ret = ceph_get_caps(dst_filp, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER,
2649e72968e1SJeff Layton dst_endoff, dst_got);
2650503f82a9SLuis Henriques if (ret < 0)
2651503f82a9SLuis Henriques return ret;
2652503f82a9SLuis Henriques
2653503f82a9SLuis Henriques /*
2654503f82a9SLuis Henriques * Since we're already holding the FILE_WR capability for the dst file,
2655503f82a9SLuis Henriques * we would risk a deadlock by using ceph_get_caps. Thus, we'll do some
2656503f82a9SLuis Henriques * retry dance instead to try to get both capabilities.
2657503f82a9SLuis Henriques */
26585e3ded1bSYan, Zheng ret = ceph_try_get_caps(file_inode(src_filp),
26595e3ded1bSYan, Zheng CEPH_CAP_FILE_RD, CEPH_CAP_FILE_SHARED,
2660503f82a9SLuis Henriques false, src_got);
2661503f82a9SLuis Henriques if (ret <= 0) {
2662503f82a9SLuis Henriques /* Start by dropping dst_ci caps and getting src_ci caps */
26635e3ded1bSYan, Zheng ceph_put_cap_refs(ceph_inode(file_inode(dst_filp)), *dst_got);
2664503f82a9SLuis Henriques if (retrying) {
2665503f82a9SLuis Henriques if (!ret)
2666503f82a9SLuis Henriques /* ceph_try_get_caps masks EAGAIN */
2667503f82a9SLuis Henriques ret = -EAGAIN;
2668503f82a9SLuis Henriques return ret;
2669503f82a9SLuis Henriques }
26705e3ded1bSYan, Zheng ret = ceph_get_caps(src_filp, CEPH_CAP_FILE_RD,
2671e72968e1SJeff Layton CEPH_CAP_FILE_SHARED, -1, src_got);
2672503f82a9SLuis Henriques if (ret < 0)
2673503f82a9SLuis Henriques return ret;
2674503f82a9SLuis Henriques /*... drop src_ci caps too, and retry */
26755e3ded1bSYan, Zheng ceph_put_cap_refs(ceph_inode(file_inode(src_filp)), *src_got);
2676503f82a9SLuis Henriques retrying = true;
2677503f82a9SLuis Henriques goto retry_caps;
2678503f82a9SLuis Henriques }
2679503f82a9SLuis Henriques return ret;
2680503f82a9SLuis Henriques }
2681503f82a9SLuis Henriques
put_rd_wr_caps(struct ceph_inode_info * src_ci,int src_got,struct ceph_inode_info * dst_ci,int dst_got)2682503f82a9SLuis Henriques static void put_rd_wr_caps(struct ceph_inode_info *src_ci, int src_got,
2683503f82a9SLuis Henriques struct ceph_inode_info *dst_ci, int dst_got)
2684503f82a9SLuis Henriques {
2685503f82a9SLuis Henriques ceph_put_cap_refs(src_ci, src_got);
2686503f82a9SLuis Henriques ceph_put_cap_refs(dst_ci, dst_got);
2687503f82a9SLuis Henriques }
2688503f82a9SLuis Henriques
2689503f82a9SLuis Henriques /*
2690503f82a9SLuis Henriques * This function does several size-related checks, returning an error if:
2691503f82a9SLuis Henriques * - source file is smaller than off+len
2692503f82a9SLuis Henriques * - destination file size is not OK (inode_newsize_ok())
2693503f82a9SLuis Henriques * - max bytes quotas is exceeded
2694503f82a9SLuis Henriques */
is_file_size_ok(struct inode * src_inode,struct inode * dst_inode,loff_t src_off,loff_t dst_off,size_t len)2695503f82a9SLuis Henriques static int is_file_size_ok(struct inode *src_inode, struct inode *dst_inode,
2696503f82a9SLuis Henriques loff_t src_off, loff_t dst_off, size_t len)
2697503f82a9SLuis Henriques {
2698503f82a9SLuis Henriques loff_t size, endoff;
2699503f82a9SLuis Henriques
2700503f82a9SLuis Henriques size = i_size_read(src_inode);
2701503f82a9SLuis Henriques /*
2702503f82a9SLuis Henriques * Don't copy beyond source file EOF. Instead of simply setting length
2703503f82a9SLuis Henriques * to (size - src_off), just drop to VFS default implementation, as the
2704503f82a9SLuis Henriques * local i_size may be stale due to other clients writing to the source
2705503f82a9SLuis Henriques * inode.
2706503f82a9SLuis Henriques */
2707503f82a9SLuis Henriques if (src_off + len > size) {
2708503f82a9SLuis Henriques dout("Copy beyond EOF (%llu + %zu > %llu)\n",
2709503f82a9SLuis Henriques src_off, len, size);
2710503f82a9SLuis Henriques return -EOPNOTSUPP;
2711503f82a9SLuis Henriques }
2712503f82a9SLuis Henriques size = i_size_read(dst_inode);
2713503f82a9SLuis Henriques
2714503f82a9SLuis Henriques endoff = dst_off + len;
2715503f82a9SLuis Henriques if (inode_newsize_ok(dst_inode, endoff))
2716503f82a9SLuis Henriques return -EOPNOTSUPP;
2717503f82a9SLuis Henriques
2718503f82a9SLuis Henriques if (ceph_quota_is_max_bytes_exceeded(dst_inode, endoff))
2719503f82a9SLuis Henriques return -EDQUOT;
2720503f82a9SLuis Henriques
2721503f82a9SLuis Henriques return 0;
2722503f82a9SLuis Henriques }
2723503f82a9SLuis Henriques
2724aca39d9eSLuís Henriques static struct ceph_osd_request *
ceph_alloc_copyfrom_request(struct ceph_osd_client * osdc,u64 src_snapid,struct ceph_object_id * src_oid,struct ceph_object_locator * src_oloc,struct ceph_object_id * dst_oid,struct ceph_object_locator * dst_oloc,u32 truncate_seq,u64 truncate_size)2725aca39d9eSLuís Henriques ceph_alloc_copyfrom_request(struct ceph_osd_client *osdc,
2726aca39d9eSLuís Henriques u64 src_snapid,
2727aca39d9eSLuís Henriques struct ceph_object_id *src_oid,
2728aca39d9eSLuís Henriques struct ceph_object_locator *src_oloc,
2729aca39d9eSLuís Henriques struct ceph_object_id *dst_oid,
2730aca39d9eSLuís Henriques struct ceph_object_locator *dst_oloc,
2731aca39d9eSLuís Henriques u32 truncate_seq, u64 truncate_size)
2732aca39d9eSLuís Henriques {
2733aca39d9eSLuís Henriques struct ceph_osd_request *req;
2734aca39d9eSLuís Henriques int ret;
2735aca39d9eSLuís Henriques u32 src_fadvise_flags =
2736aca39d9eSLuís Henriques CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
2737aca39d9eSLuís Henriques CEPH_OSD_OP_FLAG_FADVISE_NOCACHE;
2738aca39d9eSLuís Henriques u32 dst_fadvise_flags =
2739aca39d9eSLuís Henriques CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
2740aca39d9eSLuís Henriques CEPH_OSD_OP_FLAG_FADVISE_DONTNEED;
2741aca39d9eSLuís Henriques
2742aca39d9eSLuís Henriques req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
2743aca39d9eSLuís Henriques if (!req)
2744aca39d9eSLuís Henriques return ERR_PTR(-ENOMEM);
2745aca39d9eSLuís Henriques
2746aca39d9eSLuís Henriques req->r_flags = CEPH_OSD_FLAG_WRITE;
2747aca39d9eSLuís Henriques
2748aca39d9eSLuís Henriques ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
2749aca39d9eSLuís Henriques ceph_oid_copy(&req->r_t.base_oid, dst_oid);
2750aca39d9eSLuís Henriques
2751aca39d9eSLuís Henriques ret = osd_req_op_copy_from_init(req, src_snapid, 0,
2752aca39d9eSLuís Henriques src_oid, src_oloc,
2753aca39d9eSLuís Henriques src_fadvise_flags,
2754aca39d9eSLuís Henriques dst_fadvise_flags,
2755aca39d9eSLuís Henriques truncate_seq,
2756aca39d9eSLuís Henriques truncate_size,
2757aca39d9eSLuís Henriques CEPH_OSD_COPY_FROM_FLAG_TRUNCATE_SEQ);
2758aca39d9eSLuís Henriques if (ret)
2759aca39d9eSLuís Henriques goto out;
2760aca39d9eSLuís Henriques
2761aca39d9eSLuís Henriques ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
2762aca39d9eSLuís Henriques if (ret)
2763aca39d9eSLuís Henriques goto out;
2764aca39d9eSLuís Henriques
2765aca39d9eSLuís Henriques return req;
2766aca39d9eSLuís Henriques
2767aca39d9eSLuís Henriques out:
2768aca39d9eSLuís Henriques ceph_osdc_put_request(req);
2769aca39d9eSLuís Henriques return ERR_PTR(ret);
2770aca39d9eSLuís Henriques }
2771aca39d9eSLuís Henriques
ceph_do_objects_copy(struct ceph_inode_info * src_ci,u64 * src_off,struct ceph_inode_info * dst_ci,u64 * dst_off,struct ceph_fs_client * fsc,size_t len,unsigned int flags)27721b0c3b9fSLuis Henriques static ssize_t ceph_do_objects_copy(struct ceph_inode_info *src_ci, u64 *src_off,
27731b0c3b9fSLuis Henriques struct ceph_inode_info *dst_ci, u64 *dst_off,
27741b0c3b9fSLuis Henriques struct ceph_fs_client *fsc,
27751b0c3b9fSLuis Henriques size_t len, unsigned int flags)
27761b0c3b9fSLuis Henriques {
27771b0c3b9fSLuis Henriques struct ceph_object_locator src_oloc, dst_oloc;
27781b0c3b9fSLuis Henriques struct ceph_object_id src_oid, dst_oid;
2779aca39d9eSLuís Henriques struct ceph_osd_client *osdc;
2780aca39d9eSLuís Henriques struct ceph_osd_request *req;
27811b0c3b9fSLuis Henriques size_t bytes = 0;
27821b0c3b9fSLuis Henriques u64 src_objnum, src_objoff, dst_objnum, dst_objoff;
27831b0c3b9fSLuis Henriques u32 src_objlen, dst_objlen;
27841b0c3b9fSLuis Henriques u32 object_size = src_ci->i_layout.object_size;
27851b0c3b9fSLuis Henriques int ret;
27861b0c3b9fSLuis Henriques
27871b0c3b9fSLuis Henriques src_oloc.pool = src_ci->i_layout.pool_id;
27881b0c3b9fSLuis Henriques src_oloc.pool_ns = ceph_try_get_string(src_ci->i_layout.pool_ns);
27891b0c3b9fSLuis Henriques dst_oloc.pool = dst_ci->i_layout.pool_id;
27901b0c3b9fSLuis Henriques dst_oloc.pool_ns = ceph_try_get_string(dst_ci->i_layout.pool_ns);
2791aca39d9eSLuís Henriques osdc = &fsc->client->osdc;
27921b0c3b9fSLuis Henriques
27931b0c3b9fSLuis Henriques while (len >= object_size) {
27941b0c3b9fSLuis Henriques ceph_calc_file_object_mapping(&src_ci->i_layout, *src_off,
27951b0c3b9fSLuis Henriques object_size, &src_objnum,
27961b0c3b9fSLuis Henriques &src_objoff, &src_objlen);
27971b0c3b9fSLuis Henriques ceph_calc_file_object_mapping(&dst_ci->i_layout, *dst_off,
27981b0c3b9fSLuis Henriques object_size, &dst_objnum,
27991b0c3b9fSLuis Henriques &dst_objoff, &dst_objlen);
28001b0c3b9fSLuis Henriques ceph_oid_init(&src_oid);
28011b0c3b9fSLuis Henriques ceph_oid_printf(&src_oid, "%llx.%08llx",
28021b0c3b9fSLuis Henriques src_ci->i_vino.ino, src_objnum);
28031b0c3b9fSLuis Henriques ceph_oid_init(&dst_oid);
28041b0c3b9fSLuis Henriques ceph_oid_printf(&dst_oid, "%llx.%08llx",
28051b0c3b9fSLuis Henriques dst_ci->i_vino.ino, dst_objnum);
28061b0c3b9fSLuis Henriques /* Do an object remote copy */
2807aca39d9eSLuís Henriques req = ceph_alloc_copyfrom_request(osdc, src_ci->i_vino.snap,
28081b0c3b9fSLuis Henriques &src_oid, &src_oloc,
28091b0c3b9fSLuis Henriques &dst_oid, &dst_oloc,
28101b0c3b9fSLuis Henriques dst_ci->i_truncate_seq,
2811aca39d9eSLuís Henriques dst_ci->i_truncate_size);
2812aca39d9eSLuís Henriques if (IS_ERR(req))
2813aca39d9eSLuís Henriques ret = PTR_ERR(req);
2814aca39d9eSLuís Henriques else {
2815a8af0d68SJeff Layton ceph_osdc_start_request(osdc, req);
2816aca39d9eSLuís Henriques ret = ceph_osdc_wait_request(osdc, req);
2817c02cb7bdSLuís Henriques ceph_update_copyfrom_metrics(&fsc->mdsc->metric,
2818c02cb7bdSLuís Henriques req->r_start_latency,
2819c02cb7bdSLuís Henriques req->r_end_latency,
2820c02cb7bdSLuís Henriques object_size, ret);
2821aca39d9eSLuís Henriques ceph_osdc_put_request(req);
2822aca39d9eSLuís Henriques }
28231b0c3b9fSLuis Henriques if (ret) {
28241b0c3b9fSLuis Henriques if (ret == -EOPNOTSUPP) {
28251b0c3b9fSLuis Henriques fsc->have_copy_from2 = false;
28261b0c3b9fSLuis Henriques pr_notice("OSDs don't support copy-from2; disabling copy offload\n");
28271b0c3b9fSLuis Henriques }
28281b0c3b9fSLuis Henriques dout("ceph_osdc_copy_from returned %d\n", ret);
28291b0c3b9fSLuis Henriques if (!bytes)
28301b0c3b9fSLuis Henriques bytes = ret;
28311b0c3b9fSLuis Henriques goto out;
28321b0c3b9fSLuis Henriques }
28331b0c3b9fSLuis Henriques len -= object_size;
28341b0c3b9fSLuis Henriques bytes += object_size;
28351b0c3b9fSLuis Henriques *src_off += object_size;
28361b0c3b9fSLuis Henriques *dst_off += object_size;
28371b0c3b9fSLuis Henriques }
28381b0c3b9fSLuis Henriques
28391b0c3b9fSLuis Henriques out:
28401b0c3b9fSLuis Henriques ceph_oloc_destroy(&src_oloc);
28411b0c3b9fSLuis Henriques ceph_oloc_destroy(&dst_oloc);
28421b0c3b9fSLuis Henriques return bytes;
28431b0c3b9fSLuis Henriques }
28441b0c3b9fSLuis Henriques
__ceph_copy_file_range(struct file * src_file,loff_t src_off,struct file * dst_file,loff_t dst_off,size_t len,unsigned int flags)284564bf5ff5SDave Chinner static ssize_t __ceph_copy_file_range(struct file *src_file, loff_t src_off,
2846503f82a9SLuis Henriques struct file *dst_file, loff_t dst_off,
2847503f82a9SLuis Henriques size_t len, unsigned int flags)
2848503f82a9SLuis Henriques {
2849503f82a9SLuis Henriques struct inode *src_inode = file_inode(src_file);
2850503f82a9SLuis Henriques struct inode *dst_inode = file_inode(dst_file);
2851503f82a9SLuis Henriques struct ceph_inode_info *src_ci = ceph_inode(src_inode);
2852503f82a9SLuis Henriques struct ceph_inode_info *dst_ci = ceph_inode(dst_inode);
2853503f82a9SLuis Henriques struct ceph_cap_flush *prealloc_cf;
2854985b9ee8SXiubo Li struct ceph_fs_client *src_fsc = ceph_inode_to_fs_client(src_inode);
28551b0c3b9fSLuis Henriques loff_t size;
28561b0c3b9fSLuis Henriques ssize_t ret = -EIO, bytes;
2857503f82a9SLuis Henriques u64 src_objnum, dst_objnum, src_objoff, dst_objoff;
28581b0c3b9fSLuis Henriques u32 src_objlen, dst_objlen;
2859503f82a9SLuis Henriques int src_got = 0, dst_got = 0, err, dirty;
2860503f82a9SLuis Henriques
28616fd4e634SLuis Henriques if (src_inode->i_sb != dst_inode->i_sb) {
2862985b9ee8SXiubo Li struct ceph_fs_client *dst_fsc = ceph_inode_to_fs_client(dst_inode);
28636fd4e634SLuis Henriques
28646fd4e634SLuis Henriques if (ceph_fsid_compare(&src_fsc->client->fsid,
28656fd4e634SLuis Henriques &dst_fsc->client->fsid)) {
28666fd4e634SLuis Henriques dout("Copying files across clusters: src: %pU dst: %pU\n",
28676fd4e634SLuis Henriques &src_fsc->client->fsid, &dst_fsc->client->fsid);
28685dae222aSAmir Goldstein return -EXDEV;
28696fd4e634SLuis Henriques }
28706fd4e634SLuis Henriques }
2871503f82a9SLuis Henriques if (ceph_snap(dst_inode) != CEPH_NOSNAP)
2872503f82a9SLuis Henriques return -EROFS;
2873503f82a9SLuis Henriques
2874503f82a9SLuis Henriques /*
2875503f82a9SLuis Henriques * Some of the checks below will return -EOPNOTSUPP, which will force a
2876503f82a9SLuis Henriques * fallback to the default VFS copy_file_range implementation. This is
2877503f82a9SLuis Henriques * desirable in several cases (for ex, the 'len' is smaller than the
2878503f82a9SLuis Henriques * size of the objects, or in cases where that would be more
2879503f82a9SLuis Henriques * efficient).
2880503f82a9SLuis Henriques */
2881503f82a9SLuis Henriques
28826fd4e634SLuis Henriques if (ceph_test_mount_opt(src_fsc, NOCOPYFROM))
2883ea4cdc54SLuis Henriques return -EOPNOTSUPP;
2884ea4cdc54SLuis Henriques
288578beb0ffSLuis Henriques if (!src_fsc->have_copy_from2)
288678beb0ffSLuis Henriques return -EOPNOTSUPP;
288778beb0ffSLuis Henriques
2888a3a08193SLuis Henriques /*
2889a3a08193SLuis Henriques * Striped file layouts require that we copy partial objects, but the
2890a3a08193SLuis Henriques * OSD copy-from operation only supports full-object copies. Limit
2891a3a08193SLuis Henriques * this to non-striped file layouts for now.
2892a3a08193SLuis Henriques */
2893503f82a9SLuis Henriques if ((src_ci->i_layout.stripe_unit != dst_ci->i_layout.stripe_unit) ||
2894a3a08193SLuis Henriques (src_ci->i_layout.stripe_count != 1) ||
2895a3a08193SLuis Henriques (dst_ci->i_layout.stripe_count != 1) ||
2896a3a08193SLuis Henriques (src_ci->i_layout.object_size != dst_ci->i_layout.object_size)) {
2897a3a08193SLuis Henriques dout("Invalid src/dst files layout\n");
2898503f82a9SLuis Henriques return -EOPNOTSUPP;
2899a3a08193SLuis Henriques }
2900503f82a9SLuis Henriques
290194af0470SJeff Layton /* Every encrypted inode gets its own key, so we can't offload them */
290294af0470SJeff Layton if (IS_ENCRYPTED(src_inode) || IS_ENCRYPTED(dst_inode))
290394af0470SJeff Layton return -EOPNOTSUPP;
290494af0470SJeff Layton
2905503f82a9SLuis Henriques if (len < src_ci->i_layout.object_size)
2906503f82a9SLuis Henriques return -EOPNOTSUPP; /* no remote copy will be done */
2907503f82a9SLuis Henriques
2908503f82a9SLuis Henriques prealloc_cf = ceph_alloc_cap_flush();
2909503f82a9SLuis Henriques if (!prealloc_cf)
2910503f82a9SLuis Henriques return -ENOMEM;
2911503f82a9SLuis Henriques
2912c2c6d3ceSLuis Henriques /* Start by sync'ing the source and destination files */
2913503f82a9SLuis Henriques ret = file_write_and_wait_range(src_file, src_off, (src_off + len));
2914c2c6d3ceSLuis Henriques if (ret < 0) {
2915c2c6d3ceSLuis Henriques dout("failed to write src file (%zd)\n", ret);
2916503f82a9SLuis Henriques goto out;
2917c2c6d3ceSLuis Henriques }
2918c2c6d3ceSLuis Henriques ret = file_write_and_wait_range(dst_file, dst_off, (dst_off + len));
2919c2c6d3ceSLuis Henriques if (ret < 0) {
2920c2c6d3ceSLuis Henriques dout("failed to write dst file (%zd)\n", ret);
2921c2c6d3ceSLuis Henriques goto out;
2922c2c6d3ceSLuis Henriques }
2923503f82a9SLuis Henriques
2924503f82a9SLuis Henriques /*
2925503f82a9SLuis Henriques * We need FILE_WR caps for dst_ci and FILE_RD for src_ci as other
2926503f82a9SLuis Henriques * clients may have dirty data in their caches. And OSDs know nothing
2927503f82a9SLuis Henriques * about caps, so they can't safely do the remote object copies.
2928503f82a9SLuis Henriques */
29295e3ded1bSYan, Zheng err = get_rd_wr_caps(src_file, &src_got,
29305e3ded1bSYan, Zheng dst_file, (dst_off + len), &dst_got);
2931503f82a9SLuis Henriques if (err < 0) {
2932503f82a9SLuis Henriques dout("get_rd_wr_caps returned %d\n", err);
2933503f82a9SLuis Henriques ret = -EOPNOTSUPP;
2934503f82a9SLuis Henriques goto out;
2935503f82a9SLuis Henriques }
2936503f82a9SLuis Henriques
2937503f82a9SLuis Henriques ret = is_file_size_ok(src_inode, dst_inode, src_off, dst_off, len);
2938503f82a9SLuis Henriques if (ret < 0)
2939503f82a9SLuis Henriques goto out_caps;
2940503f82a9SLuis Henriques
2941503f82a9SLuis Henriques /* Drop dst file cached pages */
2942400e1286SJeff Layton ceph_fscache_invalidate(dst_inode, false);
2943503f82a9SLuis Henriques ret = invalidate_inode_pages2_range(dst_inode->i_mapping,
2944503f82a9SLuis Henriques dst_off >> PAGE_SHIFT,
29451b0c3b9fSLuis Henriques (dst_off + len) >> PAGE_SHIFT);
2946503f82a9SLuis Henriques if (ret < 0) {
2947503f82a9SLuis Henriques dout("Failed to invalidate inode pages (%zd)\n", ret);
2948503f82a9SLuis Henriques ret = 0; /* XXX */
2949503f82a9SLuis Henriques }
2950503f82a9SLuis Henriques ceph_calc_file_object_mapping(&src_ci->i_layout, src_off,
2951503f82a9SLuis Henriques src_ci->i_layout.object_size,
2952503f82a9SLuis Henriques &src_objnum, &src_objoff, &src_objlen);
2953503f82a9SLuis Henriques ceph_calc_file_object_mapping(&dst_ci->i_layout, dst_off,
2954503f82a9SLuis Henriques dst_ci->i_layout.object_size,
2955503f82a9SLuis Henriques &dst_objnum, &dst_objoff, &dst_objlen);
2956503f82a9SLuis Henriques /* object-level offsets need to the same */
2957503f82a9SLuis Henriques if (src_objoff != dst_objoff) {
2958503f82a9SLuis Henriques ret = -EOPNOTSUPP;
2959503f82a9SLuis Henriques goto out_caps;
2960503f82a9SLuis Henriques }
2961503f82a9SLuis Henriques
2962503f82a9SLuis Henriques /*
2963503f82a9SLuis Henriques * Do a manual copy if the object offset isn't object aligned.
2964503f82a9SLuis Henriques * 'src_objlen' contains the bytes left until the end of the object,
2965503f82a9SLuis Henriques * starting at the src_off
2966503f82a9SLuis Henriques */
2967503f82a9SLuis Henriques if (src_objoff) {
29681b0c3b9fSLuis Henriques dout("Initial partial copy of %u bytes\n", src_objlen);
29691b0c3b9fSLuis Henriques
2970503f82a9SLuis Henriques /*
2971503f82a9SLuis Henriques * we need to temporarily drop all caps as we'll be calling
2972503f82a9SLuis Henriques * {read,write}_iter, which will get caps again.
2973503f82a9SLuis Henriques */
2974503f82a9SLuis Henriques put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
2975503f82a9SLuis Henriques ret = do_splice_direct(src_file, &src_off, dst_file,
2976503f82a9SLuis Henriques &dst_off, src_objlen, flags);
29771b0c3b9fSLuis Henriques /* Abort on short copies or on error */
297807bb00efSDan Carpenter if (ret < (long)src_objlen) {
29791b0c3b9fSLuis Henriques dout("Failed partial copy (%zd)\n", ret);
2980503f82a9SLuis Henriques goto out;
2981503f82a9SLuis Henriques }
2982503f82a9SLuis Henriques len -= ret;
29835e3ded1bSYan, Zheng err = get_rd_wr_caps(src_file, &src_got,
29845e3ded1bSYan, Zheng dst_file, (dst_off + len), &dst_got);
2985503f82a9SLuis Henriques if (err < 0)
2986503f82a9SLuis Henriques goto out;
2987503f82a9SLuis Henriques err = is_file_size_ok(src_inode, dst_inode,
2988503f82a9SLuis Henriques src_off, dst_off, len);
2989503f82a9SLuis Henriques if (err < 0)
2990503f82a9SLuis Henriques goto out_caps;
2991503f82a9SLuis Henriques }
29921b0c3b9fSLuis Henriques
29931b0c3b9fSLuis Henriques size = i_size_read(dst_inode);
29941b0c3b9fSLuis Henriques bytes = ceph_do_objects_copy(src_ci, &src_off, dst_ci, &dst_off,
29951b0c3b9fSLuis Henriques src_fsc, len, flags);
29961b0c3b9fSLuis Henriques if (bytes <= 0) {
2997503f82a9SLuis Henriques if (!ret)
29981b0c3b9fSLuis Henriques ret = bytes;
2999503f82a9SLuis Henriques goto out_caps;
3000503f82a9SLuis Henriques }
30011b0c3b9fSLuis Henriques dout("Copied %zu bytes out of %zu\n", bytes, len);
30021b0c3b9fSLuis Henriques len -= bytes;
30031b0c3b9fSLuis Henriques ret += bytes;
3004503f82a9SLuis Henriques
3005503f82a9SLuis Henriques file_update_time(dst_file);
30065c308356SJeff Layton inode_inc_iversion_raw(dst_inode);
30075c308356SJeff Layton
30081b0c3b9fSLuis Henriques if (dst_off > size) {
3009503f82a9SLuis Henriques /* Let the MDS know about dst file size change */
3010a0d93e32SYan, Zheng if (ceph_inode_set_size(dst_inode, dst_off) ||
3011a0d93e32SYan, Zheng ceph_quota_is_max_bytes_approaching(dst_inode, dst_off))
3012e4b731ccSXiubo Li ceph_check_caps(dst_ci, CHECK_CAPS_AUTHONLY | CHECK_CAPS_FLUSH);
3013503f82a9SLuis Henriques }
3014503f82a9SLuis Henriques /* Mark Fw dirty */
3015503f82a9SLuis Henriques spin_lock(&dst_ci->i_ceph_lock);
3016503f82a9SLuis Henriques dirty = __ceph_mark_dirty_caps(dst_ci, CEPH_CAP_FILE_WR, &prealloc_cf);
3017503f82a9SLuis Henriques spin_unlock(&dst_ci->i_ceph_lock);
3018503f82a9SLuis Henriques if (dirty)
3019503f82a9SLuis Henriques __mark_inode_dirty(dst_inode, dirty);
3020503f82a9SLuis Henriques
3021503f82a9SLuis Henriques out_caps:
3022503f82a9SLuis Henriques put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
3023503f82a9SLuis Henriques
30241b0c3b9fSLuis Henriques /*
30251b0c3b9fSLuis Henriques * Do the final manual copy if we still have some bytes left, unless
30261b0c3b9fSLuis Henriques * there were errors in remote object copies (len >= object_size).
30271b0c3b9fSLuis Henriques */
30281b0c3b9fSLuis Henriques if (len && (len < src_ci->i_layout.object_size)) {
30291b0c3b9fSLuis Henriques dout("Final partial copy of %zu bytes\n", len);
30301b0c3b9fSLuis Henriques bytes = do_splice_direct(src_file, &src_off, dst_file,
3031503f82a9SLuis Henriques &dst_off, len, flags);
30321b0c3b9fSLuis Henriques if (bytes > 0)
30331b0c3b9fSLuis Henriques ret += bytes;
30341b0c3b9fSLuis Henriques else
30351b0c3b9fSLuis Henriques dout("Failed partial copy (%zd)\n", bytes);
3036503f82a9SLuis Henriques }
3037503f82a9SLuis Henriques
3038503f82a9SLuis Henriques out:
3039503f82a9SLuis Henriques ceph_free_cap_flush(prealloc_cf);
3040503f82a9SLuis Henriques
3041503f82a9SLuis Henriques return ret;
3042503f82a9SLuis Henriques }
3043503f82a9SLuis Henriques
ceph_copy_file_range(struct file * src_file,loff_t src_off,struct file * dst_file,loff_t dst_off,size_t len,unsigned int flags)304464bf5ff5SDave Chinner static ssize_t ceph_copy_file_range(struct file *src_file, loff_t src_off,
304564bf5ff5SDave Chinner struct file *dst_file, loff_t dst_off,
304664bf5ff5SDave Chinner size_t len, unsigned int flags)
304764bf5ff5SDave Chinner {
304864bf5ff5SDave Chinner ssize_t ret;
304964bf5ff5SDave Chinner
305064bf5ff5SDave Chinner ret = __ceph_copy_file_range(src_file, src_off, dst_file, dst_off,
305164bf5ff5SDave Chinner len, flags);
305264bf5ff5SDave Chinner
30535dae222aSAmir Goldstein if (ret == -EOPNOTSUPP || ret == -EXDEV)
305464bf5ff5SDave Chinner ret = generic_copy_file_range(src_file, src_off, dst_file,
305564bf5ff5SDave Chinner dst_off, len, flags);
305664bf5ff5SDave Chinner return ret;
305764bf5ff5SDave Chinner }
305864bf5ff5SDave Chinner
3059124e68e7SSage Weil const struct file_operations ceph_file_fops = {
3060124e68e7SSage Weil .open = ceph_open,
3061124e68e7SSage Weil .release = ceph_release,
3062124e68e7SSage Weil .llseek = ceph_llseek,
30633644424dSAl Viro .read_iter = ceph_read_iter,
30644908b822SAl Viro .write_iter = ceph_write_iter,
3065124e68e7SSage Weil .mmap = ceph_mmap,
3066124e68e7SSage Weil .fsync = ceph_fsync,
306740819f6fSGreg Farnum .lock = ceph_lock,
3068496ceaf1SJeff Layton .setlease = simple_nosetlease,
306940819f6fSGreg Farnum .flock = ceph_flock,
3070ccfdf7cbSDavid Howells .splice_read = ceph_splice_read,
30713551dd79SAl Viro .splice_write = iter_file_splice_write,
3072124e68e7SSage Weil .unlocked_ioctl = ceph_ioctl,
307318bd6caaSArnd Bergmann .compat_ioctl = compat_ptr_ioctl,
3074ad7a60deSLi Wang .fallocate = ceph_fallocate,
3075503f82a9SLuis Henriques .copy_file_range = ceph_copy_file_range,
3076124e68e7SSage Weil };
3077