blob: 39b364a51c17ae1d4a3cb9ab8184bd71d1790e9b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Luis Henriques503f82a2018-10-15 16:45:59 +01003#include <linux/ceph/striper.h>
Sage Weil124e68e2009-10-06 11:31:08 -07004
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005#include <linux/module.h>
Sage Weil124e68e2009-10-06 11:31:08 -07006#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Sage Weil124e68e2009-10-06 11:31:08 -07008#include <linux/file.h>
Sage Weil5ef50c32012-07-31 11:27:36 -07009#include <linux/mount.h>
Sage Weil124e68e2009-10-06 11:31:08 -070010#include <linux/namei.h>
11#include <linux/writeback.h>
Li Wangad7a60d2013-08-15 11:51:44 +080012#include <linux/falloc.h>
Jeff Layton5c308352019-06-06 08:57:27 -040013#include <linux/iversion.h>
Sage Weil124e68e2009-10-06 11:31:08 -070014
15#include "super.h"
16#include "mds_client.h"
Milosz Tanski99ccbd22013-08-21 17:29:54 -040017#include "cache.h"
Jeff Layton321fe132019-08-02 13:15:39 -040018#include "io.h"
Sage Weil124e68e2009-10-06 11:31:08 -070019
Alexander Graff775ff72017-04-27 18:34:00 +020020static __le32 ceph_flags_sys2wire(u32 flags)
21{
22 u32 wire_flags = 0;
23
24 switch (flags & O_ACCMODE) {
25 case O_RDONLY:
26 wire_flags |= CEPH_O_RDONLY;
27 break;
28 case O_WRONLY:
29 wire_flags |= CEPH_O_WRONLY;
30 break;
31 case O_RDWR:
32 wire_flags |= CEPH_O_RDWR;
33 break;
34 }
35
Chengguang Xu51b10f32018-03-09 15:12:40 +080036 flags &= ~O_ACCMODE;
37
Alexander Graff775ff72017-04-27 18:34:00 +020038#define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; }
39
40 ceph_sys2wire(O_CREAT);
41 ceph_sys2wire(O_EXCL);
42 ceph_sys2wire(O_TRUNC);
43 ceph_sys2wire(O_DIRECTORY);
44 ceph_sys2wire(O_NOFOLLOW);
45
46#undef ceph_sys2wire
47
48 if (flags)
Chengguang Xu4c069a52018-01-30 16:29:17 +080049 dout("unused open flags: %x\n", flags);
Alexander Graff775ff72017-04-27 18:34:00 +020050
51 return cpu_to_le32(wire_flags);
52}
53
Sage Weil124e68e2009-10-06 11:31:08 -070054/*
55 * Ceph file operations
56 *
57 * Implement basic open/close functionality, and implement
58 * read/write.
59 *
60 * We implement three modes of file I/O:
61 * - buffered uses the generic_file_aio_{read,write} helpers
62 *
63 * - synchronous is used when there is multi-client read/write
64 * sharing, avoids the page cache, and synchronously waits for an
65 * ack from the OSD.
66 *
67 * - direct io takes the variant of the sync path that references
68 * user pages directly.
69 *
70 * fsync() flushes and waits on dirty pages, but just queues metadata
71 * for writeback: since the MDS can recover size and mtime there is no
72 * need to wait for MDS acknowledgement.
73 */
74
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +080075/*
Ilya Dryomovfc218542018-05-04 16:57:31 +020076 * How many pages to get in one call to iov_iter_get_pages(). This
77 * determines the size of the on-stack array used as a buffer.
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +080078 */
Ilya Dryomovfc218542018-05-04 16:57:31 +020079#define ITER_GET_BVECS_PAGES 64
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +080080
Ilya Dryomovfc218542018-05-04 16:57:31 +020081static ssize_t __iter_get_bvecs(struct iov_iter *iter, size_t maxsize,
82 struct bio_vec *bvecs)
83{
84 size_t size = 0;
85 int bvec_idx = 0;
86
87 if (maxsize > iov_iter_count(iter))
88 maxsize = iov_iter_count(iter);
89
90 while (size < maxsize) {
91 struct page *pages[ITER_GET_BVECS_PAGES];
92 ssize_t bytes;
93 size_t start;
94 int idx = 0;
95
96 bytes = iov_iter_get_pages(iter, pages, maxsize - size,
97 ITER_GET_BVECS_PAGES, &start);
98 if (bytes < 0)
99 return size ?: bytes;
100
101 iov_iter_advance(iter, bytes);
102 size += bytes;
103
104 for ( ; bytes; idx++, bvec_idx++) {
105 struct bio_vec bv = {
106 .bv_page = pages[idx],
107 .bv_len = min_t(int, bytes, PAGE_SIZE - start),
108 .bv_offset = start,
109 };
110
111 bvecs[bvec_idx] = bv;
112 bytes -= bv.bv_len;
113 start = 0;
114 }
115 }
116
117 return size;
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +0800118}
119
120/*
Ilya Dryomovfc218542018-05-04 16:57:31 +0200121 * iov_iter_get_pages() only considers one iov_iter segment, no matter
122 * what maxsize or maxpages are given. For ITER_BVEC that is a single
123 * page.
124 *
125 * Attempt to get up to @maxsize bytes worth of pages from @iter.
126 * Return the number of bytes in the created bio_vec array, or an error.
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +0800127 */
Ilya Dryomovfc218542018-05-04 16:57:31 +0200128static ssize_t iter_get_bvecs_alloc(struct iov_iter *iter, size_t maxsize,
129 struct bio_vec **bvecs, int *num_bvecs)
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +0800130{
Ilya Dryomovfc218542018-05-04 16:57:31 +0200131 struct bio_vec *bv;
132 size_t orig_count = iov_iter_count(iter);
133 ssize_t bytes;
134 int npages;
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +0800135
Ilya Dryomovfc218542018-05-04 16:57:31 +0200136 iov_iter_truncate(iter, maxsize);
137 npages = iov_iter_npages(iter, INT_MAX);
138 iov_iter_reexpand(iter, orig_count);
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +0800139
Ilya Dryomovfc218542018-05-04 16:57:31 +0200140 /*
141 * __iter_get_bvecs() may populate only part of the array -- zero it
142 * out.
143 */
144 bv = kvmalloc_array(npages, sizeof(*bv), GFP_KERNEL | __GFP_ZERO);
145 if (!bv)
146 return -ENOMEM;
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +0800147
Ilya Dryomovfc218542018-05-04 16:57:31 +0200148 bytes = __iter_get_bvecs(iter, maxsize, bv);
149 if (bytes < 0) {
150 /*
151 * No pages were pinned -- just free the array.
152 */
153 kvfree(bv);
154 return bytes;
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +0800155 }
156
Ilya Dryomovfc218542018-05-04 16:57:31 +0200157 *bvecs = bv;
158 *num_bvecs = npages;
159 return bytes;
160}
161
162static void put_bvecs(struct bio_vec *bvecs, int num_bvecs, bool should_dirty)
163{
164 int i;
165
166 for (i = 0; i < num_bvecs; i++) {
167 if (bvecs[i].bv_page) {
168 if (should_dirty)
169 set_page_dirty_lock(bvecs[i].bv_page);
170 put_page(bvecs[i].bv_page);
171 }
172 }
173 kvfree(bvecs);
Zhu, Caifengb5b98989d2015-10-08 15:26:15 +0800174}
Sage Weil124e68e2009-10-06 11:31:08 -0700175
176/*
177 * Prepare an open request. Preallocate ceph_cap to avoid an
178 * inopportune ENOMEM later.
179 */
180static struct ceph_mds_request *
181prepare_open_request(struct super_block *sb, int flags, int create_mode)
182{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700183 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
184 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil124e68e2009-10-06 11:31:08 -0700185 struct ceph_mds_request *req;
186 int want_auth = USE_ANY_MDS;
187 int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
188
189 if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
190 want_auth = USE_AUTH_MDS;
191
192 req = ceph_mdsc_create_request(mdsc, op, want_auth);
193 if (IS_ERR(req))
194 goto out;
195 req->r_fmode = ceph_flags_to_mode(flags);
Alexander Graff775ff72017-04-27 18:34:00 +0200196 req->r_args.open.flags = ceph_flags_sys2wire(flags);
Sage Weil124e68e2009-10-06 11:31:08 -0700197 req->r_args.open.mode = cpu_to_le32(create_mode);
Sage Weil124e68e2009-10-06 11:31:08 -0700198out:
199 return req;
200}
201
Chengguang Xubb48bd42018-03-13 10:42:44 +0800202static int ceph_init_file_info(struct inode *inode, struct file *file,
203 int fmode, bool isdir)
204{
Yan, Zhengf4b97862019-07-25 20:16:42 +0800205 struct ceph_inode_info *ci = ceph_inode(inode);
Chengguang Xubb48bd42018-03-13 10:42:44 +0800206 struct ceph_file_info *fi;
207
208 dout("%s %p %p 0%o (%s)\n", __func__, inode, file,
209 inode->i_mode, isdir ? "dir" : "regular");
210 BUG_ON(inode->i_fop->release != ceph_release);
211
212 if (isdir) {
213 struct ceph_dir_file_info *dfi =
214 kmem_cache_zalloc(ceph_dir_file_cachep, GFP_KERNEL);
215 if (!dfi) {
Yan, Zheng719a2512020-03-05 20:21:00 +0800216 ceph_put_fmode(ci, fmode, 1); /* clean up */
Chengguang Xubb48bd42018-03-13 10:42:44 +0800217 return -ENOMEM;
218 }
219
220 file->private_data = dfi;
221 fi = &dfi->file_info;
222 dfi->next_offset = 2;
223 dfi->readdir_cache_idx = -1;
224 } else {
225 fi = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
226 if (!fi) {
Yan, Zheng719a2512020-03-05 20:21:00 +0800227 ceph_put_fmode(ci, fmode, 1); /* clean up */
Chengguang Xubb48bd42018-03-13 10:42:44 +0800228 return -ENOMEM;
229 }
230
231 file->private_data = fi;
232 }
233
234 fi->fmode = fmode;
235 spin_lock_init(&fi->rw_contexts_lock);
236 INIT_LIST_HEAD(&fi->rw_contexts);
Yan, Zhengf4b97862019-07-25 20:16:42 +0800237 fi->meta_err = errseq_sample(&ci->i_meta_err);
Yan, Zheng81f148a2019-07-25 20:16:46 +0800238 fi->filp_gen = READ_ONCE(ceph_inode_to_client(inode)->filp_gen);
Chengguang Xubb48bd42018-03-13 10:42:44 +0800239
240 return 0;
241}
242
Sage Weil124e68e2009-10-06 11:31:08 -0700243/*
244 * initialize private struct file data.
245 * if we fail, clean up by dropping fmode reference on the ceph_inode
246 */
247static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
248{
Sage Weil124e68e2009-10-06 11:31:08 -0700249 int ret = 0;
250
251 switch (inode->i_mode & S_IFMT) {
252 case S_IFREG:
Yan, Zheng46b59b22016-05-18 15:25:03 +0800253 ceph_fscache_register_inode_cookie(inode);
254 ceph_fscache_file_set_cookie(inode, file);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600255 /* fall through */
Sage Weil124e68e2009-10-06 11:31:08 -0700256 case S_IFDIR:
Chengguang Xubb48bd42018-03-13 10:42:44 +0800257 ret = ceph_init_file_info(inode, file, fmode,
258 S_ISDIR(inode->i_mode));
259 if (ret)
260 return ret;
Sage Weil124e68e2009-10-06 11:31:08 -0700261 break;
262
263 case S_IFLNK:
264 dout("init_file %p %p 0%o (symlink)\n", inode, file,
265 inode->i_mode);
Yan, Zheng719a2512020-03-05 20:21:00 +0800266 ceph_put_fmode(ceph_inode(inode), fmode, 1); /* clean up */
Sage Weil124e68e2009-10-06 11:31:08 -0700267 break;
268
269 default:
270 dout("init_file %p %p 0%o (special)\n", inode, file,
271 inode->i_mode);
272 /*
273 * we need to drop the open ref now, since we don't
274 * have .release set to ceph_release.
275 */
Yan, Zheng719a2512020-03-05 20:21:00 +0800276 ceph_put_fmode(ceph_inode(inode), fmode, 1); /* clean up */
Sage Weil124e68e2009-10-06 11:31:08 -0700277 BUG_ON(inode->i_fop->release == ceph_release);
278
279 /* call the proper open fop */
280 ret = inode->i_fop->open(inode, file);
281 }
282 return ret;
283}
284
285/*
Yan, Zheng77310322016-04-08 15:27:16 +0800286 * try renew caps after session gets killed.
287 */
Yan, Zheng719a2512020-03-05 20:21:00 +0800288int ceph_renew_caps(struct inode *inode, int fmode)
Yan, Zheng77310322016-04-08 15:27:16 +0800289{
Yan, Zheng719a2512020-03-05 20:21:00 +0800290 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Yan, Zheng77310322016-04-08 15:27:16 +0800291 struct ceph_inode_info *ci = ceph_inode(inode);
292 struct ceph_mds_request *req;
293 int err, flags, wanted;
294
295 spin_lock(&ci->i_ceph_lock);
Yan, Zheng719a2512020-03-05 20:21:00 +0800296 __ceph_touch_fmode(ci, mdsc, fmode);
Yan, Zheng77310322016-04-08 15:27:16 +0800297 wanted = __ceph_caps_file_wanted(ci);
298 if (__ceph_is_any_real_caps(ci) &&
Yan, Zheng8242c9f2017-03-25 17:28:10 +0800299 (!(wanted & CEPH_CAP_ANY_WR) || ci->i_auth_cap)) {
Yan, Zheng77310322016-04-08 15:27:16 +0800300 int issued = __ceph_caps_issued(ci, NULL);
301 spin_unlock(&ci->i_ceph_lock);
302 dout("renew caps %p want %s issued %s updating mds_wanted\n",
303 inode, ceph_cap_string(wanted), ceph_cap_string(issued));
304 ceph_check_caps(ci, 0, NULL);
305 return 0;
306 }
307 spin_unlock(&ci->i_ceph_lock);
308
309 flags = 0;
310 if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
311 flags = O_RDWR;
312 else if (wanted & CEPH_CAP_FILE_RD)
313 flags = O_RDONLY;
314 else if (wanted & CEPH_CAP_FILE_WR)
315 flags = O_WRONLY;
316#ifdef O_LAZY
317 if (wanted & CEPH_CAP_FILE_LAZYIO)
318 flags |= O_LAZY;
319#endif
320
321 req = prepare_open_request(inode->i_sb, flags, 0);
322 if (IS_ERR(req)) {
323 err = PTR_ERR(req);
324 goto out;
325 }
326
327 req->r_inode = inode;
328 ihold(inode);
329 req->r_num_caps = 1;
330 req->r_fmode = -1;
331
332 err = ceph_mdsc_do_request(mdsc, NULL, req);
333 ceph_mdsc_put_request(req);
334out:
335 dout("renew caps %p open result=%d\n", inode, err);
336 return err < 0 ? err : 0;
337}
338
339/*
Sage Weil124e68e2009-10-06 11:31:08 -0700340 * If we already have the requisite capabilities, we can satisfy
341 * the open request locally (no need to request new caps from the
342 * MDS). We do, however, need to inform the MDS (asynchronously)
343 * if our wanted caps set expands.
344 */
345int ceph_open(struct inode *inode, struct file *file)
346{
347 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700348 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
349 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil124e68e2009-10-06 11:31:08 -0700350 struct ceph_mds_request *req;
Chengguang Xu73737682018-02-28 19:43:47 +0800351 struct ceph_file_info *fi = file->private_data;
Sage Weil124e68e2009-10-06 11:31:08 -0700352 int err;
353 int flags, fmode, wanted;
354
Chengguang Xu73737682018-02-28 19:43:47 +0800355 if (fi) {
Sage Weil124e68e2009-10-06 11:31:08 -0700356 dout("open file %p is already opened\n", file);
357 return 0;
358 }
359
360 /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
361 flags = file->f_flags & ~(O_CREAT|O_EXCL);
362 if (S_ISDIR(inode->i_mode))
363 flags = O_DIRECTORY; /* mds likes to know */
364
365 dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
366 ceph_vinop(inode), file, flags, file->f_flags);
367 fmode = ceph_flags_to_mode(flags);
368 wanted = ceph_caps_for_mode(fmode);
369
370 /* snapped files are read-only */
371 if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
372 return -EROFS;
373
374 /* trivially open snapdir */
375 if (ceph_snap(inode) == CEPH_SNAPDIR) {
Sage Weilbe655592011-11-30 09:47:09 -0800376 spin_lock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700377 __ceph_get_fmode(ci, fmode);
Sage Weilbe655592011-11-30 09:47:09 -0800378 spin_unlock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700379 return ceph_init_file(inode, file, fmode);
380 }
381
382 /*
Sage Weil7421ab82010-11-07 09:07:15 -0800383 * No need to block if we have caps on the auth MDS (for
384 * write) or any MDS (for read). Update wanted set
Sage Weil124e68e2009-10-06 11:31:08 -0700385 * asynchronously.
386 */
Sage Weilbe655592011-11-30 09:47:09 -0800387 spin_lock(&ci->i_ceph_lock);
Sage Weil7421ab82010-11-07 09:07:15 -0800388 if (__ceph_is_any_real_caps(ci) &&
389 (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
Yan, Zhengc1944fe2017-01-29 22:15:47 +0800390 int mds_wanted = __ceph_caps_mds_wanted(ci, true);
Sage Weil124e68e2009-10-06 11:31:08 -0700391 int issued = __ceph_caps_issued(ci, NULL);
392
393 dout("open %p fmode %d want %s issued %s using existing\n",
394 inode, fmode, ceph_cap_string(wanted),
395 ceph_cap_string(issued));
396 __ceph_get_fmode(ci, fmode);
Sage Weilbe655592011-11-30 09:47:09 -0800397 spin_unlock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700398
399 /* adjust wanted? */
400 if ((issued & wanted) != wanted &&
401 (mds_wanted & wanted) != wanted &&
402 ceph_snap(inode) != CEPH_SNAPDIR)
403 ceph_check_caps(ci, 0, NULL);
404
405 return ceph_init_file(inode, file, fmode);
406 } else if (ceph_snap(inode) != CEPH_NOSNAP &&
407 (ci->i_snap_caps & wanted) == wanted) {
408 __ceph_get_fmode(ci, fmode);
Yan, Zheng719a2512020-03-05 20:21:00 +0800409 __ceph_touch_fmode(ci, mdsc, fmode);
Sage Weilbe655592011-11-30 09:47:09 -0800410 spin_unlock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700411 return ceph_init_file(inode, file, fmode);
412 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400413
Sage Weilbe655592011-11-30 09:47:09 -0800414 spin_unlock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700415
416 dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
417 req = prepare_open_request(inode->i_sb, flags, 0);
418 if (IS_ERR(req)) {
419 err = PTR_ERR(req);
420 goto out;
421 }
Sage Weil70b666c2011-05-27 09:24:26 -0700422 req->r_inode = inode;
423 ihold(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400424
Sage Weil124e68e2009-10-06 11:31:08 -0700425 req->r_num_caps = 1;
Jianpeng Mae36d5712015-08-18 10:25:35 +0800426 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil124e68e2009-10-06 11:31:08 -0700427 if (!err)
428 err = ceph_init_file(inode, file, req->r_fmode);
429 ceph_mdsc_put_request(req);
430 dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
431out:
432 return err;
433}
434
Jeff Layton785892f2020-01-02 07:11:38 -0500435/* Clone the layout from a synchronous create, if the dir now has Dc caps */
436static void
437cache_file_layout(struct inode *dst, struct inode *src)
438{
439 struct ceph_inode_info *cdst = ceph_inode(dst);
440 struct ceph_inode_info *csrc = ceph_inode(src);
441
442 spin_lock(&cdst->i_ceph_lock);
443 if ((__ceph_caps_issued(cdst, NULL) & CEPH_CAP_DIR_CREATE) &&
444 !ceph_file_layout_is_valid(&cdst->i_cached_layout)) {
445 memcpy(&cdst->i_cached_layout, &csrc->i_layout,
446 sizeof(cdst->i_cached_layout));
447 rcu_assign_pointer(cdst->i_cached_layout.pool_ns,
448 ceph_try_get_string(csrc->i_layout.pool_ns));
449 }
450 spin_unlock(&cdst->i_ceph_lock);
451}
Sage Weil124e68e2009-10-06 11:31:08 -0700452
453/*
Jeff Layton9a8d03c2019-11-27 12:06:14 -0500454 * Try to set up an async create. We need caps, a file layout, and inode number,
455 * and either a lease on the dentry or complete dir info. If any of those
456 * criteria are not satisfied, then return false and the caller can go
457 * synchronous.
458 */
459static int try_prep_async_create(struct inode *dir, struct dentry *dentry,
460 struct ceph_file_layout *lo, u64 *pino)
461{
462 struct ceph_inode_info *ci = ceph_inode(dir);
463 struct ceph_dentry_info *di = ceph_dentry(dentry);
464 int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_CREATE;
465 u64 ino;
466
467 spin_lock(&ci->i_ceph_lock);
468 /* No auth cap means no chance for Dc caps */
469 if (!ci->i_auth_cap)
470 goto no_async;
471
472 /* Any delegated inos? */
473 if (xa_empty(&ci->i_auth_cap->session->s_delegated_inos))
474 goto no_async;
475
476 if (!ceph_file_layout_is_valid(&ci->i_cached_layout))
477 goto no_async;
478
479 if ((__ceph_caps_issued(ci, NULL) & want) != want)
480 goto no_async;
481
482 if (d_in_lookup(dentry)) {
483 if (!__ceph_dir_is_complete(ci))
484 goto no_async;
Yan, Zheng3313f66a2020-03-04 21:22:20 +0800485 spin_lock(&dentry->d_lock);
486 di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
487 spin_unlock(&dentry->d_lock);
Jeff Layton9a8d03c2019-11-27 12:06:14 -0500488 } else if (atomic_read(&ci->i_shared_gen) !=
489 READ_ONCE(di->lease_shared_gen)) {
490 goto no_async;
491 }
492
493 ino = ceph_get_deleg_ino(ci->i_auth_cap->session);
494 if (!ino)
495 goto no_async;
496
497 *pino = ino;
498 ceph_take_cap_refs(ci, want, false);
499 memcpy(lo, &ci->i_cached_layout, sizeof(*lo));
500 rcu_assign_pointer(lo->pool_ns,
501 ceph_try_get_string(ci->i_cached_layout.pool_ns));
502 got = want;
503no_async:
504 spin_unlock(&ci->i_ceph_lock);
505 return got;
506}
507
508static void restore_deleg_ino(struct inode *dir, u64 ino)
509{
510 struct ceph_inode_info *ci = ceph_inode(dir);
511 struct ceph_mds_session *s = NULL;
512
513 spin_lock(&ci->i_ceph_lock);
514 if (ci->i_auth_cap)
515 s = ceph_get_mds_session(ci->i_auth_cap->session);
516 spin_unlock(&ci->i_ceph_lock);
517 if (s) {
518 int err = ceph_restore_deleg_ino(s, ino);
519 if (err)
520 pr_warn("ceph: unable to restore delegated ino 0x%llx to session: %d\n",
521 ino, err);
522 ceph_put_mds_session(s);
523 }
524}
525
526static void ceph_async_create_cb(struct ceph_mds_client *mdsc,
527 struct ceph_mds_request *req)
528{
529 int result = req->r_err ? req->r_err :
530 le32_to_cpu(req->r_reply_info.head->result);
531
532 if (result == -EJUKEBOX)
533 goto out;
534
535 mapping_set_error(req->r_parent->i_mapping, result);
536
537 if (result) {
538 struct dentry *dentry = req->r_dentry;
539 int pathlen;
540 u64 base;
541 char *path = ceph_mdsc_build_path(req->r_dentry, &pathlen,
542 &base, 0);
543
544 ceph_dir_clear_complete(req->r_parent);
545 if (!d_unhashed(dentry))
546 d_drop(dentry);
547
548 /* FIXME: start returning I/O errors on all accesses? */
549 pr_warn("ceph: async create failure path=(%llx)%s result=%d!\n",
550 base, IS_ERR(path) ? "<<bad>>" : path, result);
551 ceph_mdsc_free_path(path, pathlen);
552 }
553
554 if (req->r_target_inode) {
555 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
556 u64 ino = ceph_vino(req->r_target_inode).ino;
557
558 if (req->r_deleg_ino != ino)
559 pr_warn("%s: inode number mismatch! err=%d deleg_ino=0x%llx target=0x%llx\n",
560 __func__, req->r_err, req->r_deleg_ino, ino);
561 mapping_set_error(req->r_target_inode->i_mapping, result);
562
563 spin_lock(&ci->i_ceph_lock);
564 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
565 ci->i_ceph_flags &= ~CEPH_I_ASYNC_CREATE;
566 wake_up_bit(&ci->i_ceph_flags, CEPH_ASYNC_CREATE_BIT);
567 }
568 ceph_kick_flushing_inode_caps(req->r_session, ci);
569 spin_unlock(&ci->i_ceph_lock);
570 } else {
571 pr_warn("%s: no req->r_target_inode for 0x%llx\n", __func__,
572 req->r_deleg_ino);
573 }
574out:
575 ceph_mdsc_release_dir_caps(req);
576}
577
578static int ceph_finish_async_create(struct inode *dir, struct dentry *dentry,
579 struct file *file, umode_t mode,
580 struct ceph_mds_request *req,
581 struct ceph_acl_sec_ctx *as_ctx,
582 struct ceph_file_layout *lo)
583{
584 int ret;
585 char xattr_buf[4];
586 struct ceph_mds_reply_inode in = { };
587 struct ceph_mds_reply_info_in iinfo = { .in = &in };
588 struct ceph_inode_info *ci = ceph_inode(dir);
589 struct inode *inode;
590 struct timespec64 now;
591 struct ceph_vino vino = { .ino = req->r_deleg_ino,
592 .snap = CEPH_NOSNAP };
593
594 ktime_get_real_ts64(&now);
595
596 inode = ceph_get_inode(dentry->d_sb, vino);
597 if (IS_ERR(inode))
598 return PTR_ERR(inode);
599
600 iinfo.inline_version = CEPH_INLINE_NONE;
601 iinfo.change_attr = 1;
602 ceph_encode_timespec64(&iinfo.btime, &now);
603
604 iinfo.xattr_len = ARRAY_SIZE(xattr_buf);
605 iinfo.xattr_data = xattr_buf;
606 memset(iinfo.xattr_data, 0, iinfo.xattr_len);
607
608 in.ino = cpu_to_le64(vino.ino);
609 in.snapid = cpu_to_le64(CEPH_NOSNAP);
610 in.version = cpu_to_le64(1); // ???
611 in.cap.caps = in.cap.wanted = cpu_to_le32(CEPH_CAP_ALL_FILE);
612 in.cap.cap_id = cpu_to_le64(1);
613 in.cap.realm = cpu_to_le64(ci->i_snap_realm->ino);
614 in.cap.flags = CEPH_CAP_FLAG_AUTH;
615 in.ctime = in.mtime = in.atime = iinfo.btime;
616 in.mode = cpu_to_le32((u32)mode);
617 in.truncate_seq = cpu_to_le32(1);
618 in.truncate_size = cpu_to_le64(-1ULL);
619 in.xattr_version = cpu_to_le64(1);
620 in.uid = cpu_to_le32(from_kuid(&init_user_ns, current_fsuid()));
621 in.gid = cpu_to_le32(from_kgid(&init_user_ns, dir->i_mode & S_ISGID ?
622 dir->i_gid : current_fsgid()));
623 in.nlink = cpu_to_le32(1);
624 in.max_size = cpu_to_le64(lo->stripe_unit);
625
626 ceph_file_layout_to_legacy(lo, &in.layout);
627
628 ret = ceph_fill_inode(inode, NULL, &iinfo, NULL, req->r_session,
629 req->r_fmode, NULL);
630 if (ret) {
631 dout("%s failed to fill inode: %d\n", __func__, ret);
632 ceph_dir_clear_complete(dir);
633 if (!d_unhashed(dentry))
634 d_drop(dentry);
635 if (inode->i_state & I_NEW)
636 discard_new_inode(inode);
637 } else {
638 struct dentry *dn;
639
640 dout("%s d_adding new inode 0x%llx to 0x%lx/%s\n", __func__,
641 vino.ino, dir->i_ino, dentry->d_name.name);
642 ceph_dir_clear_ordered(dir);
643 ceph_init_inode_acls(inode, as_ctx);
644 if (inode->i_state & I_NEW) {
645 /*
646 * If it's not I_NEW, then someone created this before
647 * we got here. Assume the server is aware of it at
648 * that point and don't worry about setting
649 * CEPH_I_ASYNC_CREATE.
650 */
651 ceph_inode(inode)->i_ceph_flags = CEPH_I_ASYNC_CREATE;
652 unlock_new_inode(inode);
653 }
654 if (d_in_lookup(dentry) || d_really_is_negative(dentry)) {
655 if (!d_unhashed(dentry))
656 d_drop(dentry);
657 dn = d_splice_alias(inode, dentry);
658 WARN_ON_ONCE(dn && dn != dentry);
659 }
660 file->f_mode |= FMODE_CREATED;
661 ret = finish_open(file, dentry, ceph_open);
662 }
663 return ret;
664}
665
666/*
Sage Weil5ef50c32012-07-31 11:27:36 -0700667 * Do a lookup + open with a single request. If we get a non-existent
668 * file or symlink, return 1 so the VFS can retry.
Sage Weil124e68e2009-10-06 11:31:08 -0700669 */
Sage Weil5ef50c32012-07-31 11:27:36 -0700670int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
Al Viro44907d72018-06-08 13:32:02 -0400671 struct file *file, unsigned flags, umode_t mode)
Sage Weil124e68e2009-10-06 11:31:08 -0700672{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700673 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
674 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil124e68e2009-10-06 11:31:08 -0700675 struct ceph_mds_request *req;
Sage Weil5ef50c32012-07-31 11:27:36 -0700676 struct dentry *dn;
Yan, Zheng5c31e922019-05-26 15:35:39 +0800677 struct ceph_acl_sec_ctx as_ctx = {};
Jeff Layton9a8d03c2019-11-27 12:06:14 -0500678 bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
Luis Henriquesb7a29212018-01-05 10:47:19 +0000679 int mask;
Sage Weil124e68e2009-10-06 11:31:08 -0700680 int err;
Sage Weil124e68e2009-10-06 11:31:08 -0700681
Al Viroa4555892014-10-21 20:11:25 -0400682 dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
683 dir, dentry, dentry,
Sage Weil5ef50c32012-07-31 11:27:36 -0700684 d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
685
686 if (dentry->d_name.len > NAME_MAX)
687 return -ENAMETOOLONG;
688
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800689 if (flags & O_CREAT) {
Luis Henriquesb7a29212018-01-05 10:47:19 +0000690 if (ceph_quota_is_max_files_exceeded(dir))
691 return -EDQUOT;
Yan, Zheng5c31e922019-05-26 15:35:39 +0800692 err = ceph_pre_init_acls(dir, &mode, &as_ctx);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800693 if (err < 0)
694 return err;
Yan, Zhengac6713c2019-05-26 16:27:56 +0800695 err = ceph_security_init_secctx(dentry, mode, &as_ctx);
696 if (err < 0)
697 goto out_ctx;
Jeff Layton5bb5e6e2019-10-30 12:15:10 -0400698 } else if (!d_in_lookup(dentry)) {
699 /* If it's not being looked up, it's negative */
700 return -ENOENT;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800701 }
Jeff Layton9a8d03c2019-11-27 12:06:14 -0500702retry:
Sage Weil124e68e2009-10-06 11:31:08 -0700703 /* do the open */
704 req = prepare_open_request(dir->i_sb, flags, mode);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800705 if (IS_ERR(req)) {
706 err = PTR_ERR(req);
Yan, Zheng5c31e922019-05-26 15:35:39 +0800707 goto out_ctx;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800708 }
Sage Weil124e68e2009-10-06 11:31:08 -0700709 req->r_dentry = dget(dentry);
710 req->r_num_caps = 2;
Jeff Layton9a8d03c2019-11-27 12:06:14 -0500711 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
712 if (ceph_security_xattr_wanted(dir))
713 mask |= CEPH_CAP_XATTR_SHARED;
714 req->r_args.open.mask = cpu_to_le32(mask);
715 req->r_parent = dir;
716
Sage Weil124e68e2009-10-06 11:31:08 -0700717 if (flags & O_CREAT) {
Jeff Layton9a8d03c2019-11-27 12:06:14 -0500718 struct ceph_file_layout lo;
719
Yan, Zheng222b7f92017-11-23 17:47:15 +0800720 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
Sage Weil124e68e2009-10-06 11:31:08 -0700721 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zheng5c31e922019-05-26 15:35:39 +0800722 if (as_ctx.pagelist) {
723 req->r_pagelist = as_ctx.pagelist;
724 as_ctx.pagelist = NULL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800725 }
Jeff Layton9a8d03c2019-11-27 12:06:14 -0500726 if (try_async &&
727 (req->r_dir_caps =
728 try_prep_async_create(dir, dentry, &lo,
729 &req->r_deleg_ino))) {
730 set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
731 req->r_args.open.flags |= cpu_to_le32(CEPH_O_EXCL);
732 req->r_callback = ceph_async_create_cb;
733 err = ceph_mdsc_submit_request(mdsc, dir, req);
734 if (!err) {
735 err = ceph_finish_async_create(dir, dentry,
736 file, mode, req,
737 &as_ctx, &lo);
738 } else if (err == -EJUKEBOX) {
739 restore_deleg_ino(dir, req->r_deleg_ino);
740 ceph_mdsc_put_request(req);
741 try_async = false;
742 goto retry;
743 }
744 goto out_req;
745 }
Sage Weil124e68e2009-10-06 11:31:08 -0700746 }
Yan, Zheng315f2402016-03-07 10:34:50 +0800747
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500748 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weilacda7652011-07-26 11:27:48 -0700749 err = ceph_mdsc_do_request(mdsc,
750 (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
751 req);
Yan, Zhengbf91c312015-01-19 13:23:20 +0800752 err = ceph_handle_snapdir(req, dentry, err);
Sam Lang79aec9842012-12-19 09:44:23 -1000753 if (err)
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800754 goto out_req;
Sam Lang79aec9842012-12-19 09:44:23 -1000755
Jianpeng Maa43137f2015-08-18 10:23:50 +0800756 if ((flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
Sage Weil124e68e2009-10-06 11:31:08 -0700757 err = ceph_handle_notrace_create(dir, dentry);
Sage Weil5ef50c32012-07-31 11:27:36 -0700758
Al Viro00699ad2016-07-05 09:44:53 -0400759 if (d_in_lookup(dentry)) {
Sage Weil5ef50c32012-07-31 11:27:36 -0700760 dn = ceph_finish_lookup(req, dentry, err);
761 if (IS_ERR(dn))
762 err = PTR_ERR(dn);
763 } else {
764 /* we were given a hashed negative dentry */
765 dn = NULL;
766 }
Sage Weil468640e2011-07-26 11:28:11 -0700767 if (err)
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800768 goto out_req;
David Howells2b0143b2015-03-17 22:25:59 +0000769 if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
Sage Weil5ef50c32012-07-31 11:27:36 -0700770 /* make vfs retry on splice, ENOENT, or symlink */
771 dout("atomic_open finish_no_open on dn %p\n", dn);
772 err = finish_no_open(file, dn);
773 } else {
774 dout("atomic_open finish_open on dn %p\n", dn);
Sam Lang6e8575f2012-12-28 09:56:46 -0800775 if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
Jeff Layton785892f2020-01-02 07:11:38 -0500776 struct inode *newino = d_inode(dentry);
777
778 cache_file_layout(dir, newino);
779 ceph_init_inode_acls(newino, &as_ctx);
Al Viro73a09dd2018-06-08 13:22:02 -0400780 file->f_mode |= FMODE_CREATED;
Sam Lang6e8575f2012-12-28 09:56:46 -0800781 }
Al Virobe12af32018-06-08 11:44:56 -0400782 err = finish_open(file, dentry, ceph_open);
Sage Weil5ef50c32012-07-31 11:27:36 -0700783 }
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800784out_req:
Yan, Zhengab866542014-04-01 20:20:17 +0800785 if (!req->r_err && req->r_target_inode)
Yan, Zheng719a2512020-03-05 20:21:00 +0800786 ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode, 1);
Sage Weil124e68e2009-10-06 11:31:08 -0700787 ceph_mdsc_put_request(req);
Yan, Zheng5c31e922019-05-26 15:35:39 +0800788out_ctx:
789 ceph_release_acl_sec_ctx(&as_ctx);
Sage Weil5ef50c32012-07-31 11:27:36 -0700790 dout("atomic_open result=%d\n", err);
Al Virod9585272012-06-22 12:39:14 +0400791 return err;
Sage Weil124e68e2009-10-06 11:31:08 -0700792}
793
794int ceph_release(struct inode *inode, struct file *file)
795{
796 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil124e68e2009-10-06 11:31:08 -0700797
Chengguang Xubb48bd42018-03-13 10:42:44 +0800798 if (S_ISDIR(inode->i_mode)) {
799 struct ceph_dir_file_info *dfi = file->private_data;
800 dout("release inode %p dir file %p\n", inode, file);
801 WARN_ON(!list_empty(&dfi->file_info.rw_contexts));
802
Yan, Zheng719a2512020-03-05 20:21:00 +0800803 ceph_put_fmode(ci, dfi->file_info.fmode, 1);
Chengguang Xubb48bd42018-03-13 10:42:44 +0800804
805 if (dfi->last_readdir)
806 ceph_mdsc_put_request(dfi->last_readdir);
807 kfree(dfi->last_name);
808 kfree(dfi->dir_info);
809 kmem_cache_free(ceph_dir_file_cachep, dfi);
810 } else {
811 struct ceph_file_info *fi = file->private_data;
812 dout("release inode %p regular file %p\n", inode, file);
813 WARN_ON(!list_empty(&fi->rw_contexts));
814
Yan, Zheng719a2512020-03-05 20:21:00 +0800815 ceph_put_fmode(ci, fi->fmode, 1);
816
Chengguang Xubb48bd42018-03-13 10:42:44 +0800817 kmem_cache_free(ceph_file_cachep, fi);
818 }
Sage Weil195d3ce2010-03-01 09:57:54 -0800819
820 /* wake up anyone waiting for caps on this inode */
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700821 wake_up_all(&ci->i_cap_wq);
Sage Weil124e68e2009-10-06 11:31:08 -0700822 return 0;
823}
824
Yan, Zheng83701242014-11-14 22:36:18 +0800825enum {
Yan, Zhengc8fe9b12015-12-23 21:23:38 +0800826 HAVE_RETRIED = 1,
827 CHECK_EOF = 2,
828 READ_INLINE = 3,
Yan, Zheng83701242014-11-14 22:36:18 +0800829};
830
Sage Weil124e68e2009-10-06 11:31:08 -0700831/*
Yan, Zhengfce7a972018-09-29 16:02:19 +0800832 * Completely synchronous read and write methods. Direct from __user
833 * buffer to osd, or directly to user pages (if O_DIRECT).
834 *
835 * If the read spans object boundary, just do multiple reads. (That's not
836 * atomic, but good enough for now.)
Sage Weil124e68e2009-10-06 11:31:08 -0700837 *
838 * If we get a short result from the OSD, check against i_size; we need to
839 * only return a short read to the caller if we hit EOF.
840 */
Yan, Zheng7ce469a2016-11-08 21:54:34 +0800841static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
Yan, Zhengfce7a972018-09-29 16:02:19 +0800842 int *retry_op)
Sage Weil124e68e2009-10-06 11:31:08 -0700843{
majianpeng8eb4efb2013-09-26 14:42:17 +0800844 struct file *file = iocb->ki_filp;
Al Viro496ad9a2013-01-23 17:07:38 -0500845 struct inode *inode = file_inode(file);
Yan, Zhengfce7a972018-09-29 16:02:19 +0800846 struct ceph_inode_info *ci = ceph_inode(inode);
847 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
848 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng7ce469a2016-11-08 21:54:34 +0800849 ssize_t ret;
Yan, Zhengfce7a972018-09-29 16:02:19 +0800850 u64 off = iocb->ki_pos;
851 u64 len = iov_iter_count(to);
Sage Weil124e68e2009-10-06 11:31:08 -0700852
Yan, Zheng1c0a9c22017-08-16 17:24:58 +0800853 dout("sync_read on file %p %llu~%u %s\n", file, off, (unsigned)len,
Sage Weil124e68e2009-10-06 11:31:08 -0700854 (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
Yan, Zhengd0d0db22014-07-21 10:15:48 +0800855
856 if (!len)
857 return 0;
Sage Weile98b6fe2010-11-09 12:24:53 -0800858 /*
859 * flush any page cache pages in this range. this
860 * will make concurrent normal and sync io slow,
861 * but it will at least behave sensibly when they are
862 * in sequence.
863 */
zhengbine450f4d2019-02-01 11:19:15 +0000864 ret = filemap_write_and_wait_range(inode->i_mapping,
865 off, off + len - 1);
Yehuda Sadeh29065a52010-02-09 11:14:41 -0800866 if (ret < 0)
majianpeng8eb4efb2013-09-26 14:42:17 +0800867 return ret;
Yehuda Sadeh29065a52010-02-09 11:14:41 -0800868
Yan, Zhengfce7a972018-09-29 16:02:19 +0800869 ret = 0;
870 while ((len = iov_iter_count(to)) > 0) {
871 struct ceph_osd_request *req;
872 struct page **pages;
873 int num_pages;
Yan, Zheng7ce469a2016-11-08 21:54:34 +0800874 size_t page_off;
Yan, Zhengfce7a972018-09-29 16:02:19 +0800875 u64 i_size;
876 bool more;
Sage Weil124e68e2009-10-06 11:31:08 -0700877
Yan, Zhengfce7a972018-09-29 16:02:19 +0800878 req = ceph_osdc_new_request(osdc, &ci->i_layout,
879 ci->i_vino, off, &len, 0, 1,
880 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
881 NULL, ci->i_truncate_seq,
882 ci->i_truncate_size, false);
883 if (IS_ERR(req)) {
884 ret = PTR_ERR(req);
885 break;
majianpeng8eb4efb2013-09-26 14:42:17 +0800886 }
Yan, Zheng7ce469a2016-11-08 21:54:34 +0800887
Yan, Zhengfce7a972018-09-29 16:02:19 +0800888 more = len < iov_iter_count(to);
Yan, Zheng7ce469a2016-11-08 21:54:34 +0800889
Linus Torvalds9931a072018-11-01 19:58:52 -0700890 if (unlikely(iov_iter_is_pipe(to))) {
Yan, Zhengfce7a972018-09-29 16:02:19 +0800891 ret = iov_iter_get_pages_alloc(to, &pages, len,
892 &page_off);
893 if (ret <= 0) {
894 ceph_osdc_put_request(req);
895 ret = -ENOMEM;
896 break;
897 }
898 num_pages = DIV_ROUND_UP(ret + page_off, PAGE_SIZE);
899 if (ret < len) {
900 len = ret;
901 osd_req_op_extent_update(req, 0, len);
902 more = false;
903 }
904 } else {
905 num_pages = calc_pages_for(off, len);
906 page_off = off & ~PAGE_MASK;
907 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
908 if (IS_ERR(pages)) {
909 ceph_osdc_put_request(req);
910 ret = PTR_ERR(pages);
911 break;
Yan, Zheng7ce469a2016-11-08 21:54:34 +0800912 }
913 }
Yan, Zhengfce7a972018-09-29 16:02:19 +0800914
915 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_off,
916 false, false);
917 ret = ceph_osdc_start_request(osdc, req, false);
918 if (!ret)
919 ret = ceph_osdc_wait_request(osdc, req);
920 ceph_osdc_put_request(req);
921
922 i_size = i_size_read(inode);
923 dout("sync_read %llu~%llu got %zd i_size %llu%s\n",
924 off, len, ret, i_size, (more ? " MORE" : ""));
925
926 if (ret == -ENOENT)
927 ret = 0;
928 if (ret >= 0 && ret < len && (off + ret < i_size)) {
929 int zlen = min(len - ret, i_size - off - ret);
930 int zoff = page_off + ret;
931 dout("sync_read zero gap %llu~%llu\n",
932 off + ret, off + ret + zlen);
933 ceph_zero_page_vector_range(zoff, zlen, pages);
934 ret += zlen;
935 }
936
Linus Torvalds9931a072018-11-01 19:58:52 -0700937 if (unlikely(iov_iter_is_pipe(to))) {
Yan, Zhengfce7a972018-09-29 16:02:19 +0800938 if (ret > 0) {
939 iov_iter_advance(to, ret);
940 off += ret;
941 } else {
942 iov_iter_advance(to, 0);
943 }
944 ceph_put_page_vector(pages, num_pages, false);
945 } else {
946 int idx = 0;
947 size_t left = ret > 0 ? ret : 0;
948 while (left > 0) {
949 size_t len, copied;
950 page_off = off & ~PAGE_MASK;
951 len = min_t(size_t, left, PAGE_SIZE - page_off);
952 copied = copy_page_to_iter(pages[idx++],
953 page_off, len, to);
954 off += copied;
955 left -= copied;
956 if (copied < len) {
957 ret = -EFAULT;
958 break;
959 }
960 }
961 ceph_release_page_vector(pages, num_pages);
962 }
963
Yan, Zheng131d7eb2019-07-25 20:16:47 +0800964 if (ret < 0) {
965 if (ret == -EBLACKLISTED)
966 fsc->blacklisted = true;
967 break;
968 }
969
970 if (off >= i_size || !more)
Yan, Zhengfce7a972018-09-29 16:02:19 +0800971 break;
majianpeng8eb4efb2013-09-26 14:42:17 +0800972 }
973
974 if (off > iocb->ki_pos) {
Yan, Zhengfce7a972018-09-29 16:02:19 +0800975 if (ret >= 0 &&
976 iov_iter_count(to) > 0 && off >= i_size_read(inode))
977 *retry_op = CHECK_EOF;
majianpeng8eb4efb2013-09-26 14:42:17 +0800978 ret = off - iocb->ki_pos;
979 iocb->ki_pos = off;
980 }
981
Yan, Zhengfce7a972018-09-29 16:02:19 +0800982 dout("sync_read result %zd retry_op %d\n", ret, *retry_op);
Sage Weil124e68e2009-10-06 11:31:08 -0700983 return ret;
984}
985
Yan, Zhengc8fe9b12015-12-23 21:23:38 +0800986struct ceph_aio_request {
987 struct kiocb *iocb;
988 size_t total_len;
Yan, Zheng85784f92018-03-16 11:22:29 +0800989 bool write;
990 bool should_dirty;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +0800991 int error;
992 struct list_head osd_reqs;
993 unsigned num_reqs;
994 atomic_t pending_reqs;
Arnd Bergmannfac02dd2018-07-13 22:18:37 +0200995 struct timespec64 mtime;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +0800996 struct ceph_cap_flush *prealloc_cf;
997};
998
Yan, Zheng5be03892015-12-24 08:44:20 +0800999struct ceph_aio_work {
1000 struct work_struct work;
1001 struct ceph_osd_request *req;
1002};
1003
1004static void ceph_aio_retry_work(struct work_struct *work);
1005
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001006static void ceph_aio_complete(struct inode *inode,
1007 struct ceph_aio_request *aio_req)
1008{
1009 struct ceph_inode_info *ci = ceph_inode(inode);
1010 int ret;
1011
1012 if (!atomic_dec_and_test(&aio_req->pending_reqs))
1013 return;
1014
Jeff Layton6a817492019-11-13 09:56:06 -05001015 if (aio_req->iocb->ki_flags & IOCB_DIRECT)
1016 inode_dio_end(inode);
1017
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001018 ret = aio_req->error;
1019 if (!ret)
1020 ret = aio_req->total_len;
1021
1022 dout("ceph_aio_complete %p rc %d\n", inode, ret);
1023
1024 if (ret >= 0 && aio_req->write) {
1025 int dirty;
1026
1027 loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
1028 if (endoff > i_size_read(inode)) {
1029 if (ceph_inode_set_size(inode, endoff))
1030 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1031 }
1032
1033 spin_lock(&ci->i_ceph_lock);
1034 ci->i_inline_version = CEPH_INLINE_NONE;
1035 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1036 &aio_req->prealloc_cf);
1037 spin_unlock(&ci->i_ceph_lock);
1038 if (dirty)
1039 __mark_inode_dirty(inode, dirty);
1040
1041 }
1042
1043 ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
1044 CEPH_CAP_FILE_RD));
1045
1046 aio_req->iocb->ki_complete(aio_req->iocb, ret, 0);
1047
1048 ceph_free_cap_flush(aio_req->prealloc_cf);
1049 kfree(aio_req);
1050}
1051
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001052static void ceph_aio_complete_req(struct ceph_osd_request *req)
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001053{
1054 int rc = req->r_result;
1055 struct inode *inode = req->r_inode;
1056 struct ceph_aio_request *aio_req = req->r_priv;
1057 struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001058
Ilya Dryomovfc218542018-05-04 16:57:31 +02001059 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_BVECS);
1060 BUG_ON(!osd_data->num_bvecs);
1061
1062 dout("ceph_aio_complete_req %p rc %d bytes %u\n",
1063 inode, rc, osd_data->bvec_pos.iter.bi_size);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001064
1065 if (rc == -EOLDSNAPC) {
Yan, Zheng5be03892015-12-24 08:44:20 +08001066 struct ceph_aio_work *aio_work;
1067 BUG_ON(!aio_req->write);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001068
Yan, Zheng5be03892015-12-24 08:44:20 +08001069 aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
1070 if (aio_work) {
1071 INIT_WORK(&aio_work->work, ceph_aio_retry_work);
1072 aio_work->req = req;
Yan, Zheng1cf89a82019-05-18 11:18:44 +08001073 queue_work(ceph_inode_to_client(inode)->inode_wq,
Yan, Zheng5be03892015-12-24 08:44:20 +08001074 &aio_work->work);
1075 return;
1076 }
1077 rc = -ENOMEM;
1078 } else if (!aio_req->write) {
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001079 if (rc == -ENOENT)
1080 rc = 0;
Ilya Dryomovfc218542018-05-04 16:57:31 +02001081 if (rc >= 0 && osd_data->bvec_pos.iter.bi_size > rc) {
1082 struct iov_iter i;
1083 int zlen = osd_data->bvec_pos.iter.bi_size - rc;
1084
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001085 /*
1086 * If read is satisfied by single OSD request,
1087 * it can pass EOF. Otherwise read is within
1088 * i_size.
1089 */
1090 if (aio_req->num_reqs == 1) {
1091 loff_t i_size = i_size_read(inode);
1092 loff_t endoff = aio_req->iocb->ki_pos + rc;
1093 if (endoff < i_size)
1094 zlen = min_t(size_t, zlen,
1095 i_size - endoff);
1096 aio_req->total_len = rc + zlen;
1097 }
1098
David Howellsaa563d72018-10-20 00:57:56 +01001099 iov_iter_bvec(&i, READ, osd_data->bvec_pos.bvecs,
Ilya Dryomovfc218542018-05-04 16:57:31 +02001100 osd_data->num_bvecs,
1101 osd_data->bvec_pos.iter.bi_size);
1102 iov_iter_advance(&i, rc);
1103 iov_iter_zero(zlen, &i);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001104 }
1105 }
1106
Ilya Dryomovfc218542018-05-04 16:57:31 +02001107 put_bvecs(osd_data->bvec_pos.bvecs, osd_data->num_bvecs,
1108 aio_req->should_dirty);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001109 ceph_osdc_put_request(req);
1110
1111 if (rc < 0)
1112 cmpxchg(&aio_req->error, 0, rc);
1113
1114 ceph_aio_complete(inode, aio_req);
1115 return;
1116}
1117
Yan, Zheng5be03892015-12-24 08:44:20 +08001118static void ceph_aio_retry_work(struct work_struct *work)
1119{
1120 struct ceph_aio_work *aio_work =
1121 container_of(work, struct ceph_aio_work, work);
1122 struct ceph_osd_request *orig_req = aio_work->req;
1123 struct ceph_aio_request *aio_req = orig_req->r_priv;
1124 struct inode *inode = orig_req->r_inode;
1125 struct ceph_inode_info *ci = ceph_inode(inode);
1126 struct ceph_snap_context *snapc;
1127 struct ceph_osd_request *req;
1128 int ret;
1129
1130 spin_lock(&ci->i_ceph_lock);
1131 if (__ceph_have_pending_cap_snap(ci)) {
1132 struct ceph_cap_snap *capsnap =
1133 list_last_entry(&ci->i_cap_snaps,
1134 struct ceph_cap_snap,
1135 ci_item);
1136 snapc = ceph_get_snap_context(capsnap->context);
1137 } else {
1138 BUG_ON(!ci->i_head_snapc);
1139 snapc = ceph_get_snap_context(ci->i_head_snapc);
1140 }
1141 spin_unlock(&ci->i_ceph_lock);
1142
Ilya Dryomov61d2f852018-10-11 16:15:38 +02001143 req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 1,
Yan, Zheng5be03892015-12-24 08:44:20 +08001144 false, GFP_NOFS);
Dan Carpenter1418bf02016-01-26 12:24:44 +03001145 if (!req) {
1146 ret = -ENOMEM;
Yan, Zheng5be03892015-12-24 08:44:20 +08001147 req = orig_req;
1148 goto out;
1149 }
1150
Yan, Zhengb178cf42017-08-16 17:27:05 +08001151 req->r_flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001152 ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001153 ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
Yan, Zheng5be03892015-12-24 08:44:20 +08001154
Ilya Dryomov26f887e2018-10-15 16:11:37 +02001155 req->r_ops[0] = orig_req->r_ops[0];
1156
1157 req->r_mtime = aio_req->mtime;
1158 req->r_data_offset = req->r_ops[0].extent.offset;
1159
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001160 ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
1161 if (ret) {
1162 ceph_osdc_put_request(req);
1163 req = orig_req;
1164 goto out;
1165 }
Yan, Zheng5be03892015-12-24 08:44:20 +08001166
Yan, Zheng5be03892015-12-24 08:44:20 +08001167 ceph_osdc_put_request(orig_req);
1168
1169 req->r_callback = ceph_aio_complete_req;
1170 req->r_inode = inode;
1171 req->r_priv = aio_req;
1172
1173 ret = ceph_osdc_start_request(req->r_osdc, req, false);
1174out:
1175 if (ret < 0) {
Yan, Zheng5be03892015-12-24 08:44:20 +08001176 req->r_result = ret;
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001177 ceph_aio_complete_req(req);
Yan, Zheng5be03892015-12-24 08:44:20 +08001178 }
1179
Yan, Zhengdb6aed72016-01-26 23:05:37 +08001180 ceph_put_snap_context(snapc);
Yan, Zheng5be03892015-12-24 08:44:20 +08001181 kfree(aio_work);
1182}
1183
majianpenge8344e62013-09-12 13:54:26 +08001184static ssize_t
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001185ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
1186 struct ceph_snap_context *snapc,
1187 struct ceph_cap_flush **pcf)
Sage Weil124e68e2009-10-06 11:31:08 -07001188{
majianpenge8344e62013-09-12 13:54:26 +08001189 struct file *file = iocb->ki_filp;
Al Viro496ad9a2013-01-23 17:07:38 -05001190 struct inode *inode = file_inode(file);
Sage Weil124e68e2009-10-06 11:31:08 -07001191 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001192 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Alex Elderacead002013-03-14 14:09:05 -05001193 struct ceph_vino vino;
Sage Weil124e68e2009-10-06 11:31:08 -07001194 struct ceph_osd_request *req;
Ilya Dryomovfc218542018-05-04 16:57:31 +02001195 struct bio_vec *bvecs;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001196 struct ceph_aio_request *aio_req = NULL;
1197 int num_pages = 0;
Sage Weil124e68e2009-10-06 11:31:08 -07001198 int flags;
Jeff Layton321fe132019-08-02 13:15:39 -04001199 int ret = 0;
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001200 struct timespec64 mtime = current_time(inode);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001201 size_t count = iov_iter_count(iter);
1202 loff_t pos = iocb->ki_pos;
1203 bool write = iov_iter_rw(iter) == WRITE;
Yan, Zheng85784f92018-03-16 11:22:29 +08001204 bool should_dirty = !write && iter_is_iovec(iter);
Sage Weil124e68e2009-10-06 11:31:08 -07001205
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001206 if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
Sage Weil124e68e2009-10-06 11:31:08 -07001207 return -EROFS;
1208
Yan, Zheng1c0a9c22017-08-16 17:24:58 +08001209 dout("sync_direct_%s on file %p %lld~%u snapc %p seq %lld\n",
1210 (write ? "write" : "read"), file, pos, (unsigned)count,
Jeff Layton40e7e2c2019-04-23 14:18:45 -04001211 snapc, snapc ? snapc->seq : 0);
Sage Weil124e68e2009-10-06 11:31:08 -07001212
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001213 if (write) {
NeilBrown5d7eb1a2016-09-01 22:26:23 +08001214 int ret2 = invalidate_inode_pages2_range(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001215 pos >> PAGE_SHIFT,
zhengbine450f4d2019-02-01 11:19:15 +00001216 (pos + count - 1) >> PAGE_SHIFT);
NeilBrown5d7eb1a2016-09-01 22:26:23 +08001217 if (ret2 < 0)
Zhi Zhanga380a032016-11-08 14:35:59 +01001218 dout("invalidate_inode_pages2_range returned %d\n", ret2);
Yehuda Sadeh29065a52010-02-09 11:14:41 -08001219
Yan, Zhengb178cf42017-08-16 17:27:05 +08001220 flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001221 } else {
1222 flags = CEPH_OSD_FLAG_READ;
1223 }
Sage Weil124e68e2009-10-06 11:31:08 -07001224
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001225 while (iov_iter_count(iter) > 0) {
Ilya Dryomovfc218542018-05-04 16:57:31 +02001226 u64 size = iov_iter_count(iter);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001227 ssize_t len;
majianpenge8344e62013-09-12 13:54:26 +08001228
Ilya Dryomov3a15b382018-05-03 16:10:09 +02001229 if (write)
1230 size = min_t(u64, size, fsc->mount_options->wsize);
1231 else
1232 size = min_t(u64, size, fsc->mount_options->rsize);
1233
majianpenge8344e62013-09-12 13:54:26 +08001234 vino = ceph_vino(inode);
1235 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001236 vino, pos, &size, 0,
Yanhu Cao3fb99d42017-07-21 17:20:10 +08001237 1,
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001238 write ? CEPH_OSD_OP_WRITE :
1239 CEPH_OSD_OP_READ,
1240 flags, snapc,
majianpenge8344e62013-09-12 13:54:26 +08001241 ci->i_truncate_seq,
1242 ci->i_truncate_size,
1243 false);
1244 if (IS_ERR(req)) {
1245 ret = PTR_ERR(req);
Al Viroeab87232014-04-03 22:44:19 -04001246 break;
majianpenge8344e62013-09-12 13:54:26 +08001247 }
1248
Ilya Dryomovfc218542018-05-04 16:57:31 +02001249 len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages);
1250 if (len < 0) {
Al Viro64c31312014-04-03 22:58:25 -04001251 ceph_osdc_put_request(req);
Ilya Dryomovfc218542018-05-04 16:57:31 +02001252 ret = len;
Al Viro64c31312014-04-03 22:58:25 -04001253 break;
Sage Weil124e68e2009-10-06 11:31:08 -07001254 }
Ilya Dryomovfc218542018-05-04 16:57:31 +02001255 if (len != size)
1256 osd_req_op_extent_update(req, 0, len);
Sage Weil124e68e2009-10-06 11:31:08 -07001257
1258 /*
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001259 * To simplify error handling, allow AIO when IO within i_size
1260 * or IO can be satisfied by single OSD request.
Sage Weil124e68e2009-10-06 11:31:08 -07001261 */
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001262 if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
1263 (len == count || pos + count <= i_size_read(inode))) {
1264 aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
1265 if (aio_req) {
1266 aio_req->iocb = iocb;
1267 aio_req->write = write;
Yan, Zheng85784f92018-03-16 11:22:29 +08001268 aio_req->should_dirty = should_dirty;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001269 INIT_LIST_HEAD(&aio_req->osd_reqs);
1270 if (write) {
Yan, Zheng5be03892015-12-24 08:44:20 +08001271 aio_req->mtime = mtime;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001272 swap(aio_req->prealloc_cf, *pcf);
1273 }
1274 }
1275 /* ignore error */
1276 }
majianpenge8344e62013-09-12 13:54:26 +08001277
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001278 if (write) {
1279 /*
1280 * throw out any page cache pages in this range. this
1281 * may block.
1282 */
1283 truncate_inode_pages_range(inode->i_mapping, pos,
Luis Henriquesd31d07b2019-07-01 18:16:34 +01001284 PAGE_ALIGN(pos + len) - 1);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001285
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001286 req->r_mtime = mtime;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001287 }
1288
Ilya Dryomovfc218542018-05-04 16:57:31 +02001289 osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001290
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001291 if (aio_req) {
1292 aio_req->total_len += len;
1293 aio_req->num_reqs++;
1294 atomic_inc(&aio_req->pending_reqs);
1295
1296 req->r_callback = ceph_aio_complete_req;
1297 req->r_inode = inode;
1298 req->r_priv = aio_req;
Ilya Dryomov94e85772019-07-08 12:50:09 +02001299 list_add_tail(&req->r_private_item, &aio_req->osd_reqs);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001300
1301 pos += len;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001302 continue;
1303 }
1304
1305 ret = ceph_osdc_start_request(req->r_osdc, req, false);
majianpenge8344e62013-09-12 13:54:26 +08001306 if (!ret)
1307 ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1308
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001309 size = i_size_read(inode);
1310 if (!write) {
1311 if (ret == -ENOENT)
1312 ret = 0;
1313 if (ret >= 0 && ret < len && pos + ret < size) {
Ilya Dryomovfc218542018-05-04 16:57:31 +02001314 struct iov_iter i;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001315 int zlen = min_t(size_t, len - ret,
1316 size - pos - ret);
Ilya Dryomovfc218542018-05-04 16:57:31 +02001317
David Howellsaa563d72018-10-20 00:57:56 +01001318 iov_iter_bvec(&i, READ, bvecs, num_pages, len);
Ilya Dryomovfc218542018-05-04 16:57:31 +02001319 iov_iter_advance(&i, ret);
1320 iov_iter_zero(zlen, &i);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001321 ret += zlen;
1322 }
1323 if (ret >= 0)
1324 len = ret;
1325 }
1326
Ilya Dryomovfc218542018-05-04 16:57:31 +02001327 put_bvecs(bvecs, num_pages, should_dirty);
majianpenge8344e62013-09-12 13:54:26 +08001328 ceph_osdc_put_request(req);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001329 if (ret < 0)
majianpenge8344e62013-09-12 13:54:26 +08001330 break;
Al Viro64c31312014-04-03 22:58:25 -04001331
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001332 pos += len;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001333 if (!write && pos >= size)
1334 break;
1335
1336 if (write && pos > size) {
1337 if (ceph_inode_set_size(inode, pos))
Al Viro64c31312014-04-03 22:58:25 -04001338 ceph_check_caps(ceph_inode(inode),
1339 CHECK_CAPS_AUTHONLY,
1340 NULL);
1341 }
majianpenge8344e62013-09-12 13:54:26 +08001342 }
1343
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001344 if (aio_req) {
Yan, Zhengfc8c3892016-06-14 11:13:59 +08001345 LIST_HEAD(osd_reqs);
1346
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001347 if (aio_req->num_reqs == 0) {
1348 kfree(aio_req);
1349 return ret;
1350 }
1351
1352 ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
1353 CEPH_CAP_FILE_RD);
1354
Yan, Zhengfc8c3892016-06-14 11:13:59 +08001355 list_splice(&aio_req->osd_reqs, &osd_reqs);
Jeff Layton6a817492019-11-13 09:56:06 -05001356 inode_dio_begin(inode);
Yan, Zhengfc8c3892016-06-14 11:13:59 +08001357 while (!list_empty(&osd_reqs)) {
1358 req = list_first_entry(&osd_reqs,
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001359 struct ceph_osd_request,
Ilya Dryomov94e85772019-07-08 12:50:09 +02001360 r_private_item);
1361 list_del_init(&req->r_private_item);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001362 if (ret >= 0)
1363 ret = ceph_osdc_start_request(req->r_osdc,
1364 req, false);
1365 if (ret < 0) {
1366 req->r_result = ret;
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001367 ceph_aio_complete_req(req);
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001368 }
1369 }
1370 return -EIOCBQUEUED;
1371 }
1372
1373 if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
1374 ret = pos - iocb->ki_pos;
majianpenge8344e62013-09-12 13:54:26 +08001375 iocb->ki_pos = pos;
majianpenge8344e62013-09-12 13:54:26 +08001376 }
1377 return ret;
1378}
1379
majianpenge8344e62013-09-12 13:54:26 +08001380/*
1381 * Synchronous write, straight from __user pointer or user pages.
1382 *
1383 * If write spans object boundary, just do multiple writes. (For a
1384 * correct atomic write, we should e.g. take write locks on all
1385 * objects, rollback on failure, etc.)
1386 */
Yan, Zheng06fee302014-07-28 14:33:46 +08001387static ssize_t
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001388ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
1389 struct ceph_snap_context *snapc)
majianpenge8344e62013-09-12 13:54:26 +08001390{
1391 struct file *file = iocb->ki_filp;
1392 struct inode *inode = file_inode(file);
1393 struct ceph_inode_info *ci = ceph_inode(inode);
1394 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
majianpenge8344e62013-09-12 13:54:26 +08001395 struct ceph_vino vino;
1396 struct ceph_osd_request *req;
1397 struct page **pages;
1398 u64 len;
1399 int num_pages;
1400 int written = 0;
1401 int flags;
majianpenge8344e62013-09-12 13:54:26 +08001402 int ret;
Yan, Zhengefb0ca72017-05-22 12:03:32 +08001403 bool check_caps = false;
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001404 struct timespec64 mtime = current_time(inode);
Al Viro4908b822014-04-03 23:09:01 -04001405 size_t count = iov_iter_count(from);
majianpenge8344e62013-09-12 13:54:26 +08001406
1407 if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
1408 return -EROFS;
1409
Yan, Zheng1c0a9c22017-08-16 17:24:58 +08001410 dout("sync_write on file %p %lld~%u snapc %p seq %lld\n",
1411 file, pos, (unsigned)count, snapc, snapc->seq);
majianpenge8344e62013-09-12 13:54:26 +08001412
zhengbine450f4d2019-02-01 11:19:15 +00001413 ret = filemap_write_and_wait_range(inode->i_mapping,
1414 pos, pos + count - 1);
majianpenge8344e62013-09-12 13:54:26 +08001415 if (ret < 0)
1416 return ret;
1417
1418 ret = invalidate_inode_pages2_range(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001419 pos >> PAGE_SHIFT,
zhengbine450f4d2019-02-01 11:19:15 +00001420 (pos + count - 1) >> PAGE_SHIFT);
majianpenge8344e62013-09-12 13:54:26 +08001421 if (ret < 0)
1422 dout("invalidate_inode_pages2_range returned %d\n", ret);
1423
Yan, Zhengb178cf42017-08-16 17:27:05 +08001424 flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
majianpenge8344e62013-09-12 13:54:26 +08001425
Al Viro4908b822014-04-03 23:09:01 -04001426 while ((len = iov_iter_count(from)) > 0) {
majianpenge8344e62013-09-12 13:54:26 +08001427 size_t left;
1428 int n;
1429
majianpenge8344e62013-09-12 13:54:26 +08001430 vino = ceph_vino(inode);
1431 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
Yan, Zheng715e4cd2014-11-13 14:40:37 +08001432 vino, pos, &len, 0, 1,
majianpenge8344e62013-09-12 13:54:26 +08001433 CEPH_OSD_OP_WRITE, flags, snapc,
1434 ci->i_truncate_seq,
1435 ci->i_truncate_size,
1436 false);
1437 if (IS_ERR(req)) {
1438 ret = PTR_ERR(req);
Al Viroeab87232014-04-03 22:44:19 -04001439 break;
majianpenge8344e62013-09-12 13:54:26 +08001440 }
1441
1442 /*
1443 * write from beginning of first page,
1444 * regardless of io alignment
1445 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001446 num_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
majianpenge8344e62013-09-12 13:54:26 +08001447
Yan, Zheng687265e2015-06-13 17:27:05 +08001448 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
Sage Weil124e68e2009-10-06 11:31:08 -07001449 if (IS_ERR(pages)) {
1450 ret = PTR_ERR(pages);
1451 goto out;
1452 }
majianpenge8344e62013-09-12 13:54:26 +08001453
1454 left = len;
1455 for (n = 0; n < num_pages; n++) {
Ilya Dryomov125d7252014-01-28 19:16:18 +02001456 size_t plen = min_t(size_t, left, PAGE_SIZE);
Al Viro4908b822014-04-03 23:09:01 -04001457 ret = copy_page_from_iter(pages[n], 0, plen, from);
majianpenge8344e62013-09-12 13:54:26 +08001458 if (ret != plen) {
1459 ret = -EFAULT;
1460 break;
1461 }
1462 left -= ret;
majianpenge8344e62013-09-12 13:54:26 +08001463 }
1464
Sage Weil124e68e2009-10-06 11:31:08 -07001465 if (ret < 0) {
1466 ceph_release_page_vector(pages, num_pages);
1467 goto out;
1468 }
1469
majianpenge8344e62013-09-12 13:54:26 +08001470 req->r_inode = inode;
Sage Weil124e68e2009-10-06 11:31:08 -07001471
majianpenge8344e62013-09-12 13:54:26 +08001472 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
1473 false, true);
Alex Elder02ee07d2013-03-14 14:09:06 -05001474
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001475 req->r_mtime = mtime;
majianpenge8344e62013-09-12 13:54:26 +08001476 ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1477 if (!ret)
1478 ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
Sage Weil124e68e2009-10-06 11:31:08 -07001479
1480out:
majianpenge8344e62013-09-12 13:54:26 +08001481 ceph_osdc_put_request(req);
Jeff Layton26544c622017-04-04 08:39:46 -04001482 if (ret != 0) {
1483 ceph_set_error_write(ci);
majianpenge8344e62013-09-12 13:54:26 +08001484 break;
Jeff Layton26544c622017-04-04 08:39:46 -04001485 }
1486
1487 ceph_clear_error_write(ci);
1488 pos += len;
1489 written += len;
1490 if (pos > i_size_read(inode)) {
1491 check_caps = ceph_inode_set_size(inode, pos);
1492 if (check_caps)
1493 ceph_check_caps(ceph_inode(inode),
1494 CHECK_CAPS_AUTHONLY,
1495 NULL);
1496 }
1497
majianpenge8344e62013-09-12 13:54:26 +08001498 }
1499
1500 if (ret != -EOLDSNAPC && written > 0) {
Sage Weil124e68e2009-10-06 11:31:08 -07001501 ret = written;
majianpenge8344e62013-09-12 13:54:26 +08001502 iocb->ki_pos = pos;
Sage Weil124e68e2009-10-06 11:31:08 -07001503 }
1504 return ret;
1505}
1506
1507/*
1508 * Wrap generic_file_aio_read with checks for cap bits on the inode.
1509 * Atomically grab references, so that those bits are not released
1510 * back to the MDS mid-read.
1511 *
1512 * Hmm, the sync read case isn't actually async... should it be?
1513 */
Al Viro36444242014-04-02 20:28:01 -04001514static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
Sage Weil124e68e2009-10-06 11:31:08 -07001515{
1516 struct file *filp = iocb->ki_filp;
Sage Weil29625072010-05-27 10:40:43 -07001517 struct ceph_file_info *fi = filp->private_data;
Christoph Hellwig66ee59a2015-02-11 19:56:46 +01001518 size_t len = iov_iter_count(to);
Al Viro496ad9a2013-01-23 17:07:38 -05001519 struct inode *inode = file_inode(filp);
Sage Weil124e68e2009-10-06 11:31:08 -07001520 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng3738daa2014-11-14 22:10:07 +08001521 struct page *pinned_page = NULL;
Sage Weil124e68e2009-10-06 11:31:08 -07001522 ssize_t ret;
Sage Weil29625072010-05-27 10:40:43 -07001523 int want, got = 0;
Yan, Zheng83701242014-11-14 22:36:18 +08001524 int retry_op = 0, read = 0;
Sage Weil124e68e2009-10-06 11:31:08 -07001525
Sage Weil6a026582010-02-09 14:04:02 -08001526again:
majianpeng8eb4efb2013-09-26 14:42:17 +08001527 dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
1528 inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
1529
Jeff Laytona81bc312019-11-13 09:10:27 -05001530 if (iocb->ki_flags & IOCB_DIRECT)
1531 ceph_start_io_direct(inode);
1532 else
1533 ceph_start_io_read(inode);
1534
Sage Weil29625072010-05-27 10:40:43 -07001535 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1536 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1537 else
1538 want = CEPH_CAP_FILE_CACHE;
Yan, Zheng5e3ded12019-07-25 20:16:43 +08001539 ret = ceph_get_caps(filp, CEPH_CAP_FILE_RD, want, -1,
1540 &got, &pinned_page);
Jeff Laytona81bc312019-11-13 09:10:27 -05001541 if (ret < 0) {
1542 if (iocb->ki_flags & IOCB_DIRECT)
1543 ceph_end_io_direct(inode);
1544 else
1545 ceph_end_io_read(inode);
majianpeng8eb4efb2013-09-26 14:42:17 +08001546 return ret;
Jeff Laytona81bc312019-11-13 09:10:27 -05001547 }
Sage Weil124e68e2009-10-06 11:31:08 -07001548
Sage Weil29625072010-05-27 10:40:43 -07001549 if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
Al Viro2ba48ce2015-04-09 13:52:01 -04001550 (iocb->ki_flags & IOCB_DIRECT) ||
majianpeng8eb4efb2013-09-26 14:42:17 +08001551 (fi->flags & CEPH_F_SYNC)) {
Sage Weil124e68e2009-10-06 11:31:08 -07001552
majianpeng8eb4efb2013-09-26 14:42:17 +08001553 dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
1554 inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
1555 ceph_cap_string(got));
1556
Yan, Zheng83701242014-11-14 22:36:18 +08001557 if (ci->i_inline_version == CEPH_INLINE_NONE) {
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001558 if (!retry_op && (iocb->ki_flags & IOCB_DIRECT)) {
1559 ret = ceph_direct_read_write(iocb, to,
1560 NULL, NULL);
1561 if (ret >= 0 && ret < len)
1562 retry_op = CHECK_EOF;
1563 } else {
1564 ret = ceph_sync_read(iocb, to, &retry_op);
1565 }
Yan, Zheng83701242014-11-14 22:36:18 +08001566 } else {
1567 retry_op = READ_INLINE;
1568 }
majianpeng8eb4efb2013-09-26 14:42:17 +08001569 } else {
Yan, Zheng5d988302017-12-15 11:15:36 +08001570 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
majianpeng8eb4efb2013-09-26 14:42:17 +08001571 dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
Al Viro36444242014-04-02 20:28:01 -04001572 inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
majianpeng8eb4efb2013-09-26 14:42:17 +08001573 ceph_cap_string(got));
Yan, Zheng5d988302017-12-15 11:15:36 +08001574 ceph_add_rw_context(fi, &rw_ctx);
Al Viro36444242014-04-02 20:28:01 -04001575 ret = generic_file_read_iter(iocb, to);
Yan, Zheng5d988302017-12-15 11:15:36 +08001576 ceph_del_rw_context(fi, &rw_ctx);
majianpeng8eb4efb2013-09-26 14:42:17 +08001577 }
Jeff Laytona81bc312019-11-13 09:10:27 -05001578
Sage Weil124e68e2009-10-06 11:31:08 -07001579 dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
1580 inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
Yan, Zheng3738daa2014-11-14 22:10:07 +08001581 if (pinned_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001582 put_page(pinned_page);
Yan, Zheng3738daa2014-11-14 22:10:07 +08001583 pinned_page = NULL;
1584 }
Sage Weil124e68e2009-10-06 11:31:08 -07001585 ceph_put_cap_refs(ci, got);
Jeff Laytona81bc312019-11-13 09:10:27 -05001586
1587 if (iocb->ki_flags & IOCB_DIRECT)
1588 ceph_end_io_direct(inode);
1589 else
1590 ceph_end_io_read(inode);
1591
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001592 if (retry_op > HAVE_RETRIED && ret >= 0) {
Yan, Zheng83701242014-11-14 22:36:18 +08001593 int statret;
1594 struct page *page = NULL;
1595 loff_t i_size;
1596 if (retry_op == READ_INLINE) {
Yan, Zheng687265e2015-06-13 17:27:05 +08001597 page = __page_cache_alloc(GFP_KERNEL);
Yan, Zheng83701242014-11-14 22:36:18 +08001598 if (!page)
1599 return -ENOMEM;
1600 }
Sage Weil6a026582010-02-09 14:04:02 -08001601
Yan, Zheng83701242014-11-14 22:36:18 +08001602 statret = __ceph_do_getattr(inode, page,
1603 CEPH_STAT_CAP_INLINE_DATA, !!page);
1604 if (statret < 0) {
Nikolay Borisov0d7718f62016-10-10 15:38:18 +03001605 if (page)
1606 __free_page(page);
Yan, Zheng83701242014-11-14 22:36:18 +08001607 if (statret == -ENODATA) {
1608 BUG_ON(retry_op != READ_INLINE);
1609 goto again;
1610 }
1611 return statret;
1612 }
1613
1614 i_size = i_size_read(inode);
1615 if (retry_op == READ_INLINE) {
Yan, Zhengfcc02d22015-01-10 11:43:12 +08001616 BUG_ON(ret > 0 || read > 0);
1617 if (iocb->ki_pos < i_size &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001618 iocb->ki_pos < PAGE_SIZE) {
Yan, Zheng83701242014-11-14 22:36:18 +08001619 loff_t end = min_t(loff_t, i_size,
1620 iocb->ki_pos + len);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001621 end = min_t(loff_t, end, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +08001622 if (statret < end)
1623 zero_user_segment(page, statret, end);
1624 ret = copy_page_to_iter(page,
1625 iocb->ki_pos & ~PAGE_MASK,
1626 end - iocb->ki_pos, to);
1627 iocb->ki_pos += ret;
Yan, Zhengfcc02d22015-01-10 11:43:12 +08001628 read += ret;
1629 }
1630 if (iocb->ki_pos < i_size && read < len) {
1631 size_t zlen = min_t(size_t, len - read,
1632 i_size - iocb->ki_pos);
1633 ret = iov_iter_zero(zlen, to);
1634 iocb->ki_pos += ret;
1635 read += ret;
Yan, Zheng83701242014-11-14 22:36:18 +08001636 }
1637 __free_pages(page, 0);
Yan, Zhengfcc02d22015-01-10 11:43:12 +08001638 return read;
Yan, Zheng83701242014-11-14 22:36:18 +08001639 }
Sage Weil6a026582010-02-09 14:04:02 -08001640
1641 /* hit EOF or hole? */
Yan, Zheng83701242014-11-14 22:36:18 +08001642 if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
Yan, Zhengfcc02d22015-01-10 11:43:12 +08001643 ret < len) {
majianpeng8eb4efb2013-09-26 14:42:17 +08001644 dout("sync_read hit hole, ppos %lld < size %lld"
Yan, Zheng99c88e62015-12-30 11:32:46 +08001645 ", reading more\n", iocb->ki_pos, i_size);
majianpeng8eb4efb2013-09-26 14:42:17 +08001646
Sage Weil6a026582010-02-09 14:04:02 -08001647 read += ret;
Sage Weil6a026582010-02-09 14:04:02 -08001648 len -= ret;
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001649 retry_op = HAVE_RETRIED;
Sage Weil6a026582010-02-09 14:04:02 -08001650 goto again;
1651 }
1652 }
majianpeng8eb4efb2013-09-26 14:42:17 +08001653
Sage Weil6a026582010-02-09 14:04:02 -08001654 if (ret >= 0)
1655 ret += read;
1656
Sage Weil124e68e2009-10-06 11:31:08 -07001657 return ret;
1658}
1659
1660/*
1661 * Take cap references to avoid releasing caps to MDS mid-write.
1662 *
1663 * If we are synchronous, and write with an old snap context, the OSD
1664 * may return EOLDSNAPC. In that case, retry the write.. _after_
1665 * dropping our cap refs and allowing the pending snap to logically
1666 * complete _before_ this write occurs.
1667 *
1668 * If we are near ENOSPC, write synchronously.
1669 */
Al Viro4908b822014-04-03 23:09:01 -04001670static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
Sage Weil124e68e2009-10-06 11:31:08 -07001671{
1672 struct file *file = iocb->ki_filp;
Sage Weil33caad32010-05-26 14:31:27 -07001673 struct ceph_file_info *fi = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05001674 struct inode *inode = file_inode(file);
Sage Weil124e68e2009-10-06 11:31:08 -07001675 struct ceph_inode_info *ci = ceph_inode(inode);
Chengguang Xu8687a3e2018-07-19 22:15:26 +08001676 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Ilya Dryomov76142092020-03-09 12:03:14 +01001677 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001678 struct ceph_cap_flush *prealloc_cf;
Al Viro3309dd02015-04-09 12:55:47 -04001679 ssize_t count, written = 0;
Yan, Zheng03d254e2013-04-12 16:11:13 +08001680 int err, want, got;
Xiubo Li8e4473b2020-02-03 21:28:25 -05001681 bool direct_lock = false;
Ilya Dryomov76142092020-03-09 12:03:14 +01001682 u32 map_flags;
1683 u64 pool_flags;
Al Viro3309dd02015-04-09 12:55:47 -04001684 loff_t pos;
Chengguang Xu8687a3e2018-07-19 22:15:26 +08001685 loff_t limit = max(i_size_read(inode), fsc->max_file_size);
Sage Weil124e68e2009-10-06 11:31:08 -07001686
1687 if (ceph_snap(inode) != CEPH_NOSNAP)
1688 return -EROFS;
1689
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001690 prealloc_cf = ceph_alloc_cap_flush();
1691 if (!prealloc_cf)
1692 return -ENOMEM;
1693
Xiubo Li8e4473b2020-02-03 21:28:25 -05001694 if ((iocb->ki_flags & (IOCB_DIRECT | IOCB_APPEND)) == IOCB_DIRECT)
1695 direct_lock = true;
1696
Yan, Zhenga5cd74a2017-08-14 10:50:50 +08001697retry_snap:
Xiubo Li8e4473b2020-02-03 21:28:25 -05001698 if (direct_lock)
Jeff Layton321fe132019-08-02 13:15:39 -04001699 ceph_start_io_direct(inode);
1700 else
1701 ceph_start_io_write(inode);
Yan, Zheng03d254e2013-04-12 16:11:13 +08001702
Yan, Zheng03d254e2013-04-12 16:11:13 +08001703 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001704 current->backing_dev_info = inode_to_bdi(inode);
Yan, Zheng03d254e2013-04-12 16:11:13 +08001705
Yan, Zheng55b0b312015-09-07 11:35:01 +08001706 if (iocb->ki_flags & IOCB_APPEND) {
1707 err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
1708 if (err < 0)
1709 goto out;
1710 }
1711
Al Viro3309dd02015-04-09 12:55:47 -04001712 err = generic_write_checks(iocb, from);
1713 if (err <= 0)
Yan, Zheng03d254e2013-04-12 16:11:13 +08001714 goto out;
1715
Al Viro3309dd02015-04-09 12:55:47 -04001716 pos = iocb->ki_pos;
Chengguang Xu8687a3e2018-07-19 22:15:26 +08001717 if (unlikely(pos >= limit)) {
1718 err = -EFBIG;
1719 goto out;
1720 } else {
1721 iov_iter_truncate(from, limit - pos);
1722 }
1723
Al Viro3309dd02015-04-09 12:55:47 -04001724 count = iov_iter_count(from);
Luis Henriques2b838452018-01-05 10:47:21 +00001725 if (ceph_quota_is_max_bytes_exceeded(inode, pos + count)) {
1726 err = -EDQUOT;
1727 goto out;
1728 }
1729
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001730 err = file_remove_privs(file);
Yan, Zheng03d254e2013-04-12 16:11:13 +08001731 if (err)
1732 goto out;
1733
1734 err = file_update_time(file);
1735 if (err)
1736 goto out;
1737
Jeff Layton5c308352019-06-06 08:57:27 -04001738 inode_inc_iversion_raw(inode);
1739
Yan, Zheng28127bd2014-11-14 22:38:29 +08001740 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1741 err = ceph_uninline_data(file, NULL);
1742 if (err < 0)
1743 goto out;
1744 }
1745
Ilya Dryomov76142092020-03-09 12:03:14 +01001746 down_read(&osdc->lock);
1747 map_flags = osdc->osdmap->flags;
1748 pool_flags = ceph_pg_pool_flags(osdc->osdmap, ci->i_layout.pool_id);
1749 up_read(&osdc->lock);
1750 if ((map_flags & CEPH_OSDMAP_FULL) ||
1751 (pool_flags & CEPH_POOL_FLAG_FULL)) {
Yan, Zheng03d254e2013-04-12 16:11:13 +08001752 err = -ENOSPC;
Yan, Zheng6070e0c2013-03-01 10:55:39 +08001753 goto out;
1754 }
Yan, Zheng03d254e2013-04-12 16:11:13 +08001755
Randy Dunlapac7f29b2013-04-19 14:20:07 -07001756 dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
Yan, Zheng99c88e62015-12-30 11:32:46 +08001757 inode, ceph_vinop(inode), pos, count, i_size_read(inode));
Sage Weil7971bd92013-05-01 21:15:58 -07001758 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1759 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1760 else
1761 want = CEPH_CAP_FILE_BUFFER;
Yan, Zheng03d254e2013-04-12 16:11:13 +08001762 got = 0;
Yan, Zheng5e3ded12019-07-25 20:16:43 +08001763 err = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, pos + count,
Yan, Zheng3738daa2014-11-14 22:10:07 +08001764 &got, NULL);
Yan, Zheng03d254e2013-04-12 16:11:13 +08001765 if (err < 0)
Yan, Zheng37505d52013-04-12 16:11:10 +08001766 goto out;
Sage Weil124e68e2009-10-06 11:31:08 -07001767
Randy Dunlapac7f29b2013-04-19 14:20:07 -07001768 dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
Yan, Zheng03d254e2013-04-12 16:11:13 +08001769 inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
Sage Weil7971bd92013-05-01 21:15:58 -07001770
1771 if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
Jeff Layton26544c622017-04-04 08:39:46 -04001772 (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC) ||
1773 (ci->i_ceph_flags & CEPH_I_ERROR_WRITE)) {
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001774 struct ceph_snap_context *snapc;
Al Viro4908b822014-04-03 23:09:01 -04001775 struct iov_iter data;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001776
1777 spin_lock(&ci->i_ceph_lock);
1778 if (__ceph_have_pending_cap_snap(ci)) {
1779 struct ceph_cap_snap *capsnap =
1780 list_last_entry(&ci->i_cap_snaps,
1781 struct ceph_cap_snap,
1782 ci_item);
1783 snapc = ceph_get_snap_context(capsnap->context);
1784 } else {
1785 BUG_ON(!ci->i_head_snapc);
1786 snapc = ceph_get_snap_context(ci->i_head_snapc);
1787 }
1788 spin_unlock(&ci->i_ceph_lock);
1789
Al Viro4908b822014-04-03 23:09:01 -04001790 /* we might need to revert back to that point */
1791 data = *from;
Xiubo Li8e4473b2020-02-03 21:28:25 -05001792 if (iocb->ki_flags & IOCB_DIRECT)
Yan, Zhengc8fe9b12015-12-23 21:23:38 +08001793 written = ceph_direct_read_write(iocb, &data, snapc,
1794 &prealloc_cf);
Xiubo Li8e4473b2020-02-03 21:28:25 -05001795 else
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001796 written = ceph_sync_write(iocb, &data, pos, snapc);
Xiubo Li8e4473b2020-02-03 21:28:25 -05001797 if (direct_lock)
1798 ceph_end_io_direct(inode);
1799 else
Jeff Layton321fe132019-08-02 13:15:39 -04001800 ceph_end_io_write(inode);
Al Viro4908b822014-04-03 23:09:01 -04001801 if (written > 0)
1802 iov_iter_advance(from, written);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001803 ceph_put_snap_context(snapc);
Sage Weil7971bd92013-05-01 21:15:58 -07001804 } else {
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001805 /*
1806 * No need to acquire the i_truncate_mutex. Because
1807 * the MDS revokes Fwb caps before sending truncate
1808 * message to us. We can't get Fwb cap while there
1809 * are pending vmtruncate. So write and vmtruncate
1810 * can not run at the same time
1811 */
Al Viro4908b822014-04-03 23:09:01 -04001812 written = generic_perform_write(file, from, pos);
Al Viroaec605f42014-02-11 22:28:43 -05001813 if (likely(written >= 0))
1814 iocb->ki_pos = pos + written;
Jeff Layton321fe132019-08-02 13:15:39 -04001815 ceph_end_io_write(inode);
Sage Weil124e68e2009-10-06 11:31:08 -07001816 }
Sage Weild8de9ab2011-07-26 11:27:34 -07001817
Yan, Zheng03d254e2013-04-12 16:11:13 +08001818 if (written >= 0) {
Sage Weilfca65b42011-05-04 11:33:47 -07001819 int dirty;
Luis Henriques1ab302a2018-01-05 10:47:22 +00001820
Sage Weilbe655592011-11-30 09:47:09 -08001821 spin_lock(&ci->i_ceph_lock);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001822 ci->i_inline_version = CEPH_INLINE_NONE;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001823 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1824 &prealloc_cf);
Sage Weilbe655592011-11-30 09:47:09 -08001825 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -07001826 if (dirty)
1827 __mark_inode_dirty(inode, dirty);
Luis Henriques1ab302a2018-01-05 10:47:22 +00001828 if (ceph_quota_is_max_bytes_approaching(inode, iocb->ki_pos))
Yan, Zhenga0d93e32020-03-05 20:21:01 +08001829 ceph_check_caps(ci, 0, NULL);
Sage Weil124e68e2009-10-06 11:31:08 -07001830 }
Sage Weil7971bd92013-05-01 21:15:58 -07001831
Sage Weil124e68e2009-10-06 11:31:08 -07001832 dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
Al Viro4908b822014-04-03 23:09:01 -04001833 inode, ceph_vinop(inode), pos, (unsigned)count,
Sage Weil7971bd92013-05-01 21:15:58 -07001834 ceph_cap_string(got));
Sage Weil124e68e2009-10-06 11:31:08 -07001835 ceph_put_cap_refs(ci, got);
Sage Weil7971bd92013-05-01 21:15:58 -07001836
Yan, Zhenga5cd74a2017-08-14 10:50:50 +08001837 if (written == -EOLDSNAPC) {
1838 dout("aio_write %p %llx.%llx %llu~%u" "got EOLDSNAPC, retrying\n",
1839 inode, ceph_vinop(inode), pos, (unsigned)count);
1840 goto retry_snap;
1841 }
1842
Christoph Hellwig6aa657c2016-04-07 08:52:02 -07001843 if (written >= 0) {
Ilya Dryomov76142092020-03-09 12:03:14 +01001844 if ((map_flags & CEPH_OSDMAP_NEARFULL) ||
1845 (pool_flags & CEPH_POOL_FLAG_NEARFULL))
Christoph Hellwig6aa657c2016-04-07 08:52:02 -07001846 iocb->ki_flags |= IOCB_DSYNC;
Christoph Hellwig6aa657c2016-04-07 08:52:02 -07001847 written = generic_write_sync(iocb, written);
Yan, Zheng6070e0c2013-03-01 10:55:39 +08001848 }
Yan, Zheng03d254e2013-04-12 16:11:13 +08001849
Sage Weil2f75e9e2013-08-09 09:57:58 -07001850 goto out_unlocked;
Sage Weil2f75e9e2013-08-09 09:57:58 -07001851out:
Xiubo Li8e4473b2020-02-03 21:28:25 -05001852 if (direct_lock)
Jeff Layton321fe132019-08-02 13:15:39 -04001853 ceph_end_io_direct(inode);
1854 else
1855 ceph_end_io_write(inode);
Sage Weil2f75e9e2013-08-09 09:57:58 -07001856out_unlocked:
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001857 ceph_free_cap_flush(prealloc_cf);
Sage Weil2f75e9e2013-08-09 09:57:58 -07001858 current->backing_dev_info = NULL;
Yan, Zheng03d254e2013-04-12 16:11:13 +08001859 return written ? written : err;
Sage Weil124e68e2009-10-06 11:31:08 -07001860}
1861
1862/*
1863 * llseek. be sure to verify file size on SEEK_END.
1864 */
Andrew Morton965c8e52012-12-17 15:59:39 -08001865static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
Sage Weil124e68e2009-10-06 11:31:08 -07001866{
1867 struct inode *inode = file->f_mapping->host;
Chengguang Xu9da12e32018-07-19 22:15:27 +08001868 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Yan, Zheng99c88e62015-12-30 11:32:46 +08001869 loff_t i_size;
Phil Turnbull955818c2016-07-21 13:43:09 -04001870 loff_t ret;
Sage Weil124e68e2009-10-06 11:31:08 -07001871
Al Viro59551022016-01-22 15:40:57 -05001872 inode_lock(inode);
Sage Weil6a82c472011-12-13 09:19:26 -08001873
Andrew Morton965c8e52012-12-17 15:59:39 -08001874 if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
Yan, Zheng508b32d2014-09-16 21:46:17 +08001875 ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
Phil Turnbull955818c2016-07-21 13:43:09 -04001876 if (ret < 0)
Sage Weil124e68e2009-10-06 11:31:08 -07001877 goto out;
Josef Bacik06222e42011-07-18 13:21:38 -04001878 }
1879
Yan, Zheng99c88e62015-12-30 11:32:46 +08001880 i_size = i_size_read(inode);
Andrew Morton965c8e52012-12-17 15:59:39 -08001881 switch (whence) {
Josef Bacik06222e42011-07-18 13:21:38 -04001882 case SEEK_END:
Yan, Zheng99c88e62015-12-30 11:32:46 +08001883 offset += i_size;
Sage Weil124e68e2009-10-06 11:31:08 -07001884 break;
1885 case SEEK_CUR:
1886 /*
1887 * Here we special-case the lseek(fd, 0, SEEK_CUR)
1888 * position-querying operation. Avoid rewriting the "same"
1889 * f_pos value back to the file because a concurrent read(),
1890 * write() or lseek() might have altered it
1891 */
1892 if (offset == 0) {
Phil Turnbull955818c2016-07-21 13:43:09 -04001893 ret = file->f_pos;
Sage Weil124e68e2009-10-06 11:31:08 -07001894 goto out;
1895 }
1896 offset += file->f_pos;
1897 break;
Josef Bacik06222e42011-07-18 13:21:38 -04001898 case SEEK_DATA:
Luis Henriques397f2382017-07-28 11:56:40 +01001899 if (offset < 0 || offset >= i_size) {
Josef Bacik06222e42011-07-18 13:21:38 -04001900 ret = -ENXIO;
1901 goto out;
1902 }
1903 break;
1904 case SEEK_HOLE:
Luis Henriques397f2382017-07-28 11:56:40 +01001905 if (offset < 0 || offset >= i_size) {
Josef Bacik06222e42011-07-18 13:21:38 -04001906 ret = -ENXIO;
1907 goto out;
1908 }
Yan, Zheng99c88e62015-12-30 11:32:46 +08001909 offset = i_size;
Josef Bacik06222e42011-07-18 13:21:38 -04001910 break;
Sage Weil124e68e2009-10-06 11:31:08 -07001911 }
1912
Chengguang Xu9da12e32018-07-19 22:15:27 +08001913 ret = vfs_setpos(file, offset, max(i_size, fsc->max_file_size));
Sage Weil124e68e2009-10-06 11:31:08 -07001914
1915out:
Al Viro59551022016-01-22 15:40:57 -05001916 inode_unlock(inode);
Phil Turnbull955818c2016-07-21 13:43:09 -04001917 return ret;
Sage Weil124e68e2009-10-06 11:31:08 -07001918}
1919
Li Wangad7a60d2013-08-15 11:51:44 +08001920static inline void ceph_zero_partial_page(
1921 struct inode *inode, loff_t offset, unsigned size)
1922{
1923 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001924 pgoff_t index = offset >> PAGE_SHIFT;
Li Wangad7a60d2013-08-15 11:51:44 +08001925
1926 page = find_lock_page(inode->i_mapping, index);
1927 if (page) {
1928 wait_on_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001929 zero_user(page, offset & (PAGE_SIZE - 1), size);
Li Wangad7a60d2013-08-15 11:51:44 +08001930 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001931 put_page(page);
Li Wangad7a60d2013-08-15 11:51:44 +08001932 }
1933}
1934
1935static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
1936 loff_t length)
1937{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001938 loff_t nearly = round_up(offset, PAGE_SIZE);
Li Wangad7a60d2013-08-15 11:51:44 +08001939 if (offset < nearly) {
1940 loff_t size = nearly - offset;
1941 if (length < size)
1942 size = length;
1943 ceph_zero_partial_page(inode, offset, size);
1944 offset += size;
1945 length -= size;
1946 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001947 if (length >= PAGE_SIZE) {
1948 loff_t size = round_down(length, PAGE_SIZE);
Li Wangad7a60d2013-08-15 11:51:44 +08001949 truncate_pagecache_range(inode, offset, offset + size - 1);
1950 offset += size;
1951 length -= size;
1952 }
1953 if (length)
1954 ceph_zero_partial_page(inode, offset, length);
1955}
1956
1957static int ceph_zero_partial_object(struct inode *inode,
1958 loff_t offset, loff_t *length)
1959{
1960 struct ceph_inode_info *ci = ceph_inode(inode);
1961 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1962 struct ceph_osd_request *req;
1963 int ret = 0;
1964 loff_t zero = 0;
1965 int op;
1966
1967 if (!length) {
1968 op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
1969 length = &zero;
1970 } else {
1971 op = CEPH_OSD_OP_ZERO;
1972 }
1973
1974 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1975 ceph_vino(inode),
1976 offset, length,
Yan, Zheng715e4cd2014-11-13 14:40:37 +08001977 0, 1, op,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001978 CEPH_OSD_FLAG_WRITE,
Li Wangad7a60d2013-08-15 11:51:44 +08001979 NULL, 0, 0, false);
1980 if (IS_ERR(req)) {
1981 ret = PTR_ERR(req);
1982 goto out;
1983 }
1984
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001985 req->r_mtime = inode->i_mtime;
Li Wangad7a60d2013-08-15 11:51:44 +08001986 ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1987 if (!ret) {
1988 ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1989 if (ret == -ENOENT)
1990 ret = 0;
1991 }
1992 ceph_osdc_put_request(req);
1993
1994out:
1995 return ret;
1996}
1997
1998static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
1999{
2000 int ret = 0;
2001 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng76271512016-02-03 21:24:49 +08002002 s32 stripe_unit = ci->i_layout.stripe_unit;
2003 s32 stripe_count = ci->i_layout.stripe_count;
2004 s32 object_size = ci->i_layout.object_size;
Sage Weilb314a902013-08-27 12:15:16 -07002005 u64 object_set_size = object_size * stripe_count;
2006 u64 nearly, t;
Li Wangad7a60d2013-08-15 11:51:44 +08002007
Sage Weilb314a902013-08-27 12:15:16 -07002008 /* round offset up to next period boundary */
2009 nearly = offset + object_set_size - 1;
2010 t = nearly;
2011 nearly -= do_div(t, object_set_size);
2012
Li Wangad7a60d2013-08-15 11:51:44 +08002013 while (length && offset < nearly) {
2014 loff_t size = length;
2015 ret = ceph_zero_partial_object(inode, offset, &size);
2016 if (ret < 0)
2017 return ret;
2018 offset += size;
2019 length -= size;
2020 }
2021 while (length >= object_set_size) {
2022 int i;
2023 loff_t pos = offset;
2024 for (i = 0; i < stripe_count; ++i) {
2025 ret = ceph_zero_partial_object(inode, pos, NULL);
2026 if (ret < 0)
2027 return ret;
2028 pos += stripe_unit;
2029 }
2030 offset += object_set_size;
2031 length -= object_set_size;
2032 }
2033 while (length) {
2034 loff_t size = length;
2035 ret = ceph_zero_partial_object(inode, offset, &size);
2036 if (ret < 0)
2037 return ret;
2038 offset += size;
2039 length -= size;
2040 }
2041 return ret;
2042}
2043
2044static long ceph_fallocate(struct file *file, int mode,
2045 loff_t offset, loff_t length)
2046{
2047 struct ceph_file_info *fi = file->private_data;
Libo Chenaa8b60e2013-12-11 13:49:11 +08002048 struct inode *inode = file_inode(file);
Li Wangad7a60d2013-08-15 11:51:44 +08002049 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08002050 struct ceph_cap_flush *prealloc_cf;
Li Wangad7a60d2013-08-15 11:51:44 +08002051 int want, got = 0;
2052 int dirty;
2053 int ret = 0;
2054 loff_t endoff = 0;
2055 loff_t size;
2056
Luis Henriquesbddff632018-10-09 18:54:28 +01002057 if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
Yan, Zheng494d77b2014-06-26 15:25:17 +08002058 return -EOPNOTSUPP;
2059
Li Wangad7a60d2013-08-15 11:51:44 +08002060 if (!S_ISREG(inode->i_mode))
2061 return -EOPNOTSUPP;
2062
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08002063 prealloc_cf = ceph_alloc_cap_flush();
2064 if (!prealloc_cf)
2065 return -ENOMEM;
2066
Al Viro59551022016-01-22 15:40:57 -05002067 inode_lock(inode);
Li Wangad7a60d2013-08-15 11:51:44 +08002068
2069 if (ceph_snap(inode) != CEPH_NOSNAP) {
2070 ret = -EROFS;
2071 goto unlock;
2072 }
2073
Yan, Zheng28127bd2014-11-14 22:38:29 +08002074 if (ci->i_inline_version != CEPH_INLINE_NONE) {
2075 ret = ceph_uninline_data(file, NULL);
2076 if (ret < 0)
2077 goto unlock;
2078 }
2079
Li Wangad7a60d2013-08-15 11:51:44 +08002080 size = i_size_read(inode);
Luis Henriquesbddff632018-10-09 18:54:28 +01002081
2082 /* Are we punching a hole beyond EOF? */
2083 if (offset >= size)
2084 goto unlock;
2085 if ((offset + length) > size)
2086 length = size - offset;
Li Wangad7a60d2013-08-15 11:51:44 +08002087
2088 if (fi->fmode & CEPH_FILE_MODE_LAZY)
2089 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
2090 else
2091 want = CEPH_CAP_FILE_BUFFER;
2092
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002093 ret = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, endoff, &got, NULL);
Li Wangad7a60d2013-08-15 11:51:44 +08002094 if (ret < 0)
2095 goto unlock;
2096
Luis Henriquesbddff632018-10-09 18:54:28 +01002097 ceph_zero_pagecache_range(inode, offset, length);
2098 ret = ceph_zero_objects(inode, offset, length);
Li Wangad7a60d2013-08-15 11:51:44 +08002099
2100 if (!ret) {
2101 spin_lock(&ci->i_ceph_lock);
Yan, Zheng28127bd2014-11-14 22:38:29 +08002102 ci->i_inline_version = CEPH_INLINE_NONE;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08002103 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
2104 &prealloc_cf);
Li Wangad7a60d2013-08-15 11:51:44 +08002105 spin_unlock(&ci->i_ceph_lock);
2106 if (dirty)
2107 __mark_inode_dirty(inode, dirty);
2108 }
2109
2110 ceph_put_cap_refs(ci, got);
2111unlock:
Al Viro59551022016-01-22 15:40:57 -05002112 inode_unlock(inode);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08002113 ceph_free_cap_flush(prealloc_cf);
Li Wangad7a60d2013-08-15 11:51:44 +08002114 return ret;
2115}
2116
Luis Henriques503f82a2018-10-15 16:45:59 +01002117/*
2118 * This function tries to get FILE_WR capabilities for dst_ci and FILE_RD for
2119 * src_ci. Two attempts are made to obtain both caps, and an error is return if
2120 * this fails; zero is returned on success.
2121 */
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002122static int get_rd_wr_caps(struct file *src_filp, int *src_got,
2123 struct file *dst_filp,
Luis Henriques503f82a2018-10-15 16:45:59 +01002124 loff_t dst_endoff, int *dst_got)
2125{
2126 int ret = 0;
2127 bool retrying = false;
2128
2129retry_caps:
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002130 ret = ceph_get_caps(dst_filp, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER,
Luis Henriques503f82a2018-10-15 16:45:59 +01002131 dst_endoff, dst_got, NULL);
2132 if (ret < 0)
2133 return ret;
2134
2135 /*
2136 * Since we're already holding the FILE_WR capability for the dst file,
2137 * we would risk a deadlock by using ceph_get_caps. Thus, we'll do some
2138 * retry dance instead to try to get both capabilities.
2139 */
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002140 ret = ceph_try_get_caps(file_inode(src_filp),
2141 CEPH_CAP_FILE_RD, CEPH_CAP_FILE_SHARED,
Luis Henriques503f82a2018-10-15 16:45:59 +01002142 false, src_got);
2143 if (ret <= 0) {
2144 /* Start by dropping dst_ci caps and getting src_ci caps */
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002145 ceph_put_cap_refs(ceph_inode(file_inode(dst_filp)), *dst_got);
Luis Henriques503f82a2018-10-15 16:45:59 +01002146 if (retrying) {
2147 if (!ret)
2148 /* ceph_try_get_caps masks EAGAIN */
2149 ret = -EAGAIN;
2150 return ret;
2151 }
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002152 ret = ceph_get_caps(src_filp, CEPH_CAP_FILE_RD,
2153 CEPH_CAP_FILE_SHARED, -1, src_got, NULL);
Luis Henriques503f82a2018-10-15 16:45:59 +01002154 if (ret < 0)
2155 return ret;
2156 /*... drop src_ci caps too, and retry */
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002157 ceph_put_cap_refs(ceph_inode(file_inode(src_filp)), *src_got);
Luis Henriques503f82a2018-10-15 16:45:59 +01002158 retrying = true;
2159 goto retry_caps;
2160 }
2161 return ret;
2162}
2163
2164static void put_rd_wr_caps(struct ceph_inode_info *src_ci, int src_got,
2165 struct ceph_inode_info *dst_ci, int dst_got)
2166{
2167 ceph_put_cap_refs(src_ci, src_got);
2168 ceph_put_cap_refs(dst_ci, dst_got);
2169}
2170
2171/*
2172 * This function does several size-related checks, returning an error if:
2173 * - source file is smaller than off+len
2174 * - destination file size is not OK (inode_newsize_ok())
2175 * - max bytes quotas is exceeded
2176 */
2177static int is_file_size_ok(struct inode *src_inode, struct inode *dst_inode,
2178 loff_t src_off, loff_t dst_off, size_t len)
2179{
2180 loff_t size, endoff;
2181
2182 size = i_size_read(src_inode);
2183 /*
2184 * Don't copy beyond source file EOF. Instead of simply setting length
2185 * to (size - src_off), just drop to VFS default implementation, as the
2186 * local i_size may be stale due to other clients writing to the source
2187 * inode.
2188 */
2189 if (src_off + len > size) {
2190 dout("Copy beyond EOF (%llu + %zu > %llu)\n",
2191 src_off, len, size);
2192 return -EOPNOTSUPP;
2193 }
2194 size = i_size_read(dst_inode);
2195
2196 endoff = dst_off + len;
2197 if (inode_newsize_ok(dst_inode, endoff))
2198 return -EOPNOTSUPP;
2199
2200 if (ceph_quota_is_max_bytes_exceeded(dst_inode, endoff))
2201 return -EDQUOT;
2202
2203 return 0;
2204}
2205
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002206static ssize_t ceph_do_objects_copy(struct ceph_inode_info *src_ci, u64 *src_off,
2207 struct ceph_inode_info *dst_ci, u64 *dst_off,
2208 struct ceph_fs_client *fsc,
2209 size_t len, unsigned int flags)
2210{
2211 struct ceph_object_locator src_oloc, dst_oloc;
2212 struct ceph_object_id src_oid, dst_oid;
2213 size_t bytes = 0;
2214 u64 src_objnum, src_objoff, dst_objnum, dst_objoff;
2215 u32 src_objlen, dst_objlen;
2216 u32 object_size = src_ci->i_layout.object_size;
2217 int ret;
2218
2219 src_oloc.pool = src_ci->i_layout.pool_id;
2220 src_oloc.pool_ns = ceph_try_get_string(src_ci->i_layout.pool_ns);
2221 dst_oloc.pool = dst_ci->i_layout.pool_id;
2222 dst_oloc.pool_ns = ceph_try_get_string(dst_ci->i_layout.pool_ns);
2223
2224 while (len >= object_size) {
2225 ceph_calc_file_object_mapping(&src_ci->i_layout, *src_off,
2226 object_size, &src_objnum,
2227 &src_objoff, &src_objlen);
2228 ceph_calc_file_object_mapping(&dst_ci->i_layout, *dst_off,
2229 object_size, &dst_objnum,
2230 &dst_objoff, &dst_objlen);
2231 ceph_oid_init(&src_oid);
2232 ceph_oid_printf(&src_oid, "%llx.%08llx",
2233 src_ci->i_vino.ino, src_objnum);
2234 ceph_oid_init(&dst_oid);
2235 ceph_oid_printf(&dst_oid, "%llx.%08llx",
2236 dst_ci->i_vino.ino, dst_objnum);
2237 /* Do an object remote copy */
2238 ret = ceph_osdc_copy_from(&fsc->client->osdc,
2239 src_ci->i_vino.snap, 0,
2240 &src_oid, &src_oloc,
2241 CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
2242 CEPH_OSD_OP_FLAG_FADVISE_NOCACHE,
2243 &dst_oid, &dst_oloc,
2244 CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
2245 CEPH_OSD_OP_FLAG_FADVISE_DONTNEED,
2246 dst_ci->i_truncate_seq,
2247 dst_ci->i_truncate_size,
2248 CEPH_OSD_COPY_FROM_FLAG_TRUNCATE_SEQ);
2249 if (ret) {
2250 if (ret == -EOPNOTSUPP) {
2251 fsc->have_copy_from2 = false;
2252 pr_notice("OSDs don't support copy-from2; disabling copy offload\n");
2253 }
2254 dout("ceph_osdc_copy_from returned %d\n", ret);
2255 if (!bytes)
2256 bytes = ret;
2257 goto out;
2258 }
2259 len -= object_size;
2260 bytes += object_size;
2261 *src_off += object_size;
2262 *dst_off += object_size;
2263 }
2264
2265out:
2266 ceph_oloc_destroy(&src_oloc);
2267 ceph_oloc_destroy(&dst_oloc);
2268 return bytes;
2269}
2270
Dave Chinner64bf5ff2019-06-05 08:04:47 -07002271static ssize_t __ceph_copy_file_range(struct file *src_file, loff_t src_off,
2272 struct file *dst_file, loff_t dst_off,
2273 size_t len, unsigned int flags)
Luis Henriques503f82a2018-10-15 16:45:59 +01002274{
2275 struct inode *src_inode = file_inode(src_file);
2276 struct inode *dst_inode = file_inode(dst_file);
2277 struct ceph_inode_info *src_ci = ceph_inode(src_inode);
2278 struct ceph_inode_info *dst_ci = ceph_inode(dst_inode);
2279 struct ceph_cap_flush *prealloc_cf;
Luis Henriques6fd4e632019-09-09 16:48:54 +01002280 struct ceph_fs_client *src_fsc = ceph_inode_to_client(src_inode);
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002281 loff_t size;
2282 ssize_t ret = -EIO, bytes;
Luis Henriques503f82a2018-10-15 16:45:59 +01002283 u64 src_objnum, dst_objnum, src_objoff, dst_objoff;
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002284 u32 src_objlen, dst_objlen;
Luis Henriques503f82a2018-10-15 16:45:59 +01002285 int src_got = 0, dst_got = 0, err, dirty;
Luis Henriques503f82a2018-10-15 16:45:59 +01002286
Luis Henriques6fd4e632019-09-09 16:48:54 +01002287 if (src_inode->i_sb != dst_inode->i_sb) {
2288 struct ceph_fs_client *dst_fsc = ceph_inode_to_client(dst_inode);
2289
2290 if (ceph_fsid_compare(&src_fsc->client->fsid,
2291 &dst_fsc->client->fsid)) {
2292 dout("Copying files across clusters: src: %pU dst: %pU\n",
2293 &src_fsc->client->fsid, &dst_fsc->client->fsid);
2294 return -EXDEV;
2295 }
2296 }
Luis Henriques503f82a2018-10-15 16:45:59 +01002297 if (ceph_snap(dst_inode) != CEPH_NOSNAP)
2298 return -EROFS;
2299
2300 /*
2301 * Some of the checks below will return -EOPNOTSUPP, which will force a
2302 * fallback to the default VFS copy_file_range implementation. This is
2303 * desirable in several cases (for ex, the 'len' is smaller than the
2304 * size of the objects, or in cases where that would be more
2305 * efficient).
2306 */
2307
Luis Henriques6fd4e632019-09-09 16:48:54 +01002308 if (ceph_test_mount_opt(src_fsc, NOCOPYFROM))
Luis Henriquesea4cdc52018-10-15 16:46:00 +01002309 return -EOPNOTSUPP;
2310
Luis Henriques78beb0f2020-01-08 10:03:53 +00002311 if (!src_fsc->have_copy_from2)
2312 return -EOPNOTSUPP;
2313
Luis Henriquesa3a08192019-10-31 11:49:39 +00002314 /*
2315 * Striped file layouts require that we copy partial objects, but the
2316 * OSD copy-from operation only supports full-object copies. Limit
2317 * this to non-striped file layouts for now.
2318 */
Luis Henriques503f82a2018-10-15 16:45:59 +01002319 if ((src_ci->i_layout.stripe_unit != dst_ci->i_layout.stripe_unit) ||
Luis Henriquesa3a08192019-10-31 11:49:39 +00002320 (src_ci->i_layout.stripe_count != 1) ||
2321 (dst_ci->i_layout.stripe_count != 1) ||
2322 (src_ci->i_layout.object_size != dst_ci->i_layout.object_size)) {
2323 dout("Invalid src/dst files layout\n");
Luis Henriques503f82a2018-10-15 16:45:59 +01002324 return -EOPNOTSUPP;
Luis Henriquesa3a08192019-10-31 11:49:39 +00002325 }
Luis Henriques503f82a2018-10-15 16:45:59 +01002326
2327 if (len < src_ci->i_layout.object_size)
2328 return -EOPNOTSUPP; /* no remote copy will be done */
2329
2330 prealloc_cf = ceph_alloc_cap_flush();
2331 if (!prealloc_cf)
2332 return -ENOMEM;
2333
Luis Henriquesc2c6d3c2018-10-23 16:53:14 +00002334 /* Start by sync'ing the source and destination files */
Luis Henriques503f82a2018-10-15 16:45:59 +01002335 ret = file_write_and_wait_range(src_file, src_off, (src_off + len));
Luis Henriquesc2c6d3c2018-10-23 16:53:14 +00002336 if (ret < 0) {
2337 dout("failed to write src file (%zd)\n", ret);
Luis Henriques503f82a2018-10-15 16:45:59 +01002338 goto out;
Luis Henriquesc2c6d3c2018-10-23 16:53:14 +00002339 }
2340 ret = file_write_and_wait_range(dst_file, dst_off, (dst_off + len));
2341 if (ret < 0) {
2342 dout("failed to write dst file (%zd)\n", ret);
2343 goto out;
2344 }
Luis Henriques503f82a2018-10-15 16:45:59 +01002345
2346 /*
2347 * We need FILE_WR caps for dst_ci and FILE_RD for src_ci as other
2348 * clients may have dirty data in their caches. And OSDs know nothing
2349 * about caps, so they can't safely do the remote object copies.
2350 */
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002351 err = get_rd_wr_caps(src_file, &src_got,
2352 dst_file, (dst_off + len), &dst_got);
Luis Henriques503f82a2018-10-15 16:45:59 +01002353 if (err < 0) {
2354 dout("get_rd_wr_caps returned %d\n", err);
2355 ret = -EOPNOTSUPP;
2356 goto out;
2357 }
2358
2359 ret = is_file_size_ok(src_inode, dst_inode, src_off, dst_off, len);
2360 if (ret < 0)
2361 goto out_caps;
2362
Luis Henriques503f82a2018-10-15 16:45:59 +01002363 /* Drop dst file cached pages */
2364 ret = invalidate_inode_pages2_range(dst_inode->i_mapping,
2365 dst_off >> PAGE_SHIFT,
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002366 (dst_off + len) >> PAGE_SHIFT);
Luis Henriques503f82a2018-10-15 16:45:59 +01002367 if (ret < 0) {
2368 dout("Failed to invalidate inode pages (%zd)\n", ret);
2369 ret = 0; /* XXX */
2370 }
Luis Henriques503f82a2018-10-15 16:45:59 +01002371 ceph_calc_file_object_mapping(&src_ci->i_layout, src_off,
2372 src_ci->i_layout.object_size,
2373 &src_objnum, &src_objoff, &src_objlen);
2374 ceph_calc_file_object_mapping(&dst_ci->i_layout, dst_off,
2375 dst_ci->i_layout.object_size,
2376 &dst_objnum, &dst_objoff, &dst_objlen);
2377 /* object-level offsets need to the same */
2378 if (src_objoff != dst_objoff) {
2379 ret = -EOPNOTSUPP;
2380 goto out_caps;
2381 }
2382
2383 /*
2384 * Do a manual copy if the object offset isn't object aligned.
2385 * 'src_objlen' contains the bytes left until the end of the object,
2386 * starting at the src_off
2387 */
2388 if (src_objoff) {
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002389 dout("Initial partial copy of %u bytes\n", src_objlen);
2390
Luis Henriques503f82a2018-10-15 16:45:59 +01002391 /*
2392 * we need to temporarily drop all caps as we'll be calling
2393 * {read,write}_iter, which will get caps again.
2394 */
2395 put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
2396 ret = do_splice_direct(src_file, &src_off, dst_file,
2397 &dst_off, src_objlen, flags);
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002398 /* Abort on short copies or on error */
2399 if (ret < src_objlen) {
2400 dout("Failed partial copy (%zd)\n", ret);
Luis Henriques503f82a2018-10-15 16:45:59 +01002401 goto out;
2402 }
2403 len -= ret;
Yan, Zheng5e3ded12019-07-25 20:16:43 +08002404 err = get_rd_wr_caps(src_file, &src_got,
2405 dst_file, (dst_off + len), &dst_got);
Luis Henriques503f82a2018-10-15 16:45:59 +01002406 if (err < 0)
2407 goto out;
2408 err = is_file_size_ok(src_inode, dst_inode,
2409 src_off, dst_off, len);
2410 if (err < 0)
2411 goto out_caps;
2412 }
Luis Henriques503f82a2018-10-15 16:45:59 +01002413
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002414 size = i_size_read(dst_inode);
2415 bytes = ceph_do_objects_copy(src_ci, &src_off, dst_ci, &dst_off,
2416 src_fsc, len, flags);
2417 if (bytes <= 0) {
2418 if (!ret)
2419 ret = bytes;
2420 goto out_caps;
2421 }
2422 dout("Copied %zu bytes out of %zu\n", bytes, len);
2423 len -= bytes;
2424 ret += bytes;
Luis Henriques503f82a2018-10-15 16:45:59 +01002425
2426 file_update_time(dst_file);
Jeff Layton5c308352019-06-06 08:57:27 -04002427 inode_inc_iversion_raw(dst_inode);
2428
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002429 if (dst_off > size) {
Luis Henriques503f82a2018-10-15 16:45:59 +01002430 /* Let the MDS know about dst file size change */
Yan, Zhenga0d93e32020-03-05 20:21:01 +08002431 if (ceph_inode_set_size(dst_inode, dst_off) ||
2432 ceph_quota_is_max_bytes_approaching(dst_inode, dst_off))
2433 ceph_check_caps(dst_ci, CHECK_CAPS_AUTHONLY, NULL);
Luis Henriques503f82a2018-10-15 16:45:59 +01002434 }
2435 /* Mark Fw dirty */
2436 spin_lock(&dst_ci->i_ceph_lock);
2437 dst_ci->i_inline_version = CEPH_INLINE_NONE;
2438 dirty = __ceph_mark_dirty_caps(dst_ci, CEPH_CAP_FILE_WR, &prealloc_cf);
2439 spin_unlock(&dst_ci->i_ceph_lock);
2440 if (dirty)
2441 __mark_inode_dirty(dst_inode, dirty);
2442
2443out_caps:
2444 put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
2445
Luis Henriques1b0c3b92020-02-24 13:44:32 +00002446 /*
2447 * Do the final manual copy if we still have some bytes left, unless
2448 * there were errors in remote object copies (len >= object_size).
2449 */
2450 if (len && (len < src_ci->i_layout.object_size)) {
2451 dout("Final partial copy of %zu bytes\n", len);
2452 bytes = do_splice_direct(src_file, &src_off, dst_file,
2453 &dst_off, len, flags);
2454 if (bytes > 0)
2455 ret += bytes;
2456 else
2457 dout("Failed partial copy (%zd)\n", bytes);
Luis Henriques503f82a2018-10-15 16:45:59 +01002458 }
2459
2460out:
2461 ceph_free_cap_flush(prealloc_cf);
2462
2463 return ret;
2464}
2465
Dave Chinner64bf5ff2019-06-05 08:04:47 -07002466static ssize_t ceph_copy_file_range(struct file *src_file, loff_t src_off,
2467 struct file *dst_file, loff_t dst_off,
2468 size_t len, unsigned int flags)
2469{
2470 ssize_t ret;
2471
2472 ret = __ceph_copy_file_range(src_file, src_off, dst_file, dst_off,
2473 len, flags);
2474
Amir Goldstein5dae2222019-06-05 08:04:50 -07002475 if (ret == -EOPNOTSUPP || ret == -EXDEV)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07002476 ret = generic_copy_file_range(src_file, src_off, dst_file,
2477 dst_off, len, flags);
2478 return ret;
2479}
2480
Sage Weil124e68e2009-10-06 11:31:08 -07002481const struct file_operations ceph_file_fops = {
2482 .open = ceph_open,
2483 .release = ceph_release,
2484 .llseek = ceph_llseek,
Al Viro36444242014-04-02 20:28:01 -04002485 .read_iter = ceph_read_iter,
Al Viro4908b822014-04-03 23:09:01 -04002486 .write_iter = ceph_write_iter,
Sage Weil124e68e2009-10-06 11:31:08 -07002487 .mmap = ceph_mmap,
2488 .fsync = ceph_fsync,
Greg Farnum40819f62010-08-02 15:34:23 -07002489 .lock = ceph_lock,
2490 .flock = ceph_flock,
Yan, Zheng7ce469a2016-11-08 21:54:34 +08002491 .splice_read = generic_file_splice_read,
Al Viro3551dd72014-04-05 04:40:12 -04002492 .splice_write = iter_file_splice_write,
Sage Weil124e68e2009-10-06 11:31:08 -07002493 .unlocked_ioctl = ceph_ioctl,
Arnd Bergmann18bd6ca2018-09-11 20:47:23 +02002494 .compat_ioctl = compat_ptr_ioctl,
Li Wangad7a60d2013-08-15 11:51:44 +08002495 .fallocate = ceph_fallocate,
Luis Henriques503f82a2018-10-15 16:45:59 +01002496 .copy_file_range = ceph_copy_file_range,
Sage Weil124e68e2009-10-06 11:31:08 -07002497};