1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 #include <linux/ceph/striper.h>
4
5 #include <linux/module.h>
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/file.h>
9 #include <linux/mount.h>
10 #include <linux/namei.h>
11 #include <linux/writeback.h>
12 #include <linux/falloc.h>
13 #include <linux/iversion.h>
14 #include <linux/ktime.h>
15 #include <linux/splice.h>
16
17 #include "super.h"
18 #include "mds_client.h"
19 #include "cache.h"
20 #include "io.h"
21 #include "metric.h"
22 #include "subvolume_metrics.h"
23
24 /*
25 * Record I/O for subvolume metrics tracking.
26 *
27 * Callers must ensure bytes > 0 for reads (ret > 0 check) to avoid counting
28 * EOF as an I/O operation. For writes, the condition is (ret >= 0 && len > 0).
29 */
ceph_record_subvolume_io(struct inode * inode,bool is_write,ktime_t start,ktime_t end,size_t bytes)30 static inline void ceph_record_subvolume_io(struct inode *inode, bool is_write,
31 ktime_t start, ktime_t end,
32 size_t bytes)
33 {
34 if (!bytes)
35 return;
36
37 ceph_subvolume_metrics_record_io(ceph_sb_to_mdsc(inode->i_sb),
38 ceph_inode(inode),
39 is_write, bytes, start, end);
40 }
41
ceph_flags_sys2wire(struct ceph_mds_client * mdsc,u32 flags)42 static __le32 ceph_flags_sys2wire(struct ceph_mds_client *mdsc, u32 flags)
43 {
44 struct ceph_client *cl = mdsc->fsc->client;
45 u32 wire_flags = 0;
46
47 switch (flags & O_ACCMODE) {
48 case O_RDONLY:
49 wire_flags |= CEPH_O_RDONLY;
50 break;
51 case O_WRONLY:
52 wire_flags |= CEPH_O_WRONLY;
53 break;
54 case O_RDWR:
55 wire_flags |= CEPH_O_RDWR;
56 break;
57 }
58
59 flags &= ~O_ACCMODE;
60
61 #define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; }
62
63 ceph_sys2wire(O_CREAT);
64 ceph_sys2wire(O_EXCL);
65 ceph_sys2wire(O_TRUNC);
66 ceph_sys2wire(O_DIRECTORY);
67 ceph_sys2wire(O_NOFOLLOW);
68
69 #undef ceph_sys2wire
70
71 if (flags)
72 doutc(cl, "unused open flags: %x\n", flags);
73
74 return cpu_to_le32(wire_flags);
75 }
76
77 /*
78 * Ceph file operations
79 *
80 * Implement basic open/close functionality, and implement
81 * read/write.
82 *
83 * We implement three modes of file I/O:
84 * - buffered uses the generic_file_aio_{read,write} helpers
85 *
86 * - synchronous is used when there is multi-client read/write
87 * sharing, avoids the page cache, and synchronously waits for an
88 * ack from the OSD.
89 *
90 * - direct io takes the variant of the sync path that references
91 * user pages directly.
92 *
93 * fsync() flushes and waits on dirty pages, but just queues metadata
94 * for writeback: since the MDS can recover size and mtime there is no
95 * need to wait for MDS acknowledgement.
96 */
97
98 /*
99 * How many pages to get in one call to iov_iter_get_pages(). This
100 * determines the size of the on-stack array used as a buffer.
101 */
102 #define ITER_GET_BVECS_PAGES 64
103
__iter_get_bvecs(struct iov_iter * iter,size_t maxsize,struct bio_vec * bvecs)104 static ssize_t __iter_get_bvecs(struct iov_iter *iter, size_t maxsize,
105 struct bio_vec *bvecs)
106 {
107 size_t size = 0;
108 int bvec_idx = 0;
109
110 if (maxsize > iov_iter_count(iter))
111 maxsize = iov_iter_count(iter);
112
113 while (size < maxsize) {
114 struct page *pages[ITER_GET_BVECS_PAGES];
115 ssize_t bytes;
116 size_t start;
117 int idx = 0;
118
119 bytes = iov_iter_get_pages2(iter, pages, maxsize - size,
120 ITER_GET_BVECS_PAGES, &start);
121 if (bytes < 0)
122 return size ?: bytes;
123
124 size += bytes;
125
126 for ( ; bytes; idx++, bvec_idx++) {
127 int len = min_t(int, bytes, PAGE_SIZE - start);
128
129 bvec_set_page(&bvecs[bvec_idx], pages[idx], len, start);
130 bytes -= len;
131 start = 0;
132 }
133 }
134
135 return size;
136 }
137
138 /*
139 * iov_iter_get_pages() only considers one iov_iter segment, no matter
140 * what maxsize or maxpages are given. For ITER_BVEC that is a single
141 * page.
142 *
143 * Attempt to get up to @maxsize bytes worth of pages from @iter.
144 * Return the number of bytes in the created bio_vec array, or an error.
145 */
iter_get_bvecs_alloc(struct iov_iter * iter,size_t maxsize,struct bio_vec ** bvecs,int * num_bvecs)146 static ssize_t iter_get_bvecs_alloc(struct iov_iter *iter, size_t maxsize,
147 struct bio_vec **bvecs, int *num_bvecs)
148 {
149 struct bio_vec *bv;
150 size_t orig_count = iov_iter_count(iter);
151 ssize_t bytes;
152 int npages;
153
154 iov_iter_truncate(iter, maxsize);
155 npages = iov_iter_npages(iter, INT_MAX);
156 iov_iter_reexpand(iter, orig_count);
157
158 /*
159 * __iter_get_bvecs() may populate only part of the array -- zero it
160 * out.
161 */
162 bv = kvmalloc_objs(*bv, npages, GFP_KERNEL | __GFP_ZERO);
163 if (!bv)
164 return -ENOMEM;
165
166 bytes = __iter_get_bvecs(iter, maxsize, bv);
167 if (bytes < 0) {
168 /*
169 * No pages were pinned -- just free the array.
170 */
171 kvfree(bv);
172 return bytes;
173 }
174
175 *bvecs = bv;
176 *num_bvecs = npages;
177 return bytes;
178 }
179
put_bvecs(struct bio_vec * bvecs,int num_bvecs,bool should_dirty)180 static void put_bvecs(struct bio_vec *bvecs, int num_bvecs, bool should_dirty)
181 {
182 int i;
183
184 for (i = 0; i < num_bvecs; i++) {
185 if (bvecs[i].bv_page) {
186 if (should_dirty)
187 set_page_dirty_lock(bvecs[i].bv_page);
188 put_page(bvecs[i].bv_page);
189 }
190 }
191 kvfree(bvecs);
192 }
193
194 /*
195 * Prepare an open request. Preallocate ceph_cap to avoid an
196 * inopportune ENOMEM later.
197 */
198 static struct ceph_mds_request *
prepare_open_request(struct super_block * sb,int flags,int create_mode)199 prepare_open_request(struct super_block *sb, int flags, int create_mode)
200 {
201 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
202 struct ceph_mds_request *req;
203 int want_auth = USE_ANY_MDS;
204 int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
205
206 if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
207 want_auth = USE_AUTH_MDS;
208
209 req = ceph_mdsc_create_request(mdsc, op, want_auth);
210 if (IS_ERR(req))
211 goto out;
212 req->r_fmode = ceph_flags_to_mode(flags);
213 req->r_args.open.flags = ceph_flags_sys2wire(mdsc, flags);
214 req->r_args.open.mode = cpu_to_le32(create_mode);
215 out:
216 return req;
217 }
218
ceph_init_file_info(struct inode * inode,struct file * file,int fmode,bool isdir)219 static int ceph_init_file_info(struct inode *inode, struct file *file,
220 int fmode, bool isdir)
221 {
222 struct ceph_inode_info *ci = ceph_inode(inode);
223 struct ceph_mount_options *opt =
224 ceph_inode_to_fs_client(&ci->netfs.inode)->mount_options;
225 struct ceph_client *cl = ceph_inode_to_client(inode);
226 struct ceph_file_info *fi;
227 int ret;
228
229 doutc(cl, "%p %llx.%llx %p 0%o (%s)\n", inode, ceph_vinop(inode),
230 file, inode->i_mode, isdir ? "dir" : "regular");
231 BUG_ON(inode->i_fop->release != ceph_release);
232
233 if (isdir) {
234 struct ceph_dir_file_info *dfi =
235 kmem_cache_zalloc(ceph_dir_file_cachep, GFP_KERNEL);
236 if (!dfi)
237 return -ENOMEM;
238
239 file->private_data = dfi;
240 fi = &dfi->file_info;
241 dfi->next_offset = 2;
242 dfi->readdir_cache_idx = -1;
243 } else {
244 fi = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
245 if (!fi)
246 return -ENOMEM;
247
248 if (opt->flags & CEPH_MOUNT_OPT_NOPAGECACHE)
249 fi->flags |= CEPH_F_SYNC;
250
251 file->private_data = fi;
252 }
253
254 ceph_get_fmode(ci, fmode, 1);
255 fi->fmode = fmode;
256
257 spin_lock_init(&fi->rw_contexts_lock);
258 INIT_LIST_HEAD(&fi->rw_contexts);
259 fi->filp_gen = READ_ONCE(ceph_inode_to_fs_client(inode)->filp_gen);
260
261 if ((file->f_mode & FMODE_WRITE) && ceph_has_inline_data(ci)) {
262 ret = ceph_uninline_data(file);
263 if (ret < 0)
264 goto error;
265 }
266
267 return 0;
268
269 error:
270 ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE);
271 ceph_put_fmode(ci, fi->fmode, 1);
272 kmem_cache_free(ceph_file_cachep, fi);
273 /* wake up anyone waiting for caps on this inode */
274 wake_up_all(&ci->i_cap_wq);
275 return ret;
276 }
277
278 /*
279 * initialize private struct file data.
280 * if we fail, clean up by dropping fmode reference on the ceph_inode
281 */
ceph_init_file(struct inode * inode,struct file * file,int fmode)282 static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
283 {
284 struct ceph_client *cl = ceph_inode_to_client(inode);
285 int ret = 0;
286
287 switch (inode->i_mode & S_IFMT) {
288 case S_IFREG:
289 ceph_fscache_use_cookie(inode, file->f_mode & FMODE_WRITE);
290 fallthrough;
291 case S_IFDIR:
292 ret = ceph_init_file_info(inode, file, fmode,
293 S_ISDIR(inode->i_mode));
294 break;
295
296 case S_IFLNK:
297 doutc(cl, "%p %llx.%llx %p 0%o (symlink)\n", inode,
298 ceph_vinop(inode), file, inode->i_mode);
299 break;
300
301 default:
302 doutc(cl, "%p %llx.%llx %p 0%o (special)\n", inode,
303 ceph_vinop(inode), file, inode->i_mode);
304 /*
305 * we need to drop the open ref now, since we don't
306 * have .release set to ceph_release.
307 */
308 BUG_ON(inode->i_fop->release == ceph_release);
309
310 /* call the proper open fop */
311 ret = inode->i_fop->open(inode, file);
312 }
313 return ret;
314 }
315
316 /*
317 * try renew caps after session gets killed.
318 */
ceph_renew_caps(struct inode * inode,int fmode)319 int ceph_renew_caps(struct inode *inode, int fmode)
320 {
321 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
322 struct ceph_client *cl = mdsc->fsc->client;
323 struct ceph_inode_info *ci = ceph_inode(inode);
324 struct ceph_mds_request *req;
325 int err, flags, wanted;
326
327 spin_lock(&ci->i_ceph_lock);
328 __ceph_touch_fmode(ci, mdsc, fmode);
329 wanted = __ceph_caps_file_wanted(ci);
330 if (__ceph_is_any_real_caps(ci) &&
331 (!(wanted & CEPH_CAP_ANY_WR) || ci->i_auth_cap)) {
332 int issued = __ceph_caps_issued(ci, NULL);
333 spin_unlock(&ci->i_ceph_lock);
334 doutc(cl, "%p %llx.%llx want %s issued %s updating mds_wanted\n",
335 inode, ceph_vinop(inode), ceph_cap_string(wanted),
336 ceph_cap_string(issued));
337 ceph_check_caps(ci, 0);
338 return 0;
339 }
340 spin_unlock(&ci->i_ceph_lock);
341
342 flags = 0;
343 if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
344 flags = O_RDWR;
345 else if (wanted & CEPH_CAP_FILE_RD)
346 flags = O_RDONLY;
347 else if (wanted & CEPH_CAP_FILE_WR)
348 flags = O_WRONLY;
349 #ifdef O_LAZY
350 if (wanted & CEPH_CAP_FILE_LAZYIO)
351 flags |= O_LAZY;
352 #endif
353
354 req = prepare_open_request(inode->i_sb, flags, 0);
355 if (IS_ERR(req)) {
356 err = PTR_ERR(req);
357 goto out;
358 }
359
360 req->r_inode = inode;
361 ihold(inode);
362 req->r_num_caps = 1;
363
364 err = ceph_mdsc_do_request(mdsc, NULL, req);
365 ceph_mdsc_put_request(req);
366 out:
367 doutc(cl, "%p %llx.%llx open result=%d\n", inode, ceph_vinop(inode),
368 err);
369 return err < 0 ? err : 0;
370 }
371
372 /*
373 * If we already have the requisite capabilities, we can satisfy
374 * the open request locally (no need to request new caps from the
375 * MDS). We do, however, need to inform the MDS (asynchronously)
376 * if our wanted caps set expands.
377 */
ceph_open(struct inode * inode,struct file * file)378 int ceph_open(struct inode *inode, struct file *file)
379 {
380 struct ceph_inode_info *ci = ceph_inode(inode);
381 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
382 struct ceph_client *cl = fsc->client;
383 struct ceph_mds_client *mdsc = fsc->mdsc;
384 struct ceph_mds_request *req;
385 struct ceph_file_info *fi = file->private_data;
386 int err;
387 int flags, fmode, wanted;
388 struct dentry *dentry;
389 char *path;
390 bool do_sync = false;
391 int mask = MAY_READ;
392
393 if (fi) {
394 doutc(cl, "file %p is already opened\n", file);
395 return 0;
396 }
397
398 /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
399 flags = file->f_flags & ~(O_CREAT|O_EXCL);
400 if (S_ISDIR(inode->i_mode)) {
401 flags = O_DIRECTORY; /* mds likes to know */
402 } else if (S_ISREG(inode->i_mode)) {
403 err = fscrypt_file_open(inode, file);
404 if (err)
405 return err;
406 }
407
408 doutc(cl, "%p %llx.%llx file %p flags %d (%d)\n", inode,
409 ceph_vinop(inode), file, flags, file->f_flags);
410 fmode = ceph_flags_to_mode(flags);
411 wanted = ceph_caps_for_mode(fmode);
412
413 if (fmode & CEPH_FILE_MODE_WR)
414 mask |= MAY_WRITE;
415 dentry = d_find_alias(inode);
416 if (!dentry) {
417 do_sync = true;
418 } else {
419 struct ceph_path_info path_info = {0};
420 path = ceph_mdsc_build_path(mdsc, dentry, &path_info, 0);
421 if (IS_ERR(path)) {
422 do_sync = true;
423 err = 0;
424 } else {
425 err = ceph_mds_check_access(mdsc, path, mask);
426 }
427 ceph_mdsc_free_path_info(&path_info);
428 dput(dentry);
429
430 /* For none EACCES cases will let the MDS do the mds auth check */
431 if (err == -EACCES) {
432 return err;
433 } else if (err < 0) {
434 do_sync = true;
435 err = 0;
436 }
437 }
438
439 /* snapped files are read-only */
440 if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
441 return -EROFS;
442
443 /* trivially open snapdir */
444 if (ceph_snap(inode) == CEPH_SNAPDIR) {
445 return ceph_init_file(inode, file, fmode);
446 }
447
448 /*
449 * No need to block if we have caps on the auth MDS (for
450 * write) or any MDS (for read). Update wanted set
451 * asynchronously.
452 */
453 spin_lock(&ci->i_ceph_lock);
454 if (!do_sync && __ceph_is_any_real_caps(ci) &&
455 (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
456 int mds_wanted = __ceph_caps_mds_wanted(ci, true);
457 int issued = __ceph_caps_issued(ci, NULL);
458
459 doutc(cl, "open %p fmode %d want %s issued %s using existing\n",
460 inode, fmode, ceph_cap_string(wanted),
461 ceph_cap_string(issued));
462 __ceph_touch_fmode(ci, mdsc, fmode);
463 spin_unlock(&ci->i_ceph_lock);
464
465 /* adjust wanted? */
466 if ((issued & wanted) != wanted &&
467 (mds_wanted & wanted) != wanted &&
468 ceph_snap(inode) != CEPH_SNAPDIR)
469 ceph_check_caps(ci, 0);
470
471 return ceph_init_file(inode, file, fmode);
472 } else if (!do_sync && ceph_snap(inode) != CEPH_NOSNAP &&
473 (ci->i_snap_caps & wanted) == wanted) {
474 __ceph_touch_fmode(ci, mdsc, fmode);
475 spin_unlock(&ci->i_ceph_lock);
476 return ceph_init_file(inode, file, fmode);
477 }
478
479 spin_unlock(&ci->i_ceph_lock);
480
481 doutc(cl, "open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
482 req = prepare_open_request(inode->i_sb, flags, 0);
483 if (IS_ERR(req)) {
484 err = PTR_ERR(req);
485 goto out;
486 }
487 req->r_inode = inode;
488 ihold(inode);
489
490 req->r_num_caps = 1;
491 err = ceph_mdsc_do_request(mdsc, NULL, req);
492 if (!err)
493 err = ceph_init_file(inode, file, req->r_fmode);
494 ceph_mdsc_put_request(req);
495 doutc(cl, "open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
496 out:
497 return err;
498 }
499
500 /* Clone the layout from a synchronous create, if the dir now has Dc caps */
501 static void
cache_file_layout(struct inode * dst,struct inode * src)502 cache_file_layout(struct inode *dst, struct inode *src)
503 {
504 struct ceph_inode_info *cdst = ceph_inode(dst);
505 struct ceph_inode_info *csrc = ceph_inode(src);
506
507 spin_lock(&cdst->i_ceph_lock);
508 if ((__ceph_caps_issued(cdst, NULL) & CEPH_CAP_DIR_CREATE) &&
509 !ceph_file_layout_is_valid(&cdst->i_cached_layout)) {
510 memcpy(&cdst->i_cached_layout, &csrc->i_layout,
511 sizeof(cdst->i_cached_layout));
512 rcu_assign_pointer(cdst->i_cached_layout.pool_ns,
513 ceph_try_get_string(csrc->i_layout.pool_ns));
514 }
515 spin_unlock(&cdst->i_ceph_lock);
516 }
517
518 /*
519 * Try to set up an async create. We need caps, a file layout, and inode number,
520 * and either a lease on the dentry or complete dir info. If any of those
521 * criteria are not satisfied, then return false and the caller can go
522 * synchronous.
523 */
try_prep_async_create(struct inode * dir,struct dentry * dentry,struct ceph_file_layout * lo,u64 * pino)524 static int try_prep_async_create(struct inode *dir, struct dentry *dentry,
525 struct ceph_file_layout *lo, u64 *pino)
526 {
527 struct ceph_inode_info *ci = ceph_inode(dir);
528 struct ceph_dentry_info *di = ceph_dentry(dentry);
529 int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_CREATE;
530 u64 ino;
531
532 spin_lock(&ci->i_ceph_lock);
533 /* No auth cap means no chance for Dc caps */
534 if (!ci->i_auth_cap)
535 goto no_async;
536
537 /* Any delegated inos? */
538 if (xa_empty(&ci->i_auth_cap->session->s_delegated_inos))
539 goto no_async;
540
541 if (!ceph_file_layout_is_valid(&ci->i_cached_layout))
542 goto no_async;
543
544 if ((__ceph_caps_issued(ci, NULL) & want) != want)
545 goto no_async;
546
547 if (d_in_lookup(dentry)) {
548 if (!__ceph_dir_is_complete(ci))
549 goto no_async;
550 spin_lock(&dentry->d_lock);
551 di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
552 spin_unlock(&dentry->d_lock);
553 } else if (atomic_read(&ci->i_shared_gen) !=
554 READ_ONCE(di->lease_shared_gen)) {
555 goto no_async;
556 }
557
558 ino = ceph_get_deleg_ino(ci->i_auth_cap->session);
559 if (!ino)
560 goto no_async;
561
562 *pino = ino;
563 ceph_take_cap_refs(ci, want, false);
564 memcpy(lo, &ci->i_cached_layout, sizeof(*lo));
565 rcu_assign_pointer(lo->pool_ns,
566 ceph_try_get_string(ci->i_cached_layout.pool_ns));
567 got = want;
568 no_async:
569 spin_unlock(&ci->i_ceph_lock);
570 return got;
571 }
572
restore_deleg_ino(struct inode * dir,u64 ino)573 static void restore_deleg_ino(struct inode *dir, u64 ino)
574 {
575 struct ceph_client *cl = ceph_inode_to_client(dir);
576 struct ceph_inode_info *ci = ceph_inode(dir);
577 struct ceph_mds_session *s = NULL;
578
579 spin_lock(&ci->i_ceph_lock);
580 if (ci->i_auth_cap)
581 s = ceph_get_mds_session(ci->i_auth_cap->session);
582 spin_unlock(&ci->i_ceph_lock);
583 if (s) {
584 int err = ceph_restore_deleg_ino(s, ino);
585 if (err)
586 pr_warn_client(cl,
587 "unable to restore delegated ino 0x%llx to session: %d\n",
588 ino, err);
589 ceph_put_mds_session(s);
590 }
591 }
592
wake_async_create_waiters(struct inode * inode,struct ceph_mds_session * session)593 static void wake_async_create_waiters(struct inode *inode,
594 struct ceph_mds_session *session)
595 {
596 struct ceph_inode_info *ci = ceph_inode(inode);
597 bool check_cap = false;
598
599 spin_lock(&ci->i_ceph_lock);
600 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
601 clear_and_wake_up_bit(CEPH_ASYNC_CREATE_BIT, &ci->i_ceph_flags);
602
603 if (ci->i_ceph_flags & CEPH_I_ASYNC_CHECK_CAPS) {
604 ci->i_ceph_flags &= ~CEPH_I_ASYNC_CHECK_CAPS;
605 check_cap = true;
606 }
607 }
608 ceph_kick_flushing_inode_caps(session, ci);
609 spin_unlock(&ci->i_ceph_lock);
610
611 if (check_cap)
612 ceph_check_caps(ci, CHECK_CAPS_FLUSH);
613 }
614
ceph_async_create_cb(struct ceph_mds_client * mdsc,struct ceph_mds_request * req)615 static void ceph_async_create_cb(struct ceph_mds_client *mdsc,
616 struct ceph_mds_request *req)
617 {
618 struct ceph_client *cl = mdsc->fsc->client;
619 struct dentry *dentry = req->r_dentry;
620 struct inode *dinode = d_inode(dentry);
621 struct inode *tinode = req->r_target_inode;
622 int result = req->r_err ? req->r_err :
623 le32_to_cpu(req->r_reply_info.head->result);
624
625 WARN_ON_ONCE(dinode && tinode && dinode != tinode);
626
627 /* MDS changed -- caller must resubmit */
628 if (result == -EJUKEBOX)
629 goto out;
630
631 mapping_set_error(req->r_parent->i_mapping, result);
632
633 if (result) {
634 struct ceph_path_info path_info = {0};
635 char *path = ceph_mdsc_build_path(mdsc, req->r_dentry, &path_info, 0);
636
637 pr_warn_client(cl,
638 "async create failure path=(%llx)%s result=%d!\n",
639 path_info.vino.ino, IS_ERR(path) ? "<<bad>>" : path, result);
640 ceph_mdsc_free_path_info(&path_info);
641
642 ceph_dir_clear_complete(req->r_parent);
643 if (!d_unhashed(dentry))
644 d_drop(dentry);
645
646 if (dinode) {
647 mapping_set_error(dinode->i_mapping, result);
648 ceph_inode_shutdown(dinode);
649 wake_async_create_waiters(dinode, req->r_session);
650 }
651 }
652
653 if (tinode) {
654 u64 ino = ceph_vino(tinode).ino;
655
656 if (req->r_deleg_ino != ino)
657 pr_warn_client(cl,
658 "inode number mismatch! err=%d deleg_ino=0x%llx target=0x%llx\n",
659 req->r_err, req->r_deleg_ino, ino);
660
661 mapping_set_error(tinode->i_mapping, result);
662 wake_async_create_waiters(tinode, req->r_session);
663 } else if (!result) {
664 pr_warn_client(cl, "no req->r_target_inode for 0x%llx\n",
665 req->r_deleg_ino);
666 }
667 out:
668 ceph_mdsc_release_dir_caps(req);
669 }
670
ceph_finish_async_create(struct inode * dir,struct inode * inode,struct dentry * dentry,struct file * file,umode_t mode,struct ceph_mds_request * req,struct ceph_acl_sec_ctx * as_ctx,struct ceph_file_layout * lo)671 static int ceph_finish_async_create(struct inode *dir, struct inode *inode,
672 struct dentry *dentry,
673 struct file *file, umode_t mode,
674 struct ceph_mds_request *req,
675 struct ceph_acl_sec_ctx *as_ctx,
676 struct ceph_file_layout *lo)
677 {
678 int ret;
679 char xattr_buf[4];
680 struct ceph_mds_reply_inode in = { };
681 struct ceph_mds_reply_info_in iinfo = { .in = &in };
682 struct ceph_inode_info *ci = ceph_inode(dir);
683 struct ceph_dentry_info *di = ceph_dentry(dentry);
684 struct timespec64 now;
685 struct ceph_string *pool_ns;
686 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
687 struct ceph_client *cl = mdsc->fsc->client;
688 struct ceph_vino vino = { .ino = req->r_deleg_ino,
689 .snap = CEPH_NOSNAP };
690
691 ktime_get_real_ts64(&now);
692
693 iinfo.inline_version = CEPH_INLINE_NONE;
694 iinfo.change_attr = 1;
695 ceph_encode_timespec64(&iinfo.btime, &now);
696
697 if (req->r_pagelist) {
698 iinfo.xattr_len = req->r_pagelist->length;
699 iinfo.xattr_data = req->r_pagelist->mapped_tail;
700 } else {
701 /* fake it */
702 iinfo.xattr_len = ARRAY_SIZE(xattr_buf);
703 iinfo.xattr_data = xattr_buf;
704 memset(iinfo.xattr_data, 0, iinfo.xattr_len);
705 }
706
707 in.ino = cpu_to_le64(vino.ino);
708 in.snapid = cpu_to_le64(CEPH_NOSNAP);
709 in.version = cpu_to_le64(1); // ???
710 in.cap.caps = in.cap.wanted = cpu_to_le32(CEPH_CAP_ALL_FILE);
711 in.cap.cap_id = cpu_to_le64(1);
712 in.cap.realm = cpu_to_le64(ci->i_snap_realm->ino);
713 in.cap.flags = CEPH_CAP_FLAG_AUTH;
714 in.ctime = in.mtime = in.atime = iinfo.btime;
715 in.truncate_seq = cpu_to_le32(1);
716 in.truncate_size = cpu_to_le64(-1ULL);
717 in.xattr_version = cpu_to_le64(1);
718 in.uid = cpu_to_le32(from_kuid(&init_user_ns,
719 mapped_fsuid(req->r_mnt_idmap,
720 &init_user_ns)));
721 if (dir->i_mode & S_ISGID) {
722 in.gid = cpu_to_le32(from_kgid(&init_user_ns, dir->i_gid));
723
724 /* Directories always inherit the setgid bit. */
725 if (S_ISDIR(mode))
726 mode |= S_ISGID;
727 } else {
728 in.gid = cpu_to_le32(from_kgid(&init_user_ns,
729 mapped_fsgid(req->r_mnt_idmap,
730 &init_user_ns)));
731 }
732 in.mode = cpu_to_le32((u32)mode);
733
734 in.nlink = cpu_to_le32(1);
735 in.max_size = cpu_to_le64(lo->stripe_unit);
736
737 ceph_file_layout_to_legacy(lo, &in.layout);
738 /* lo is private, so pool_ns can't change */
739 pool_ns = rcu_dereference_raw(lo->pool_ns);
740 if (pool_ns) {
741 iinfo.pool_ns_len = pool_ns->len;
742 iinfo.pool_ns_data = pool_ns->str;
743 }
744
745 down_read(&mdsc->snap_rwsem);
746 ret = ceph_fill_inode(inode, NULL, &iinfo, NULL, req->r_session,
747 req->r_fmode, NULL);
748 up_read(&mdsc->snap_rwsem);
749 if (ret) {
750 doutc(cl, "failed to fill inode: %d\n", ret);
751 ceph_dir_clear_complete(dir);
752 if (!d_unhashed(dentry))
753 d_drop(dentry);
754 discard_new_inode(inode);
755 } else {
756 struct dentry *dn;
757
758 doutc(cl, "d_adding new inode 0x%llx to 0x%llx/%s\n",
759 vino.ino, ceph_ino(dir), dentry->d_name.name);
760 ceph_dir_clear_ordered(dir);
761 ceph_init_inode_acls(inode, as_ctx);
762 if (inode_state_read_once(inode) & I_NEW) {
763 /*
764 * If it's not I_NEW, then someone created this before
765 * we got here. Assume the server is aware of it at
766 * that point and don't worry about setting
767 * CEPH_I_ASYNC_CREATE.
768 */
769 ceph_inode(inode)->i_ceph_flags = CEPH_I_ASYNC_CREATE;
770 unlock_new_inode(inode);
771 }
772 if (d_in_lookup(dentry) || d_really_is_negative(dentry)) {
773 if (!d_unhashed(dentry))
774 d_drop(dentry);
775 dn = d_splice_alias(inode, dentry);
776 WARN_ON_ONCE(dn && dn != dentry);
777 }
778 file->f_mode |= FMODE_CREATED;
779 ret = finish_open(file, dentry, ceph_open);
780 }
781
782 spin_lock(&dentry->d_lock);
783 clear_and_wake_up_bit(CEPH_DENTRY_ASYNC_CREATE_BIT, &di->flags);
784 spin_unlock(&dentry->d_lock);
785
786 return ret;
787 }
788
789 /*
790 * Do a lookup + open with a single request. If we get a non-existent
791 * file or symlink, return 1 so the VFS can retry.
792 */
ceph_atomic_open(struct inode * dir,struct dentry * dentry,struct file * file,unsigned flags,umode_t mode)793 int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
794 struct file *file, unsigned flags, umode_t mode)
795 {
796 struct mnt_idmap *idmap = file_mnt_idmap(file);
797 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb);
798 struct ceph_client *cl = fsc->client;
799 struct ceph_mds_client *mdsc = fsc->mdsc;
800 struct ceph_mds_request *req;
801 struct inode *new_inode = NULL;
802 struct dentry *dn;
803 struct ceph_acl_sec_ctx as_ctx = {};
804 bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
805 int mask;
806 int err;
807 char *path;
808
809 doutc(cl, "%p %llx.%llx dentry %p '%pd' %s flags %d mode 0%o\n",
810 dir, ceph_vinop(dir), dentry, dentry,
811 d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
812
813 if (dentry->d_name.len > NAME_MAX)
814 return -ENAMETOOLONG;
815
816 err = ceph_wait_on_conflict_unlink(dentry);
817 if (err)
818 return err;
819 /*
820 * Do not truncate the file, since atomic_open is called before the
821 * permission check. The caller will do the truncation afterward.
822 */
823 flags &= ~O_TRUNC;
824
825 dn = d_find_alias(dir);
826 if (!dn) {
827 try_async = false;
828 } else {
829 struct ceph_path_info path_info = {0};
830 path = ceph_mdsc_build_path(mdsc, dn, &path_info, 0);
831 if (IS_ERR(path)) {
832 try_async = false;
833 err = 0;
834 } else {
835 int fmode = ceph_flags_to_mode(flags);
836
837 mask = MAY_READ;
838 if (fmode & CEPH_FILE_MODE_WR)
839 mask |= MAY_WRITE;
840 err = ceph_mds_check_access(mdsc, path, mask);
841 }
842 ceph_mdsc_free_path_info(&path_info);
843 dput(dn);
844
845 /* For none EACCES cases will let the MDS do the mds auth check */
846 if (err == -EACCES) {
847 return err;
848 } else if (err < 0) {
849 try_async = false;
850 err = 0;
851 }
852 }
853
854 retry:
855 if (flags & O_CREAT) {
856 if (ceph_quota_is_max_files_exceeded(dir))
857 return -EDQUOT;
858
859 new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx);
860 if (IS_ERR(new_inode)) {
861 err = PTR_ERR(new_inode);
862 goto out_ctx;
863 }
864 /* Async create can't handle more than a page of xattrs */
865 if (as_ctx.pagelist &&
866 !list_is_singular(&as_ctx.pagelist->head))
867 try_async = false;
868 } else if (!d_in_lookup(dentry)) {
869 /* If it's not being looked up, it's negative */
870 return -ENOENT;
871 }
872
873 /* do the open */
874 req = prepare_open_request(dir->i_sb, flags, mode);
875 if (IS_ERR(req)) {
876 err = PTR_ERR(req);
877 goto out_ctx;
878 }
879 req->r_dentry = dget(dentry);
880 req->r_num_caps = 2;
881 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
882 if (ceph_security_xattr_wanted(dir))
883 mask |= CEPH_CAP_XATTR_SHARED;
884 req->r_args.open.mask = cpu_to_le32(mask);
885 req->r_parent = dir;
886 if (req->r_op == CEPH_MDS_OP_CREATE)
887 req->r_mnt_idmap = mnt_idmap_get(idmap);
888 ihold(dir);
889 if (IS_ENCRYPTED(dir)) {
890 set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
891 err = fscrypt_prepare_lookup_partial(dir, dentry);
892 if (err < 0)
893 goto out_req;
894 }
895
896 if (flags & O_CREAT) {
897 struct ceph_file_layout lo;
898
899 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL |
900 CEPH_CAP_XATTR_EXCL;
901 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
902
903 ceph_as_ctx_to_req(req, &as_ctx);
904
905 if (try_async && (req->r_dir_caps =
906 try_prep_async_create(dir, dentry, &lo,
907 &req->r_deleg_ino))) {
908 struct ceph_vino vino = { .ino = req->r_deleg_ino,
909 .snap = CEPH_NOSNAP };
910 struct ceph_dentry_info *di = ceph_dentry(dentry);
911
912 set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
913 req->r_args.open.flags |= cpu_to_le32(CEPH_O_EXCL);
914 req->r_callback = ceph_async_create_cb;
915
916 /* Hash inode before RPC */
917 new_inode = ceph_get_inode(dir->i_sb, vino, new_inode);
918 if (IS_ERR(new_inode)) {
919 err = PTR_ERR(new_inode);
920 new_inode = NULL;
921 goto out_req;
922 }
923 WARN_ON_ONCE(!(inode_state_read_once(new_inode) & I_NEW));
924
925 spin_lock(&dentry->d_lock);
926 di->flags |= CEPH_DENTRY_ASYNC_CREATE;
927 spin_unlock(&dentry->d_lock);
928
929 err = ceph_mdsc_submit_request(mdsc, dir, req);
930 if (!err) {
931 err = ceph_finish_async_create(dir, new_inode,
932 dentry, file,
933 mode, req,
934 &as_ctx, &lo);
935 new_inode = NULL;
936 } else if (err == -EJUKEBOX) {
937 restore_deleg_ino(dir, req->r_deleg_ino);
938 ceph_mdsc_put_request(req);
939 discard_new_inode(new_inode);
940 ceph_release_acl_sec_ctx(&as_ctx);
941 memset(&as_ctx, 0, sizeof(as_ctx));
942 new_inode = NULL;
943 try_async = false;
944 ceph_put_string(rcu_dereference_raw(lo.pool_ns));
945 goto retry;
946 }
947 ceph_put_string(rcu_dereference_raw(lo.pool_ns));
948 goto out_req;
949 }
950 }
951
952 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
953 req->r_new_inode = new_inode;
954 new_inode = NULL;
955 err = ceph_mdsc_do_request(mdsc, (flags & O_CREAT) ? dir : NULL, req);
956 if (err == -ENOENT) {
957 dentry = ceph_handle_snapdir(req, dentry);
958 if (IS_ERR(dentry)) {
959 err = PTR_ERR(dentry);
960 goto out_req;
961 }
962 err = 0;
963 }
964
965 if (!err && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
966 err = ceph_handle_notrace_create(dir, dentry);
967
968 if (d_in_lookup(dentry)) {
969 dn = ceph_finish_lookup(req, dentry, err);
970 if (IS_ERR(dn))
971 err = PTR_ERR(dn);
972 } else {
973 /* we were given a hashed negative dentry */
974 dn = NULL;
975 }
976 if (err)
977 goto out_req;
978 if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
979 /* make vfs retry on splice, ENOENT, or symlink */
980 doutc(cl, "finish_no_open on dn %p\n", dn);
981 err = finish_no_open(file, dn);
982 } else {
983 if (IS_ENCRYPTED(dir) &&
984 !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
985 pr_warn_client(cl,
986 "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
987 ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
988 goto out_req;
989 }
990
991 doutc(cl, "finish_open on dn %p\n", dn);
992 if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
993 struct inode *newino = d_inode(dentry);
994
995 cache_file_layout(dir, newino);
996 ceph_init_inode_acls(newino, &as_ctx);
997 file->f_mode |= FMODE_CREATED;
998 }
999 err = finish_open(file, dentry, ceph_open);
1000 }
1001 out_req:
1002 ceph_mdsc_put_request(req);
1003 iput(new_inode);
1004 out_ctx:
1005 ceph_release_acl_sec_ctx(&as_ctx);
1006 doutc(cl, "result=%d\n", err);
1007 return err;
1008 }
1009
ceph_release(struct inode * inode,struct file * file)1010 int ceph_release(struct inode *inode, struct file *file)
1011 {
1012 struct ceph_client *cl = ceph_inode_to_client(inode);
1013 struct ceph_inode_info *ci = ceph_inode(inode);
1014
1015 if (S_ISDIR(inode->i_mode)) {
1016 struct ceph_dir_file_info *dfi = file->private_data;
1017 doutc(cl, "%p %llx.%llx dir file %p\n", inode,
1018 ceph_vinop(inode), file);
1019 WARN_ON(!list_empty(&dfi->file_info.rw_contexts));
1020
1021 ceph_put_fmode(ci, dfi->file_info.fmode, 1);
1022
1023 if (dfi->last_readdir)
1024 ceph_mdsc_put_request(dfi->last_readdir);
1025 kfree(dfi->last_name);
1026 kfree(dfi->dir_info);
1027 kmem_cache_free(ceph_dir_file_cachep, dfi);
1028 } else {
1029 struct ceph_file_info *fi = file->private_data;
1030 doutc(cl, "%p %llx.%llx regular file %p\n", inode,
1031 ceph_vinop(inode), file);
1032 WARN_ON(!list_empty(&fi->rw_contexts));
1033
1034 ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE);
1035 ceph_put_fmode(ci, fi->fmode, 1);
1036
1037 kmem_cache_free(ceph_file_cachep, fi);
1038 }
1039
1040 /* wake up anyone waiting for caps on this inode */
1041 wake_up_all(&ci->i_cap_wq);
1042 return 0;
1043 }
1044
1045 enum {
1046 HAVE_RETRIED = 1,
1047 CHECK_EOF = 2,
1048 READ_INLINE = 3,
1049 };
1050
1051 /*
1052 * Completely synchronous read and write methods. Direct from __user
1053 * buffer to osd, or directly to user pages (if O_DIRECT).
1054 *
1055 * If the read spans object boundary, just do multiple reads. (That's not
1056 * atomic, but good enough for now.)
1057 *
1058 * If we get a short result from the OSD, check against i_size; we need to
1059 * only return a short read to the caller if we hit EOF.
1060 */
__ceph_sync_read(struct inode * inode,loff_t * ki_pos,struct iov_iter * to,int * retry_op,u64 * last_objver)1061 ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
1062 struct iov_iter *to, int *retry_op,
1063 u64 *last_objver)
1064 {
1065 struct ceph_inode_info *ci = ceph_inode(inode);
1066 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1067 struct ceph_client *cl = fsc->client;
1068 struct ceph_osd_client *osdc = &fsc->client->osdc;
1069 ssize_t ret;
1070 u64 off = *ki_pos;
1071 u64 len = iov_iter_count(to);
1072 u64 i_size = i_size_read(inode);
1073 bool sparse = IS_ENCRYPTED(inode) || ceph_test_mount_opt(fsc, SPARSEREAD);
1074 u64 objver = 0;
1075
1076 doutc(cl, "on inode %p %llx.%llx %llx~%llx\n", inode,
1077 ceph_vinop(inode), *ki_pos, len);
1078
1079 if (ceph_inode_is_shutdown(inode))
1080 return -EIO;
1081
1082 if (!len || !i_size)
1083 return 0;
1084 /*
1085 * flush any page cache pages in this range. this
1086 * will make concurrent normal and sync io slow,
1087 * but it will at least behave sensibly when they are
1088 * in sequence.
1089 */
1090 ret = filemap_write_and_wait_range(inode->i_mapping,
1091 off, off + len - 1);
1092 if (ret < 0)
1093 return ret;
1094
1095 ret = 0;
1096 while ((len = iov_iter_count(to)) > 0) {
1097 struct ceph_osd_request *req;
1098 struct page **pages;
1099 int num_pages;
1100 size_t page_off;
1101 bool more;
1102 int idx = 0;
1103 size_t left;
1104 struct ceph_osd_req_op *op;
1105 u64 read_off = off;
1106 u64 read_len = len;
1107 int extent_cnt;
1108
1109 /* determine new offset/length if encrypted */
1110 ceph_fscrypt_adjust_off_and_len(inode, &read_off, &read_len);
1111
1112 doutc(cl, "orig %llu~%llu reading %llu~%llu", off, len,
1113 read_off, read_len);
1114
1115 req = ceph_osdc_new_request(osdc, &ci->i_layout,
1116 ci->i_vino, read_off, &read_len, 0, 1,
1117 sparse ? CEPH_OSD_OP_SPARSE_READ :
1118 CEPH_OSD_OP_READ,
1119 CEPH_OSD_FLAG_READ,
1120 NULL, ci->i_truncate_seq,
1121 ci->i_truncate_size, false);
1122 if (IS_ERR(req)) {
1123 ret = PTR_ERR(req);
1124 break;
1125 }
1126
1127 /* adjust len downward if the request truncated the len */
1128 if (off + len > read_off + read_len)
1129 len = read_off + read_len - off;
1130 more = len < iov_iter_count(to);
1131
1132 op = &req->r_ops[0];
1133 if (sparse) {
1134 extent_cnt = __ceph_sparse_read_ext_count(inode, read_len);
1135 ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
1136 if (ret) {
1137 ceph_osdc_put_request(req);
1138 break;
1139 }
1140 }
1141
1142 num_pages = calc_pages_for(read_off, read_len);
1143 page_off = offset_in_page(off);
1144 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1145 if (IS_ERR(pages)) {
1146 ceph_osdc_put_request(req);
1147 ret = PTR_ERR(pages);
1148 break;
1149 }
1150
1151 osd_req_op_extent_osd_data_pages(req, 0, pages, read_len,
1152 offset_in_page(read_off),
1153 false, true);
1154
1155 ceph_osdc_start_request(osdc, req);
1156 ret = ceph_osdc_wait_request(osdc, req);
1157
1158 ceph_update_read_metrics(&fsc->mdsc->metric,
1159 req->r_start_latency,
1160 req->r_end_latency,
1161 read_len, ret);
1162 /*
1163 * Only record subvolume metrics for actual bytes read.
1164 * ret == 0 means EOF (no data), not an I/O operation.
1165 */
1166 if (ret > 0)
1167 ceph_record_subvolume_io(inode, false,
1168 req->r_start_latency,
1169 req->r_end_latency,
1170 ret);
1171
1172 if (ret > 0)
1173 objver = req->r_version;
1174
1175 i_size = i_size_read(inode);
1176 doutc(cl, "%llu~%llu got %zd i_size %llu%s\n", off, len,
1177 ret, i_size, (more ? " MORE" : ""));
1178
1179 /* Fix it to go to end of extent map */
1180 if (sparse && ret >= 0)
1181 ret = ceph_sparse_ext_map_end(op);
1182 else if (ret == -ENOENT)
1183 ret = 0;
1184
1185 if (ret < 0) {
1186 ceph_osdc_put_request(req);
1187 if (ret == -EBLOCKLISTED)
1188 fsc->blocklisted = true;
1189 break;
1190 }
1191
1192 if (IS_ENCRYPTED(inode)) {
1193 int fret;
1194
1195 fret = ceph_fscrypt_decrypt_extents(inode, pages,
1196 read_off, op->extent.sparse_ext,
1197 op->extent.sparse_ext_cnt);
1198 if (fret < 0) {
1199 ret = fret;
1200 ceph_osdc_put_request(req);
1201 break;
1202 }
1203
1204 /* account for any partial block at the beginning */
1205 fret -= (off - read_off);
1206
1207 /*
1208 * Short read after big offset adjustment?
1209 * Nothing is usable, just call it a zero
1210 * len read.
1211 */
1212 fret = max(fret, 0);
1213
1214 /* account for partial block at the end */
1215 ret = min_t(ssize_t, fret, len);
1216 }
1217
1218 /* Short read but not EOF? Zero out the remainder. */
1219 if (ret < len && (off + ret < i_size)) {
1220 int zlen = min(len - ret, i_size - off - ret);
1221 int zoff = page_off + ret;
1222
1223 doutc(cl, "zero gap %llu~%llu\n", off + ret,
1224 off + ret + zlen);
1225 ceph_zero_page_vector_range(zoff, zlen, pages);
1226 ret += zlen;
1227 }
1228
1229 if (off + ret > i_size)
1230 left = (i_size > off) ? i_size - off : 0;
1231 else
1232 left = ret;
1233
1234 while (left > 0) {
1235 size_t plen, copied;
1236
1237 plen = min_t(size_t, left, PAGE_SIZE - page_off);
1238 SetPageUptodate(pages[idx]);
1239 copied = copy_page_to_iter(pages[idx++],
1240 page_off, plen, to);
1241 off += copied;
1242 left -= copied;
1243 page_off = 0;
1244 if (copied < plen) {
1245 ret = -EFAULT;
1246 break;
1247 }
1248 }
1249
1250 ceph_osdc_put_request(req);
1251
1252 if (off >= i_size || !more)
1253 break;
1254 }
1255
1256 if (ret > 0) {
1257 if (off >= i_size) {
1258 *retry_op = CHECK_EOF;
1259 ret = i_size - *ki_pos;
1260 *ki_pos = i_size;
1261 } else {
1262 ret = off - *ki_pos;
1263 *ki_pos = off;
1264 }
1265
1266 if (last_objver)
1267 *last_objver = objver;
1268 }
1269 doutc(cl, "result %zd retry_op %d\n", ret, *retry_op);
1270 return ret;
1271 }
1272
ceph_sync_read(struct kiocb * iocb,struct iov_iter * to,int * retry_op)1273 static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
1274 int *retry_op)
1275 {
1276 struct file *file = iocb->ki_filp;
1277 struct inode *inode = file_inode(file);
1278 struct ceph_client *cl = ceph_inode_to_client(inode);
1279
1280 doutc(cl, "on file %p %llx~%zx %s\n", file, iocb->ki_pos,
1281 iov_iter_count(to),
1282 (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
1283
1284 return __ceph_sync_read(inode, &iocb->ki_pos, to, retry_op, NULL);
1285 }
1286
1287 struct ceph_aio_request {
1288 struct kiocb *iocb;
1289 size_t total_len;
1290 bool write;
1291 bool should_dirty;
1292 int error;
1293 struct list_head osd_reqs;
1294 unsigned num_reqs;
1295 atomic_t pending_reqs;
1296 struct timespec64 mtime;
1297 struct ceph_cap_flush *prealloc_cf;
1298 };
1299
1300 struct ceph_aio_work {
1301 struct work_struct work;
1302 struct ceph_osd_request *req;
1303 };
1304
1305 static void ceph_aio_retry_work(struct work_struct *work);
1306
ceph_aio_complete(struct inode * inode,struct ceph_aio_request * aio_req)1307 static void ceph_aio_complete(struct inode *inode,
1308 struct ceph_aio_request *aio_req)
1309 {
1310 struct ceph_client *cl = ceph_inode_to_client(inode);
1311 struct ceph_inode_info *ci = ceph_inode(inode);
1312 int ret;
1313
1314 if (!atomic_dec_and_test(&aio_req->pending_reqs))
1315 return;
1316
1317 if (aio_req->iocb->ki_flags & IOCB_DIRECT)
1318 inode_dio_end(inode);
1319
1320 ret = aio_req->error;
1321 if (!ret)
1322 ret = aio_req->total_len;
1323
1324 doutc(cl, "%p %llx.%llx rc %d\n", inode, ceph_vinop(inode), ret);
1325
1326 if (ret >= 0 && aio_req->write) {
1327 int dirty;
1328
1329 loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
1330 if (endoff > i_size_read(inode)) {
1331 if (ceph_inode_set_size(inode, endoff))
1332 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY);
1333 }
1334
1335 spin_lock(&ci->i_ceph_lock);
1336 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1337 &aio_req->prealloc_cf);
1338 spin_unlock(&ci->i_ceph_lock);
1339 if (dirty)
1340 __mark_inode_dirty(inode, dirty);
1341
1342 }
1343
1344 ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
1345 CEPH_CAP_FILE_RD));
1346
1347 aio_req->iocb->ki_complete(aio_req->iocb, ret);
1348
1349 ceph_free_cap_flush(aio_req->prealloc_cf);
1350 kfree(aio_req);
1351 }
1352
ceph_aio_complete_req(struct ceph_osd_request * req)1353 static void ceph_aio_complete_req(struct ceph_osd_request *req)
1354 {
1355 int rc = req->r_result;
1356 struct inode *inode = req->r_inode;
1357 struct ceph_aio_request *aio_req = req->r_priv;
1358 struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
1359 struct ceph_osd_req_op *op = &req->r_ops[0];
1360 struct ceph_client_metric *metric = &ceph_sb_to_mdsc(inode->i_sb)->metric;
1361 unsigned int len = osd_data->bvec_pos.iter.bi_size;
1362 bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ);
1363 struct ceph_client *cl = ceph_inode_to_client(inode);
1364
1365 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_BVECS);
1366 BUG_ON(!osd_data->num_bvecs);
1367
1368 doutc(cl, "req %p inode %p %llx.%llx, rc %d bytes %u\n", req,
1369 inode, ceph_vinop(inode), rc, len);
1370
1371 if (rc == -EOLDSNAPC) {
1372 struct ceph_aio_work *aio_work;
1373 BUG_ON(!aio_req->write);
1374
1375 aio_work = kmalloc_obj(*aio_work, GFP_NOFS);
1376 if (aio_work) {
1377 INIT_WORK(&aio_work->work, ceph_aio_retry_work);
1378 aio_work->req = req;
1379 queue_work(ceph_inode_to_fs_client(inode)->inode_wq,
1380 &aio_work->work);
1381 return;
1382 }
1383 rc = -ENOMEM;
1384 } else if (!aio_req->write) {
1385 if (sparse && rc >= 0)
1386 rc = ceph_sparse_ext_map_end(op);
1387 if (rc == -ENOENT)
1388 rc = 0;
1389 if (rc >= 0 && len > rc) {
1390 struct iov_iter i;
1391 int zlen = len - rc;
1392
1393 /*
1394 * If read is satisfied by single OSD request,
1395 * it can pass EOF. Otherwise read is within
1396 * i_size.
1397 */
1398 if (aio_req->num_reqs == 1) {
1399 loff_t i_size = i_size_read(inode);
1400 loff_t endoff = aio_req->iocb->ki_pos + rc;
1401 if (endoff < i_size)
1402 zlen = min_t(size_t, zlen,
1403 i_size - endoff);
1404 aio_req->total_len = rc + zlen;
1405 }
1406
1407 iov_iter_bvec(&i, ITER_DEST, osd_data->bvec_pos.bvecs,
1408 osd_data->num_bvecs, len);
1409 iov_iter_advance(&i, rc);
1410 iov_iter_zero(zlen, &i);
1411 }
1412 }
1413
1414 /* r_start_latency == 0 means the request was not submitted */
1415 if (req->r_start_latency) {
1416 if (aio_req->write) {
1417 ceph_update_write_metrics(metric, req->r_start_latency,
1418 req->r_end_latency, len, rc);
1419 if (rc >= 0 && len)
1420 ceph_record_subvolume_io(inode, true,
1421 req->r_start_latency,
1422 req->r_end_latency,
1423 len);
1424 } else {
1425 ceph_update_read_metrics(metric, req->r_start_latency,
1426 req->r_end_latency, len, rc);
1427 if (rc > 0)
1428 ceph_record_subvolume_io(inode, false,
1429 req->r_start_latency,
1430 req->r_end_latency,
1431 rc);
1432 }
1433 }
1434
1435 put_bvecs(osd_data->bvec_pos.bvecs, osd_data->num_bvecs,
1436 aio_req->should_dirty);
1437 ceph_osdc_put_request(req);
1438
1439 if (rc < 0)
1440 cmpxchg(&aio_req->error, 0, rc);
1441
1442 ceph_aio_complete(inode, aio_req);
1443 return;
1444 }
1445
ceph_aio_retry_work(struct work_struct * work)1446 static void ceph_aio_retry_work(struct work_struct *work)
1447 {
1448 struct ceph_aio_work *aio_work =
1449 container_of(work, struct ceph_aio_work, work);
1450 struct ceph_osd_request *orig_req = aio_work->req;
1451 struct ceph_aio_request *aio_req = orig_req->r_priv;
1452 struct inode *inode = orig_req->r_inode;
1453 struct ceph_inode_info *ci = ceph_inode(inode);
1454 struct ceph_snap_context *snapc;
1455 struct ceph_osd_request *req;
1456 int ret;
1457
1458 spin_lock(&ci->i_ceph_lock);
1459 if (__ceph_have_pending_cap_snap(ci)) {
1460 struct ceph_cap_snap *capsnap =
1461 list_last_entry(&ci->i_cap_snaps,
1462 struct ceph_cap_snap,
1463 ci_item);
1464 snapc = ceph_get_snap_context(capsnap->context);
1465 } else {
1466 BUG_ON(!ci->i_head_snapc);
1467 snapc = ceph_get_snap_context(ci->i_head_snapc);
1468 }
1469 spin_unlock(&ci->i_ceph_lock);
1470
1471 req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 1,
1472 false, GFP_NOFS);
1473 if (!req) {
1474 ret = -ENOMEM;
1475 req = orig_req;
1476 goto out;
1477 }
1478
1479 req->r_flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
1480 ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
1481 ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
1482
1483 req->r_ops[0] = orig_req->r_ops[0];
1484
1485 req->r_mtime = aio_req->mtime;
1486 req->r_data_offset = req->r_ops[0].extent.offset;
1487
1488 ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
1489 if (ret) {
1490 ceph_osdc_put_request(req);
1491 req = orig_req;
1492 goto out;
1493 }
1494
1495 ceph_osdc_put_request(orig_req);
1496
1497 req->r_callback = ceph_aio_complete_req;
1498 req->r_inode = inode;
1499 req->r_priv = aio_req;
1500
1501 ceph_osdc_start_request(req->r_osdc, req);
1502 out:
1503 if (ret < 0) {
1504 req->r_result = ret;
1505 ceph_aio_complete_req(req);
1506 }
1507
1508 ceph_put_snap_context(snapc);
1509 kfree(aio_work);
1510 }
1511
1512 static ssize_t
ceph_direct_read_write(struct kiocb * iocb,struct iov_iter * iter,struct ceph_snap_context * snapc,struct ceph_cap_flush ** pcf)1513 ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
1514 struct ceph_snap_context *snapc,
1515 struct ceph_cap_flush **pcf)
1516 {
1517 struct file *file = iocb->ki_filp;
1518 struct inode *inode = file_inode(file);
1519 struct ceph_inode_info *ci = ceph_inode(inode);
1520 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1521 struct ceph_client *cl = fsc->client;
1522 struct ceph_client_metric *metric = &fsc->mdsc->metric;
1523 struct ceph_vino vino;
1524 struct ceph_osd_request *req;
1525 struct bio_vec *bvecs;
1526 struct ceph_aio_request *aio_req = NULL;
1527 int num_pages = 0;
1528 int flags;
1529 int ret = 0;
1530 struct timespec64 mtime = current_time(inode);
1531 size_t count = iov_iter_count(iter);
1532 loff_t pos = iocb->ki_pos;
1533 bool write = iov_iter_rw(iter) == WRITE;
1534 bool should_dirty = !write && user_backed_iter(iter);
1535 bool sparse = ceph_test_mount_opt(fsc, SPARSEREAD);
1536
1537 if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
1538 return -EROFS;
1539
1540 doutc(cl, "sync_direct_%s on file %p %lld~%u snapc %p seq %lld\n",
1541 (write ? "write" : "read"), file, pos, (unsigned)count,
1542 snapc, snapc ? snapc->seq : 0);
1543
1544 if (write) {
1545 int ret2;
1546
1547 ceph_fscache_invalidate(inode, true);
1548
1549 ret2 = invalidate_inode_pages2_range(inode->i_mapping,
1550 pos >> PAGE_SHIFT,
1551 (pos + count - 1) >> PAGE_SHIFT);
1552 if (ret2 < 0)
1553 doutc(cl, "invalidate_inode_pages2_range returned %d\n",
1554 ret2);
1555
1556 flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
1557 } else {
1558 flags = CEPH_OSD_FLAG_READ;
1559 }
1560
1561 while (iov_iter_count(iter) > 0) {
1562 u64 size = iov_iter_count(iter);
1563 ssize_t len;
1564 struct ceph_osd_req_op *op;
1565 int readop = sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ;
1566 int extent_cnt;
1567
1568 if (write)
1569 size = min_t(u64, size, fsc->mount_options->wsize);
1570 else
1571 size = min_t(u64, size, fsc->mount_options->rsize);
1572
1573 vino = ceph_vino(inode);
1574 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1575 vino, pos, &size, 0,
1576 1,
1577 write ? CEPH_OSD_OP_WRITE : readop,
1578 flags, snapc,
1579 ci->i_truncate_seq,
1580 ci->i_truncate_size,
1581 false);
1582 if (IS_ERR(req)) {
1583 ret = PTR_ERR(req);
1584 break;
1585 }
1586
1587 op = &req->r_ops[0];
1588 if (!write && sparse) {
1589 extent_cnt = __ceph_sparse_read_ext_count(inode, size);
1590 ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
1591 if (ret) {
1592 ceph_osdc_put_request(req);
1593 break;
1594 }
1595 }
1596
1597 len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages);
1598 if (len < 0) {
1599 ceph_osdc_put_request(req);
1600 ret = len;
1601 break;
1602 }
1603 if (len != size)
1604 osd_req_op_extent_update(req, 0, len);
1605
1606 osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len);
1607
1608 /*
1609 * To simplify error handling, allow AIO when IO within i_size
1610 * or IO can be satisfied by single OSD request.
1611 */
1612 if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
1613 (len == count || pos + count <= i_size_read(inode))) {
1614 aio_req = kzalloc_obj(*aio_req);
1615 if (aio_req) {
1616 aio_req->iocb = iocb;
1617 aio_req->write = write;
1618 aio_req->should_dirty = should_dirty;
1619 INIT_LIST_HEAD(&aio_req->osd_reqs);
1620 if (write) {
1621 aio_req->mtime = mtime;
1622 swap(aio_req->prealloc_cf, *pcf);
1623 }
1624 }
1625 /* ignore error */
1626 }
1627
1628 if (write) {
1629 /*
1630 * throw out any page cache pages in this range. this
1631 * may block.
1632 */
1633 truncate_inode_pages_range(inode->i_mapping, pos,
1634 PAGE_ALIGN(pos + len) - 1);
1635
1636 req->r_mtime = mtime;
1637 }
1638
1639 if (aio_req) {
1640 aio_req->total_len += len;
1641 aio_req->num_reqs++;
1642 atomic_inc(&aio_req->pending_reqs);
1643
1644 req->r_callback = ceph_aio_complete_req;
1645 req->r_inode = inode;
1646 req->r_priv = aio_req;
1647 list_add_tail(&req->r_private_item, &aio_req->osd_reqs);
1648
1649 pos += len;
1650 continue;
1651 }
1652
1653 ceph_osdc_start_request(req->r_osdc, req);
1654 ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1655
1656 if (write) {
1657 ceph_update_write_metrics(metric, req->r_start_latency,
1658 req->r_end_latency, len, ret);
1659 if (ret >= 0 && len)
1660 ceph_record_subvolume_io(inode, true,
1661 req->r_start_latency,
1662 req->r_end_latency,
1663 len);
1664 } else {
1665 ceph_update_read_metrics(metric, req->r_start_latency,
1666 req->r_end_latency, len, ret);
1667 if (ret > 0)
1668 ceph_record_subvolume_io(inode, false,
1669 req->r_start_latency,
1670 req->r_end_latency,
1671 ret);
1672 }
1673
1674 size = i_size_read(inode);
1675 if (!write) {
1676 if (sparse && ret >= 0)
1677 ret = ceph_sparse_ext_map_end(op);
1678 else if (ret == -ENOENT)
1679 ret = 0;
1680
1681 if (ret >= 0 && ret < len && pos + ret < size) {
1682 struct iov_iter i;
1683 int zlen = min_t(size_t, len - ret,
1684 size - pos - ret);
1685
1686 iov_iter_bvec(&i, ITER_DEST, bvecs, num_pages, len);
1687 iov_iter_advance(&i, ret);
1688 iov_iter_zero(zlen, &i);
1689 ret += zlen;
1690 }
1691 if (ret >= 0)
1692 len = ret;
1693 }
1694
1695 put_bvecs(bvecs, num_pages, should_dirty);
1696 ceph_osdc_put_request(req);
1697 if (ret < 0)
1698 break;
1699
1700 pos += len;
1701 if (!write && pos >= size)
1702 break;
1703
1704 if (write && pos > size) {
1705 if (ceph_inode_set_size(inode, pos))
1706 ceph_check_caps(ceph_inode(inode),
1707 CHECK_CAPS_AUTHONLY);
1708 }
1709 }
1710
1711 if (aio_req) {
1712 LIST_HEAD(osd_reqs);
1713
1714 if (aio_req->num_reqs == 0) {
1715 kfree(aio_req);
1716 return ret;
1717 }
1718
1719 ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
1720 CEPH_CAP_FILE_RD);
1721
1722 list_splice(&aio_req->osd_reqs, &osd_reqs);
1723 inode_dio_begin(inode);
1724 while (!list_empty(&osd_reqs)) {
1725 req = list_first_entry(&osd_reqs,
1726 struct ceph_osd_request,
1727 r_private_item);
1728 list_del_init(&req->r_private_item);
1729 if (ret >= 0)
1730 ceph_osdc_start_request(req->r_osdc, req);
1731 if (ret < 0) {
1732 req->r_result = ret;
1733 ceph_aio_complete_req(req);
1734 }
1735 }
1736 return -EIOCBQUEUED;
1737 }
1738
1739 if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
1740 ret = pos - iocb->ki_pos;
1741 iocb->ki_pos = pos;
1742 }
1743 return ret;
1744 }
1745
1746 /*
1747 * Synchronous write, straight from __user pointer or user pages.
1748 *
1749 * If write spans object boundary, just do multiple writes. (For a
1750 * correct atomic write, we should e.g. take write locks on all
1751 * objects, rollback on failure, etc.)
1752 */
1753 static ssize_t
ceph_sync_write(struct kiocb * iocb,struct iov_iter * from,loff_t pos,struct ceph_snap_context * snapc)1754 ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
1755 struct ceph_snap_context *snapc)
1756 {
1757 struct file *file = iocb->ki_filp;
1758 struct inode *inode = file_inode(file);
1759 struct ceph_inode_info *ci = ceph_inode(inode);
1760 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
1761 struct ceph_client *cl = fsc->client;
1762 struct ceph_osd_client *osdc = &fsc->client->osdc;
1763 struct ceph_osd_request *req;
1764 struct page **pages;
1765 u64 len;
1766 int num_pages;
1767 int written = 0;
1768 int ret;
1769 bool check_caps = false;
1770 struct timespec64 mtime = current_time(inode);
1771 size_t count = iov_iter_count(from);
1772
1773 if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
1774 return -EROFS;
1775
1776 doutc(cl, "on file %p %lld~%u snapc %p seq %lld\n", file, pos,
1777 (unsigned)count, snapc, snapc->seq);
1778
1779 ret = filemap_write_and_wait_range(inode->i_mapping,
1780 pos, pos + count - 1);
1781 if (ret < 0)
1782 return ret;
1783
1784 ceph_fscache_invalidate(inode, false);
1785
1786 while ((len = iov_iter_count(from)) > 0) {
1787 size_t left;
1788 int n;
1789 u64 write_pos = pos;
1790 u64 write_len = len;
1791 u64 objnum, objoff;
1792 u32 xlen;
1793 u64 assert_ver = 0;
1794 bool rmw;
1795 bool first, last;
1796 struct iov_iter saved_iter = *from;
1797 size_t off;
1798
1799 ceph_fscrypt_adjust_off_and_len(inode, &write_pos, &write_len);
1800
1801 /* clamp the length to the end of first object */
1802 ceph_calc_file_object_mapping(&ci->i_layout, write_pos,
1803 write_len, &objnum, &objoff,
1804 &xlen);
1805 write_len = xlen;
1806
1807 /* adjust len downward if it goes beyond current object */
1808 if (pos + len > write_pos + write_len)
1809 len = write_pos + write_len - pos;
1810
1811 /*
1812 * If we had to adjust the length or position to align with a
1813 * crypto block, then we must do a read/modify/write cycle. We
1814 * use a version assertion to redrive the thing if something
1815 * changes in between.
1816 */
1817 first = pos != write_pos;
1818 last = (pos + len) != (write_pos + write_len);
1819 rmw = first || last;
1820
1821 doutc(cl, "ino %llx %lld~%llu adjusted %lld~%llu -- %srmw\n",
1822 ci->i_vino.ino, pos, len, write_pos, write_len,
1823 rmw ? "" : "no ");
1824
1825 /*
1826 * The data is emplaced into the page as it would be if it were
1827 * in an array of pagecache pages.
1828 */
1829 num_pages = calc_pages_for(write_pos, write_len);
1830 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1831 if (IS_ERR(pages)) {
1832 ret = PTR_ERR(pages);
1833 break;
1834 }
1835
1836 /* Do we need to preload the pages? */
1837 if (rmw) {
1838 u64 first_pos = write_pos;
1839 u64 last_pos = (write_pos + write_len) - CEPH_FSCRYPT_BLOCK_SIZE;
1840 u64 read_len = CEPH_FSCRYPT_BLOCK_SIZE;
1841 struct ceph_osd_req_op *op;
1842
1843 /* We should only need to do this for encrypted inodes */
1844 WARN_ON_ONCE(!IS_ENCRYPTED(inode));
1845
1846 /* No need to do two reads if first and last blocks are same */
1847 if (first && last_pos == first_pos)
1848 last = false;
1849
1850 /*
1851 * Allocate a read request for one or two extents,
1852 * depending on how the request was aligned.
1853 */
1854 req = ceph_osdc_new_request(osdc, &ci->i_layout,
1855 ci->i_vino, first ? first_pos : last_pos,
1856 &read_len, 0, (first && last) ? 2 : 1,
1857 CEPH_OSD_OP_SPARSE_READ, CEPH_OSD_FLAG_READ,
1858 NULL, ci->i_truncate_seq,
1859 ci->i_truncate_size, false);
1860 if (IS_ERR(req)) {
1861 ceph_release_page_vector(pages, num_pages);
1862 ret = PTR_ERR(req);
1863 break;
1864 }
1865
1866 /* Something is misaligned! */
1867 if (read_len != CEPH_FSCRYPT_BLOCK_SIZE) {
1868 ceph_osdc_put_request(req);
1869 ceph_release_page_vector(pages, num_pages);
1870 ret = -EIO;
1871 break;
1872 }
1873
1874 /* Add extent for first block? */
1875 op = &req->r_ops[0];
1876
1877 if (first) {
1878 osd_req_op_extent_osd_data_pages(req, 0, pages,
1879 CEPH_FSCRYPT_BLOCK_SIZE,
1880 offset_in_page(first_pos),
1881 false, false);
1882 /* We only expect a single extent here */
1883 ret = __ceph_alloc_sparse_ext_map(op, 1);
1884 if (ret) {
1885 ceph_osdc_put_request(req);
1886 ceph_release_page_vector(pages, num_pages);
1887 break;
1888 }
1889 }
1890
1891 /* Add extent for last block */
1892 if (last) {
1893 /* Init the other extent if first extent has been used */
1894 if (first) {
1895 op = &req->r_ops[1];
1896 osd_req_op_extent_init(req, 1,
1897 CEPH_OSD_OP_SPARSE_READ,
1898 last_pos, CEPH_FSCRYPT_BLOCK_SIZE,
1899 ci->i_truncate_size,
1900 ci->i_truncate_seq);
1901 }
1902
1903 ret = __ceph_alloc_sparse_ext_map(op, 1);
1904 if (ret) {
1905 ceph_osdc_put_request(req);
1906 ceph_release_page_vector(pages, num_pages);
1907 break;
1908 }
1909
1910 osd_req_op_extent_osd_data_pages(req, first ? 1 : 0,
1911 &pages[num_pages - 1],
1912 CEPH_FSCRYPT_BLOCK_SIZE,
1913 offset_in_page(last_pos),
1914 false, false);
1915 }
1916
1917 ceph_osdc_start_request(osdc, req);
1918 ret = ceph_osdc_wait_request(osdc, req);
1919
1920 /* FIXME: length field is wrong if there are 2 extents */
1921 ceph_update_read_metrics(&fsc->mdsc->metric,
1922 req->r_start_latency,
1923 req->r_end_latency,
1924 read_len, ret);
1925 if (ret > 0)
1926 ceph_record_subvolume_io(inode, false,
1927 req->r_start_latency,
1928 req->r_end_latency,
1929 ret);
1930
1931 /* Ok if object is not already present */
1932 if (ret == -ENOENT) {
1933 /*
1934 * If there is no object, then we can't assert
1935 * on its version. Set it to 0, and we'll use an
1936 * exclusive create instead.
1937 */
1938 ceph_osdc_put_request(req);
1939 ret = 0;
1940
1941 /*
1942 * zero out the soon-to-be uncopied parts of the
1943 * first and last pages.
1944 */
1945 if (first)
1946 zero_user_segment(pages[0], 0,
1947 offset_in_page(first_pos));
1948 if (last)
1949 zero_user_segment(pages[num_pages - 1],
1950 offset_in_page(last_pos),
1951 PAGE_SIZE);
1952 } else {
1953 if (ret < 0) {
1954 ceph_osdc_put_request(req);
1955 ceph_release_page_vector(pages, num_pages);
1956 break;
1957 }
1958
1959 op = &req->r_ops[0];
1960 if (op->extent.sparse_ext_cnt == 0) {
1961 if (first)
1962 zero_user_segment(pages[0], 0,
1963 offset_in_page(first_pos));
1964 else
1965 zero_user_segment(pages[num_pages - 1],
1966 offset_in_page(last_pos),
1967 PAGE_SIZE);
1968 } else if (op->extent.sparse_ext_cnt != 1 ||
1969 ceph_sparse_ext_map_end(op) !=
1970 CEPH_FSCRYPT_BLOCK_SIZE) {
1971 ret = -EIO;
1972 ceph_osdc_put_request(req);
1973 ceph_release_page_vector(pages, num_pages);
1974 break;
1975 }
1976
1977 if (first && last) {
1978 op = &req->r_ops[1];
1979 if (op->extent.sparse_ext_cnt == 0) {
1980 zero_user_segment(pages[num_pages - 1],
1981 offset_in_page(last_pos),
1982 PAGE_SIZE);
1983 } else if (op->extent.sparse_ext_cnt != 1 ||
1984 ceph_sparse_ext_map_end(op) !=
1985 CEPH_FSCRYPT_BLOCK_SIZE) {
1986 ret = -EIO;
1987 ceph_osdc_put_request(req);
1988 ceph_release_page_vector(pages, num_pages);
1989 break;
1990 }
1991 }
1992
1993 /* Grab assert version. It must be non-zero. */
1994 assert_ver = req->r_version;
1995 WARN_ON_ONCE(ret > 0 && assert_ver == 0);
1996
1997 ceph_osdc_put_request(req);
1998 if (first) {
1999 ret = ceph_fscrypt_decrypt_block_inplace(inode,
2000 pages[0], CEPH_FSCRYPT_BLOCK_SIZE,
2001 offset_in_page(first_pos),
2002 first_pos >> CEPH_FSCRYPT_BLOCK_SHIFT);
2003 if (ret < 0) {
2004 ceph_release_page_vector(pages, num_pages);
2005 break;
2006 }
2007 }
2008 if (last) {
2009 ret = ceph_fscrypt_decrypt_block_inplace(inode,
2010 pages[num_pages - 1],
2011 CEPH_FSCRYPT_BLOCK_SIZE,
2012 offset_in_page(last_pos),
2013 last_pos >> CEPH_FSCRYPT_BLOCK_SHIFT);
2014 if (ret < 0) {
2015 ceph_release_page_vector(pages, num_pages);
2016 break;
2017 }
2018 }
2019 }
2020 }
2021
2022 left = len;
2023 off = offset_in_page(pos);
2024 for (n = 0; n < num_pages; n++) {
2025 size_t plen = min_t(size_t, left, PAGE_SIZE - off);
2026
2027 /* copy the data */
2028 ret = copy_page_from_iter(pages[n], off, plen, from);
2029 if (ret != plen) {
2030 ret = -EFAULT;
2031 break;
2032 }
2033 off = 0;
2034 left -= ret;
2035 }
2036 if (ret < 0) {
2037 doutc(cl, "write failed with %d\n", ret);
2038 ceph_release_page_vector(pages, num_pages);
2039 break;
2040 }
2041
2042 if (IS_ENCRYPTED(inode)) {
2043 ret = ceph_fscrypt_encrypt_pages(inode, pages,
2044 write_pos, write_len);
2045 if (ret < 0) {
2046 doutc(cl, "encryption failed with %d\n", ret);
2047 ceph_release_page_vector(pages, num_pages);
2048 break;
2049 }
2050 }
2051
2052 req = ceph_osdc_new_request(osdc, &ci->i_layout,
2053 ci->i_vino, write_pos, &write_len,
2054 rmw ? 1 : 0, rmw ? 2 : 1,
2055 CEPH_OSD_OP_WRITE,
2056 CEPH_OSD_FLAG_WRITE,
2057 snapc, ci->i_truncate_seq,
2058 ci->i_truncate_size, false);
2059 if (IS_ERR(req)) {
2060 ret = PTR_ERR(req);
2061 ceph_release_page_vector(pages, num_pages);
2062 break;
2063 }
2064
2065 doutc(cl, "write op %lld~%llu\n", write_pos, write_len);
2066 osd_req_op_extent_osd_data_pages(req, rmw ? 1 : 0, pages, write_len,
2067 offset_in_page(write_pos), false,
2068 true);
2069 req->r_inode = inode;
2070 req->r_mtime = mtime;
2071
2072 /* Set up the assertion */
2073 if (rmw) {
2074 /*
2075 * Set up the assertion. If we don't have a version
2076 * number, then the object doesn't exist yet. Use an
2077 * exclusive create instead of a version assertion in
2078 * that case.
2079 */
2080 if (assert_ver) {
2081 osd_req_op_init(req, 0, CEPH_OSD_OP_ASSERT_VER, 0);
2082 req->r_ops[0].assert_ver.ver = assert_ver;
2083 } else {
2084 osd_req_op_init(req, 0, CEPH_OSD_OP_CREATE,
2085 CEPH_OSD_OP_FLAG_EXCL);
2086 }
2087 }
2088
2089 ceph_osdc_start_request(osdc, req);
2090 ret = ceph_osdc_wait_request(osdc, req);
2091
2092 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
2093 req->r_end_latency, len, ret);
2094 if (ret >= 0 && write_len)
2095 ceph_record_subvolume_io(inode, true,
2096 req->r_start_latency,
2097 req->r_end_latency,
2098 write_len);
2099 ceph_osdc_put_request(req);
2100 if (ret != 0) {
2101 doutc(cl, "osd write returned %d\n", ret);
2102 /* Version changed! Must re-do the rmw cycle */
2103 if ((assert_ver && (ret == -ERANGE || ret == -EOVERFLOW)) ||
2104 (!assert_ver && ret == -EEXIST)) {
2105 /* We should only ever see this on a rmw */
2106 WARN_ON_ONCE(!rmw);
2107
2108 /* The version should never go backward */
2109 WARN_ON_ONCE(ret == -EOVERFLOW);
2110
2111 *from = saved_iter;
2112
2113 /* FIXME: limit number of times we loop? */
2114 continue;
2115 }
2116 ceph_set_error_write(ci);
2117 break;
2118 }
2119
2120 ceph_clear_error_write(ci);
2121
2122 /*
2123 * We successfully wrote to a range of the file. Declare
2124 * that region of the pagecache invalid.
2125 */
2126 ret = invalidate_inode_pages2_range(
2127 inode->i_mapping,
2128 pos >> PAGE_SHIFT,
2129 (pos + len - 1) >> PAGE_SHIFT);
2130 if (ret < 0) {
2131 doutc(cl, "invalidate_inode_pages2_range returned %d\n",
2132 ret);
2133 ret = 0;
2134 }
2135 pos += len;
2136 written += len;
2137 doutc(cl, "written %d\n", written);
2138 if (pos > i_size_read(inode)) {
2139 check_caps = ceph_inode_set_size(inode, pos);
2140 if (check_caps)
2141 ceph_check_caps(ceph_inode(inode),
2142 CHECK_CAPS_AUTHONLY);
2143 }
2144
2145 }
2146
2147 if (ret != -EOLDSNAPC && written > 0) {
2148 ret = written;
2149 iocb->ki_pos = pos;
2150 }
2151 doutc(cl, "returning %d\n", ret);
2152 return ret;
2153 }
2154
2155 /*
2156 * Wrap generic_file_aio_read with checks for cap bits on the inode.
2157 * Atomically grab references, so that those bits are not released
2158 * back to the MDS mid-read.
2159 *
2160 * Hmm, the sync read case isn't actually async... should it be?
2161 */
ceph_read_iter(struct kiocb * iocb,struct iov_iter * to)2162 static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
2163 {
2164 struct file *filp = iocb->ki_filp;
2165 struct ceph_file_info *fi = filp->private_data;
2166 size_t len = iov_iter_count(to);
2167 struct inode *inode = file_inode(filp);
2168 struct ceph_inode_info *ci = ceph_inode(inode);
2169 bool direct_lock = iocb->ki_flags & IOCB_DIRECT;
2170 struct ceph_client *cl = ceph_inode_to_client(inode);
2171 ssize_t ret;
2172 int want = 0, got = 0;
2173 int retry_op = 0, read = 0;
2174
2175 again:
2176 doutc(cl, "%llu~%u trying to get caps on %p %llx.%llx\n",
2177 iocb->ki_pos, (unsigned)len, inode, ceph_vinop(inode));
2178
2179 if (ceph_inode_is_shutdown(inode))
2180 return -ESTALE;
2181
2182 ret = direct_lock ? ceph_start_io_direct(inode) :
2183 ceph_start_io_read(inode);
2184 if (ret)
2185 return ret;
2186
2187 if (!(fi->flags & CEPH_F_SYNC) && !direct_lock)
2188 want |= CEPH_CAP_FILE_CACHE;
2189 if (fi->fmode & CEPH_FILE_MODE_LAZY)
2190 want |= CEPH_CAP_FILE_LAZYIO;
2191
2192 ret = ceph_get_caps(filp, CEPH_CAP_FILE_RD, want, -1, &got);
2193 if (ret < 0) {
2194 if (direct_lock)
2195 ceph_end_io_direct(inode);
2196 else
2197 ceph_end_io_read(inode);
2198 return ret;
2199 }
2200
2201 if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
2202 (iocb->ki_flags & IOCB_DIRECT) ||
2203 (fi->flags & CEPH_F_SYNC)) {
2204
2205 doutc(cl, "sync %p %llx.%llx %llu~%u got cap refs on %s\n",
2206 inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
2207 ceph_cap_string(got));
2208
2209 if (!ceph_has_inline_data(ci)) {
2210 if (!retry_op &&
2211 (iocb->ki_flags & IOCB_DIRECT) &&
2212 !IS_ENCRYPTED(inode)) {
2213 ret = ceph_direct_read_write(iocb, to,
2214 NULL, NULL);
2215 if (ret >= 0 && ret < len)
2216 retry_op = CHECK_EOF;
2217 } else {
2218 ret = ceph_sync_read(iocb, to, &retry_op);
2219 }
2220 } else {
2221 retry_op = READ_INLINE;
2222 }
2223 } else {
2224 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
2225 doutc(cl, "async %p %llx.%llx %llu~%u got cap refs on %s\n",
2226 inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
2227 ceph_cap_string(got));
2228 ceph_add_rw_context(fi, &rw_ctx);
2229 ret = generic_file_read_iter(iocb, to);
2230 ceph_del_rw_context(fi, &rw_ctx);
2231 }
2232
2233 doutc(cl, "%p %llx.%llx dropping cap refs on %s = %d\n",
2234 inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
2235 ceph_put_cap_refs(ci, got);
2236
2237 if (direct_lock)
2238 ceph_end_io_direct(inode);
2239 else
2240 ceph_end_io_read(inode);
2241
2242 if (retry_op > HAVE_RETRIED && ret >= 0) {
2243 int statret;
2244 struct page *page = NULL;
2245 loff_t i_size;
2246 int mask = CEPH_STAT_CAP_SIZE;
2247 if (retry_op == READ_INLINE) {
2248 page = __page_cache_alloc(GFP_KERNEL);
2249 if (!page)
2250 return -ENOMEM;
2251
2252 mask = CEPH_STAT_CAP_INLINE_DATA;
2253 }
2254
2255 statret = __ceph_do_getattr(inode, page, mask, !!page);
2256 if (statret < 0) {
2257 if (page)
2258 __free_page(page);
2259 if (statret == -ENODATA) {
2260 BUG_ON(retry_op != READ_INLINE);
2261 goto again;
2262 }
2263 return statret;
2264 }
2265
2266 i_size = i_size_read(inode);
2267 if (retry_op == READ_INLINE) {
2268 BUG_ON(ret > 0 || read > 0);
2269 if (iocb->ki_pos < i_size &&
2270 iocb->ki_pos < PAGE_SIZE) {
2271 loff_t end = min_t(loff_t, i_size,
2272 iocb->ki_pos + len);
2273 end = min_t(loff_t, end, PAGE_SIZE);
2274 if (statret < end)
2275 zero_user_segment(page, statret, end);
2276 ret = copy_page_to_iter(page,
2277 iocb->ki_pos & ~PAGE_MASK,
2278 end - iocb->ki_pos, to);
2279 iocb->ki_pos += ret;
2280 read += ret;
2281 }
2282 if (iocb->ki_pos < i_size && read < len) {
2283 size_t zlen = min_t(size_t, len - read,
2284 i_size - iocb->ki_pos);
2285 ret = iov_iter_zero(zlen, to);
2286 iocb->ki_pos += ret;
2287 read += ret;
2288 }
2289 __free_pages(page, 0);
2290 return read;
2291 }
2292
2293 /* hit EOF or hole? */
2294 if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
2295 ret < len) {
2296 doutc(cl, "may hit hole, ppos %lld < size %lld, reading more\n",
2297 iocb->ki_pos, i_size);
2298
2299 read += ret;
2300 len -= ret;
2301 retry_op = HAVE_RETRIED;
2302 goto again;
2303 }
2304 }
2305
2306 if (ret >= 0)
2307 ret += read;
2308
2309 return ret;
2310 }
2311
2312 /*
2313 * Wrap filemap_splice_read with checks for cap bits on the inode.
2314 * Atomically grab references, so that those bits are not released
2315 * back to the MDS mid-read.
2316 */
ceph_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)2317 static ssize_t ceph_splice_read(struct file *in, loff_t *ppos,
2318 struct pipe_inode_info *pipe,
2319 size_t len, unsigned int flags)
2320 {
2321 struct ceph_file_info *fi = in->private_data;
2322 struct inode *inode = file_inode(in);
2323 struct ceph_inode_info *ci = ceph_inode(inode);
2324 ssize_t ret;
2325 int want = 0, got = 0;
2326 CEPH_DEFINE_RW_CONTEXT(rw_ctx, 0);
2327
2328 dout("splice_read %p %llx.%llx %llu~%zu trying to get caps on %p\n",
2329 inode, ceph_vinop(inode), *ppos, len, inode);
2330
2331 if (ceph_inode_is_shutdown(inode))
2332 return -ESTALE;
2333
2334 if (ceph_has_inline_data(ci) ||
2335 (fi->flags & CEPH_F_SYNC))
2336 return copy_splice_read(in, ppos, pipe, len, flags);
2337
2338 ret = ceph_start_io_read(inode);
2339 if (ret)
2340 return ret;
2341
2342 want = CEPH_CAP_FILE_CACHE;
2343 if (fi->fmode & CEPH_FILE_MODE_LAZY)
2344 want |= CEPH_CAP_FILE_LAZYIO;
2345
2346 ret = ceph_get_caps(in, CEPH_CAP_FILE_RD, want, -1, &got);
2347 if (ret < 0)
2348 goto out_end;
2349
2350 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) == 0) {
2351 dout("splice_read/sync %p %llx.%llx %llu~%zu got cap refs on %s\n",
2352 inode, ceph_vinop(inode), *ppos, len,
2353 ceph_cap_string(got));
2354
2355 ceph_put_cap_refs(ci, got);
2356 ceph_end_io_read(inode);
2357 return copy_splice_read(in, ppos, pipe, len, flags);
2358 }
2359
2360 dout("splice_read %p %llx.%llx %llu~%zu got cap refs on %s\n",
2361 inode, ceph_vinop(inode), *ppos, len, ceph_cap_string(got));
2362
2363 rw_ctx.caps = got;
2364 ceph_add_rw_context(fi, &rw_ctx);
2365 ret = filemap_splice_read(in, ppos, pipe, len, flags);
2366 ceph_del_rw_context(fi, &rw_ctx);
2367
2368 dout("splice_read %p %llx.%llx dropping cap refs on %s = %zd\n",
2369 inode, ceph_vinop(inode), ceph_cap_string(got), ret);
2370
2371 ceph_put_cap_refs(ci, got);
2372 out_end:
2373 ceph_end_io_read(inode);
2374 return ret;
2375 }
2376
2377 /*
2378 * Take cap references to avoid releasing caps to MDS mid-write.
2379 *
2380 * If we are synchronous, and write with an old snap context, the OSD
2381 * may return EOLDSNAPC. In that case, retry the write.. _after_
2382 * dropping our cap refs and allowing the pending snap to logically
2383 * complete _before_ this write occurs.
2384 *
2385 * If we are near ENOSPC, write synchronously.
2386 */
ceph_write_iter(struct kiocb * iocb,struct iov_iter * from)2387 static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
2388 {
2389 struct file *file = iocb->ki_filp;
2390 struct ceph_file_info *fi = file->private_data;
2391 struct inode *inode = file_inode(file);
2392 struct ceph_inode_info *ci = ceph_inode(inode);
2393 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
2394 struct ceph_client *cl = fsc->client;
2395 struct ceph_osd_client *osdc = &fsc->client->osdc;
2396 struct ceph_cap_flush *prealloc_cf;
2397 ssize_t count, written = 0;
2398 int err, want = 0, got;
2399 bool direct_lock = false;
2400 u32 map_flags;
2401 u64 pool_flags;
2402 loff_t pos;
2403 loff_t limit = max(i_size_read(inode), fsc->max_file_size);
2404
2405 if (ceph_inode_is_shutdown(inode))
2406 return -ESTALE;
2407
2408 if (ceph_snap(inode) != CEPH_NOSNAP)
2409 return -EROFS;
2410
2411 prealloc_cf = ceph_alloc_cap_flush();
2412 if (!prealloc_cf)
2413 return -ENOMEM;
2414
2415 if ((iocb->ki_flags & (IOCB_DIRECT | IOCB_APPEND)) == IOCB_DIRECT)
2416 direct_lock = true;
2417
2418 retry_snap:
2419 err = direct_lock ? ceph_start_io_direct(inode) :
2420 ceph_start_io_write(inode);
2421 if (err)
2422 goto out_unlocked;
2423
2424 if (iocb->ki_flags & IOCB_APPEND) {
2425 err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
2426 if (err < 0)
2427 goto out;
2428 }
2429
2430 err = generic_write_checks(iocb, from);
2431 if (err <= 0)
2432 goto out;
2433
2434 pos = iocb->ki_pos;
2435 if (unlikely(pos >= limit)) {
2436 err = -EFBIG;
2437 goto out;
2438 } else {
2439 iov_iter_truncate(from, limit - pos);
2440 }
2441
2442 count = iov_iter_count(from);
2443 if (ceph_quota_is_max_bytes_exceeded(inode, pos + count)) {
2444 err = -EDQUOT;
2445 goto out;
2446 }
2447
2448 down_read(&osdc->lock);
2449 map_flags = osdc->osdmap->flags;
2450 pool_flags = ceph_pg_pool_flags(osdc->osdmap, ci->i_layout.pool_id);
2451 up_read(&osdc->lock);
2452 if ((map_flags & CEPH_OSDMAP_FULL) ||
2453 (pool_flags & CEPH_POOL_FLAG_FULL)) {
2454 err = -ENOSPC;
2455 goto out;
2456 }
2457
2458 err = file_remove_privs(file);
2459 if (err)
2460 goto out;
2461
2462 doutc(cl, "%p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
2463 inode, ceph_vinop(inode), pos, count,
2464 i_size_read(inode));
2465 if (!(fi->flags & CEPH_F_SYNC) && !direct_lock)
2466 want |= CEPH_CAP_FILE_BUFFER;
2467 if (fi->fmode & CEPH_FILE_MODE_LAZY)
2468 want |= CEPH_CAP_FILE_LAZYIO;
2469 got = 0;
2470 err = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, pos + count, &got);
2471 if (err < 0)
2472 goto out;
2473
2474 err = file_update_time(file);
2475 if (err)
2476 goto out_caps;
2477
2478 inode_inc_iversion_raw(inode);
2479
2480 doutc(cl, "%p %llx.%llx %llu~%zd got cap refs on %s\n",
2481 inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
2482
2483 if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
2484 (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC) ||
2485 (ci->i_ceph_flags & CEPH_I_ERROR_WRITE)) {
2486 struct ceph_snap_context *snapc;
2487 struct iov_iter data;
2488
2489 spin_lock(&ci->i_ceph_lock);
2490 if (__ceph_have_pending_cap_snap(ci)) {
2491 struct ceph_cap_snap *capsnap =
2492 list_last_entry(&ci->i_cap_snaps,
2493 struct ceph_cap_snap,
2494 ci_item);
2495 snapc = ceph_get_snap_context(capsnap->context);
2496 } else {
2497 BUG_ON(!ci->i_head_snapc);
2498 snapc = ceph_get_snap_context(ci->i_head_snapc);
2499 }
2500 spin_unlock(&ci->i_ceph_lock);
2501
2502 /* we might need to revert back to that point */
2503 data = *from;
2504 if ((iocb->ki_flags & IOCB_DIRECT) && !IS_ENCRYPTED(inode))
2505 written = ceph_direct_read_write(iocb, &data, snapc,
2506 &prealloc_cf);
2507 else
2508 written = ceph_sync_write(iocb, &data, pos, snapc);
2509 if (direct_lock)
2510 ceph_end_io_direct(inode);
2511 else
2512 ceph_end_io_write(inode);
2513 if (written > 0)
2514 iov_iter_advance(from, written);
2515 ceph_put_snap_context(snapc);
2516 } else {
2517 /*
2518 * No need to acquire the i_truncate_mutex. Because
2519 * the MDS revokes Fwb caps before sending truncate
2520 * message to us. We can't get Fwb cap while there
2521 * are pending vmtruncate. So write and vmtruncate
2522 * can not run at the same time
2523 */
2524 written = generic_perform_write(iocb, from);
2525 ceph_end_io_write(inode);
2526 }
2527
2528 if (written >= 0) {
2529 int dirty;
2530
2531 spin_lock(&ci->i_ceph_lock);
2532 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
2533 &prealloc_cf);
2534 spin_unlock(&ci->i_ceph_lock);
2535 if (dirty)
2536 __mark_inode_dirty(inode, dirty);
2537 if (ceph_quota_is_max_bytes_approaching(inode, iocb->ki_pos))
2538 ceph_check_caps(ci, CHECK_CAPS_FLUSH);
2539 }
2540
2541 doutc(cl, "%p %llx.%llx %llu~%u dropping cap refs on %s\n",
2542 inode, ceph_vinop(inode), pos, (unsigned)count,
2543 ceph_cap_string(got));
2544 ceph_put_cap_refs(ci, got);
2545
2546 if (written == -EOLDSNAPC) {
2547 doutc(cl, "%p %llx.%llx %llu~%u" "got EOLDSNAPC, retrying\n",
2548 inode, ceph_vinop(inode), pos, (unsigned)count);
2549 goto retry_snap;
2550 }
2551
2552 if (written >= 0) {
2553 if ((map_flags & CEPH_OSDMAP_NEARFULL) ||
2554 (pool_flags & CEPH_POOL_FLAG_NEARFULL))
2555 iocb->ki_flags |= IOCB_DSYNC;
2556 written = generic_write_sync(iocb, written);
2557 }
2558
2559 goto out_unlocked;
2560 out_caps:
2561 ceph_put_cap_refs(ci, got);
2562 out:
2563 if (direct_lock)
2564 ceph_end_io_direct(inode);
2565 else
2566 ceph_end_io_write(inode);
2567 out_unlocked:
2568 ceph_free_cap_flush(prealloc_cf);
2569 return written ? written : err;
2570 }
2571
2572 /*
2573 * llseek. be sure to verify file size on SEEK_END.
2574 */
ceph_llseek(struct file * file,loff_t offset,int whence)2575 static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
2576 {
2577 if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
2578 struct inode *inode = file_inode(file);
2579 int ret;
2580
2581 ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
2582 if (ret < 0)
2583 return ret;
2584 }
2585 return generic_file_llseek(file, offset, whence);
2586 }
2587
ceph_zero_partial_page(struct inode * inode,loff_t offset,size_t size)2588 static inline void ceph_zero_partial_page(struct inode *inode,
2589 loff_t offset, size_t size)
2590 {
2591 struct folio *folio;
2592
2593 folio = filemap_lock_folio(inode->i_mapping, offset >> PAGE_SHIFT);
2594 if (IS_ERR(folio))
2595 return;
2596
2597 folio_wait_writeback(folio);
2598 folio_zero_range(folio, offset_in_folio(folio, offset), size);
2599 folio_unlock(folio);
2600 folio_put(folio);
2601 }
2602
ceph_zero_pagecache_range(struct inode * inode,loff_t offset,loff_t length)2603 static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
2604 loff_t length)
2605 {
2606 loff_t nearly = round_up(offset, PAGE_SIZE);
2607 if (offset < nearly) {
2608 loff_t size = nearly - offset;
2609 if (length < size)
2610 size = length;
2611 ceph_zero_partial_page(inode, offset, size);
2612 offset += size;
2613 length -= size;
2614 }
2615 if (length >= PAGE_SIZE) {
2616 loff_t size = round_down(length, PAGE_SIZE);
2617 truncate_pagecache_range(inode, offset, offset + size - 1);
2618 offset += size;
2619 length -= size;
2620 }
2621 if (length)
2622 ceph_zero_partial_page(inode, offset, length);
2623 }
2624
ceph_zero_partial_object(struct inode * inode,loff_t offset,loff_t * length)2625 static int ceph_zero_partial_object(struct inode *inode,
2626 loff_t offset, loff_t *length)
2627 {
2628 struct ceph_inode_info *ci = ceph_inode(inode);
2629 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
2630 struct ceph_osd_request *req;
2631 struct ceph_snap_context *snapc;
2632 int ret = 0;
2633 loff_t zero = 0;
2634 int op;
2635
2636 if (ceph_inode_is_shutdown(inode))
2637 return -EIO;
2638
2639 if (!length) {
2640 op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
2641 length = &zero;
2642 } else {
2643 op = CEPH_OSD_OP_ZERO;
2644 }
2645
2646 spin_lock(&ci->i_ceph_lock);
2647 if (__ceph_have_pending_cap_snap(ci)) {
2648 struct ceph_cap_snap *capsnap =
2649 list_last_entry(&ci->i_cap_snaps,
2650 struct ceph_cap_snap,
2651 ci_item);
2652 snapc = ceph_get_snap_context(capsnap->context);
2653 } else {
2654 BUG_ON(!ci->i_head_snapc);
2655 snapc = ceph_get_snap_context(ci->i_head_snapc);
2656 }
2657 spin_unlock(&ci->i_ceph_lock);
2658
2659 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
2660 ceph_vino(inode),
2661 offset, length,
2662 0, 1, op,
2663 CEPH_OSD_FLAG_WRITE,
2664 snapc, 0, 0, false);
2665 if (IS_ERR(req)) {
2666 ret = PTR_ERR(req);
2667 goto out;
2668 }
2669
2670 req->r_mtime = inode_get_mtime(inode);
2671 ceph_osdc_start_request(&fsc->client->osdc, req);
2672 ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
2673 if (ret == -ENOENT)
2674 ret = 0;
2675 ceph_osdc_put_request(req);
2676
2677 out:
2678 ceph_put_snap_context(snapc);
2679 return ret;
2680 }
2681
ceph_zero_objects(struct inode * inode,loff_t offset,loff_t length)2682 static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
2683 {
2684 int ret = 0;
2685 struct ceph_inode_info *ci = ceph_inode(inode);
2686 s32 stripe_unit = ci->i_layout.stripe_unit;
2687 s32 stripe_count = ci->i_layout.stripe_count;
2688 s32 object_size = ci->i_layout.object_size;
2689 u64 object_set_size = (u64) object_size * stripe_count;
2690 u64 nearly, t;
2691
2692 /* round offset up to next period boundary */
2693 nearly = offset + object_set_size - 1;
2694 t = nearly;
2695 nearly -= do_div(t, object_set_size);
2696
2697 while (length && offset < nearly) {
2698 loff_t size = length;
2699 ret = ceph_zero_partial_object(inode, offset, &size);
2700 if (ret < 0)
2701 return ret;
2702 offset += size;
2703 length -= size;
2704 }
2705 while (length >= object_set_size) {
2706 int i;
2707 loff_t pos = offset;
2708 for (i = 0; i < stripe_count; ++i) {
2709 ret = ceph_zero_partial_object(inode, pos, NULL);
2710 if (ret < 0)
2711 return ret;
2712 pos += stripe_unit;
2713 }
2714 offset += object_set_size;
2715 length -= object_set_size;
2716 }
2717 while (length) {
2718 loff_t size = length;
2719 ret = ceph_zero_partial_object(inode, offset, &size);
2720 if (ret < 0)
2721 return ret;
2722 offset += size;
2723 length -= size;
2724 }
2725 return ret;
2726 }
2727
ceph_fallocate(struct file * file,int mode,loff_t offset,loff_t length)2728 static long ceph_fallocate(struct file *file, int mode,
2729 loff_t offset, loff_t length)
2730 {
2731 struct ceph_file_info *fi = file->private_data;
2732 struct inode *inode = file_inode(file);
2733 struct ceph_inode_info *ci = ceph_inode(inode);
2734 struct ceph_cap_flush *prealloc_cf;
2735 struct ceph_client *cl = ceph_inode_to_client(inode);
2736 int want, got = 0;
2737 int dirty;
2738 int ret = 0;
2739 loff_t endoff = 0;
2740 loff_t size;
2741
2742 doutc(cl, "%p %llx.%llx mode %x, offset %llu length %llu\n",
2743 inode, ceph_vinop(inode), mode, offset, length);
2744
2745 if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2746 return -EOPNOTSUPP;
2747
2748 if (!S_ISREG(inode->i_mode))
2749 return -EOPNOTSUPP;
2750
2751 if (IS_ENCRYPTED(inode))
2752 return -EOPNOTSUPP;
2753
2754 prealloc_cf = ceph_alloc_cap_flush();
2755 if (!prealloc_cf)
2756 return -ENOMEM;
2757
2758 inode_lock(inode);
2759
2760 if (ceph_snap(inode) != CEPH_NOSNAP) {
2761 ret = -EROFS;
2762 goto unlock;
2763 }
2764
2765 size = i_size_read(inode);
2766
2767 /* Are we punching a hole beyond EOF? */
2768 if (offset >= size)
2769 goto unlock;
2770 if ((offset + length) > size)
2771 length = size - offset;
2772
2773 if (fi->fmode & CEPH_FILE_MODE_LAZY)
2774 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
2775 else
2776 want = CEPH_CAP_FILE_BUFFER;
2777
2778 ret = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, endoff, &got);
2779 if (ret < 0)
2780 goto unlock;
2781
2782 ret = file_modified(file);
2783 if (ret)
2784 goto put_caps;
2785
2786 filemap_invalidate_lock(inode->i_mapping);
2787 ceph_fscache_invalidate(inode, false);
2788 ceph_zero_pagecache_range(inode, offset, length);
2789 ret = ceph_zero_objects(inode, offset, length);
2790
2791 if (!ret) {
2792 spin_lock(&ci->i_ceph_lock);
2793 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
2794 &prealloc_cf);
2795 spin_unlock(&ci->i_ceph_lock);
2796 if (dirty)
2797 __mark_inode_dirty(inode, dirty);
2798 }
2799 filemap_invalidate_unlock(inode->i_mapping);
2800
2801 put_caps:
2802 ceph_put_cap_refs(ci, got);
2803 unlock:
2804 inode_unlock(inode);
2805 ceph_free_cap_flush(prealloc_cf);
2806 return ret;
2807 }
2808
2809 /*
2810 * This function tries to get FILE_WR capabilities for dst_ci and FILE_RD for
2811 * src_ci. Two attempts are made to obtain both caps, and an error is return if
2812 * this fails; zero is returned on success.
2813 */
get_rd_wr_caps(struct file * src_filp,int * src_got,struct file * dst_filp,loff_t dst_endoff,int * dst_got)2814 static int get_rd_wr_caps(struct file *src_filp, int *src_got,
2815 struct file *dst_filp,
2816 loff_t dst_endoff, int *dst_got)
2817 {
2818 int ret = 0;
2819 bool retrying = false;
2820
2821 retry_caps:
2822 ret = ceph_get_caps(dst_filp, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER,
2823 dst_endoff, dst_got);
2824 if (ret < 0)
2825 return ret;
2826
2827 /*
2828 * Since we're already holding the FILE_WR capability for the dst file,
2829 * we would risk a deadlock by using ceph_get_caps. Thus, we'll do some
2830 * retry dance instead to try to get both capabilities.
2831 */
2832 ret = ceph_try_get_caps(file_inode(src_filp),
2833 CEPH_CAP_FILE_RD, CEPH_CAP_FILE_SHARED,
2834 false, src_got);
2835 if (ret <= 0) {
2836 /* Start by dropping dst_ci caps and getting src_ci caps */
2837 ceph_put_cap_refs(ceph_inode(file_inode(dst_filp)), *dst_got);
2838 if (retrying) {
2839 if (!ret)
2840 /* ceph_try_get_caps masks EAGAIN */
2841 ret = -EAGAIN;
2842 return ret;
2843 }
2844 ret = ceph_get_caps(src_filp, CEPH_CAP_FILE_RD,
2845 CEPH_CAP_FILE_SHARED, -1, src_got);
2846 if (ret < 0)
2847 return ret;
2848 /*... drop src_ci caps too, and retry */
2849 ceph_put_cap_refs(ceph_inode(file_inode(src_filp)), *src_got);
2850 retrying = true;
2851 goto retry_caps;
2852 }
2853 return ret;
2854 }
2855
put_rd_wr_caps(struct ceph_inode_info * src_ci,int src_got,struct ceph_inode_info * dst_ci,int dst_got)2856 static void put_rd_wr_caps(struct ceph_inode_info *src_ci, int src_got,
2857 struct ceph_inode_info *dst_ci, int dst_got)
2858 {
2859 ceph_put_cap_refs(src_ci, src_got);
2860 ceph_put_cap_refs(dst_ci, dst_got);
2861 }
2862
2863 /*
2864 * This function does several size-related checks, returning an error if:
2865 * - source file is smaller than off+len
2866 * - destination file size is not OK (inode_newsize_ok())
2867 * - max bytes quotas is exceeded
2868 */
is_file_size_ok(struct inode * src_inode,struct inode * dst_inode,loff_t src_off,loff_t dst_off,size_t len)2869 static int is_file_size_ok(struct inode *src_inode, struct inode *dst_inode,
2870 loff_t src_off, loff_t dst_off, size_t len)
2871 {
2872 struct ceph_client *cl = ceph_inode_to_client(src_inode);
2873 loff_t size, endoff;
2874
2875 size = i_size_read(src_inode);
2876 /*
2877 * Don't copy beyond source file EOF. Instead of simply setting length
2878 * to (size - src_off), just drop to VFS default implementation, as the
2879 * local i_size may be stale due to other clients writing to the source
2880 * inode.
2881 */
2882 if (src_off + len > size) {
2883 doutc(cl, "Copy beyond EOF (%llu + %zu > %llu)\n", src_off,
2884 len, size);
2885 return -EOPNOTSUPP;
2886 }
2887 size = i_size_read(dst_inode);
2888
2889 endoff = dst_off + len;
2890 if (inode_newsize_ok(dst_inode, endoff))
2891 return -EOPNOTSUPP;
2892
2893 if (ceph_quota_is_max_bytes_exceeded(dst_inode, endoff))
2894 return -EDQUOT;
2895
2896 return 0;
2897 }
2898
2899 static struct ceph_osd_request *
ceph_alloc_copyfrom_request(struct ceph_osd_client * osdc,u64 src_snapid,struct ceph_object_id * src_oid,struct ceph_object_locator * src_oloc,struct ceph_object_id * dst_oid,struct ceph_object_locator * dst_oloc,u32 truncate_seq,u64 truncate_size)2900 ceph_alloc_copyfrom_request(struct ceph_osd_client *osdc,
2901 u64 src_snapid,
2902 struct ceph_object_id *src_oid,
2903 struct ceph_object_locator *src_oloc,
2904 struct ceph_object_id *dst_oid,
2905 struct ceph_object_locator *dst_oloc,
2906 u32 truncate_seq, u64 truncate_size)
2907 {
2908 struct ceph_osd_request *req;
2909 int ret;
2910 u32 src_fadvise_flags =
2911 CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
2912 CEPH_OSD_OP_FLAG_FADVISE_NOCACHE;
2913 u32 dst_fadvise_flags =
2914 CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
2915 CEPH_OSD_OP_FLAG_FADVISE_DONTNEED;
2916
2917 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
2918 if (!req)
2919 return ERR_PTR(-ENOMEM);
2920
2921 req->r_flags = CEPH_OSD_FLAG_WRITE;
2922
2923 ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
2924 ceph_oid_copy(&req->r_t.base_oid, dst_oid);
2925
2926 ret = osd_req_op_copy_from_init(req, src_snapid, 0,
2927 src_oid, src_oloc,
2928 src_fadvise_flags,
2929 dst_fadvise_flags,
2930 truncate_seq,
2931 truncate_size,
2932 CEPH_OSD_COPY_FROM_FLAG_TRUNCATE_SEQ);
2933 if (ret)
2934 goto out;
2935
2936 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
2937 if (ret)
2938 goto out;
2939
2940 return req;
2941
2942 out:
2943 ceph_osdc_put_request(req);
2944 return ERR_PTR(ret);
2945 }
2946
ceph_do_objects_copy(struct ceph_inode_info * src_ci,u64 * src_off,struct ceph_inode_info * dst_ci,u64 * dst_off,struct ceph_fs_client * fsc,size_t len,unsigned int flags)2947 static ssize_t ceph_do_objects_copy(struct ceph_inode_info *src_ci, u64 *src_off,
2948 struct ceph_inode_info *dst_ci, u64 *dst_off,
2949 struct ceph_fs_client *fsc,
2950 size_t len, unsigned int flags)
2951 {
2952 struct ceph_object_locator src_oloc, dst_oloc;
2953 struct ceph_object_id src_oid, dst_oid;
2954 struct ceph_osd_client *osdc;
2955 struct ceph_osd_request *req;
2956 ssize_t bytes = 0;
2957 u64 src_objnum, src_objoff, dst_objnum, dst_objoff;
2958 u32 src_objlen, dst_objlen;
2959 u32 object_size = src_ci->i_layout.object_size;
2960 struct ceph_client *cl = fsc->client;
2961 int ret;
2962
2963 src_oloc.pool = src_ci->i_layout.pool_id;
2964 src_oloc.pool_ns = ceph_try_get_string(src_ci->i_layout.pool_ns);
2965 dst_oloc.pool = dst_ci->i_layout.pool_id;
2966 dst_oloc.pool_ns = ceph_try_get_string(dst_ci->i_layout.pool_ns);
2967 osdc = &fsc->client->osdc;
2968
2969 while (len >= object_size) {
2970 ceph_calc_file_object_mapping(&src_ci->i_layout, *src_off,
2971 object_size, &src_objnum,
2972 &src_objoff, &src_objlen);
2973 ceph_calc_file_object_mapping(&dst_ci->i_layout, *dst_off,
2974 object_size, &dst_objnum,
2975 &dst_objoff, &dst_objlen);
2976 ceph_oid_init(&src_oid);
2977 ceph_oid_printf(&src_oid, "%llx.%08llx",
2978 src_ci->i_vino.ino, src_objnum);
2979 ceph_oid_init(&dst_oid);
2980 ceph_oid_printf(&dst_oid, "%llx.%08llx",
2981 dst_ci->i_vino.ino, dst_objnum);
2982 /* Do an object remote copy */
2983 req = ceph_alloc_copyfrom_request(osdc, src_ci->i_vino.snap,
2984 &src_oid, &src_oloc,
2985 &dst_oid, &dst_oloc,
2986 dst_ci->i_truncate_seq,
2987 dst_ci->i_truncate_size);
2988 if (IS_ERR(req))
2989 ret = PTR_ERR(req);
2990 else {
2991 ceph_osdc_start_request(osdc, req);
2992 ret = ceph_osdc_wait_request(osdc, req);
2993 ceph_update_copyfrom_metrics(&fsc->mdsc->metric,
2994 req->r_start_latency,
2995 req->r_end_latency,
2996 object_size, ret);
2997 ceph_osdc_put_request(req);
2998 }
2999 if (ret) {
3000 if (ret == -EOPNOTSUPP) {
3001 fsc->have_copy_from2 = false;
3002 pr_notice_client(cl,
3003 "OSDs don't support copy-from2; disabling copy offload\n");
3004 }
3005 doutc(cl, "returned %d\n", ret);
3006 if (bytes <= 0)
3007 bytes = ret;
3008 goto out;
3009 }
3010 len -= object_size;
3011 bytes += object_size;
3012 *src_off += object_size;
3013 *dst_off += object_size;
3014 }
3015
3016 out:
3017 ceph_oloc_destroy(&src_oloc);
3018 ceph_oloc_destroy(&dst_oloc);
3019 return bytes;
3020 }
3021
__ceph_copy_file_range(struct file * src_file,loff_t src_off,struct file * dst_file,loff_t dst_off,size_t len,unsigned int flags)3022 static ssize_t __ceph_copy_file_range(struct file *src_file, loff_t src_off,
3023 struct file *dst_file, loff_t dst_off,
3024 size_t len, unsigned int flags)
3025 {
3026 struct inode *src_inode = file_inode(src_file);
3027 struct inode *dst_inode = file_inode(dst_file);
3028 struct ceph_inode_info *src_ci = ceph_inode(src_inode);
3029 struct ceph_inode_info *dst_ci = ceph_inode(dst_inode);
3030 struct ceph_cap_flush *prealloc_cf;
3031 struct ceph_fs_client *src_fsc = ceph_inode_to_fs_client(src_inode);
3032 struct ceph_client *cl = src_fsc->client;
3033 loff_t size;
3034 ssize_t ret = -EIO, bytes;
3035 u64 src_objnum, dst_objnum, src_objoff, dst_objoff;
3036 u32 src_objlen, dst_objlen;
3037 int src_got = 0, dst_got = 0, err, dirty;
3038
3039 if (src_inode->i_sb != dst_inode->i_sb) {
3040 struct ceph_fs_client *dst_fsc = ceph_inode_to_fs_client(dst_inode);
3041
3042 if (ceph_fsid_compare(&src_fsc->client->fsid,
3043 &dst_fsc->client->fsid)) {
3044 dout("Copying files across clusters: src: %pU dst: %pU\n",
3045 &src_fsc->client->fsid, &dst_fsc->client->fsid);
3046 return -EXDEV;
3047 }
3048 }
3049 if (ceph_snap(dst_inode) != CEPH_NOSNAP)
3050 return -EROFS;
3051
3052 /*
3053 * Some of the checks below will return -EOPNOTSUPP, which will force a
3054 * fallback to the default VFS copy_file_range implementation. This is
3055 * desirable in several cases (for ex, the 'len' is smaller than the
3056 * size of the objects, or in cases where that would be more
3057 * efficient).
3058 */
3059
3060 if (ceph_test_mount_opt(src_fsc, NOCOPYFROM))
3061 return -EOPNOTSUPP;
3062
3063 if (!src_fsc->have_copy_from2)
3064 return -EOPNOTSUPP;
3065
3066 /*
3067 * Striped file layouts require that we copy partial objects, but the
3068 * OSD copy-from operation only supports full-object copies. Limit
3069 * this to non-striped file layouts for now.
3070 */
3071 if ((src_ci->i_layout.stripe_unit != dst_ci->i_layout.stripe_unit) ||
3072 (src_ci->i_layout.stripe_count != 1) ||
3073 (dst_ci->i_layout.stripe_count != 1) ||
3074 (src_ci->i_layout.object_size != dst_ci->i_layout.object_size)) {
3075 doutc(cl, "Invalid src/dst files layout\n");
3076 return -EOPNOTSUPP;
3077 }
3078
3079 /* Every encrypted inode gets its own key, so we can't offload them */
3080 if (IS_ENCRYPTED(src_inode) || IS_ENCRYPTED(dst_inode))
3081 return -EOPNOTSUPP;
3082
3083 if (len < src_ci->i_layout.object_size)
3084 return -EOPNOTSUPP; /* no remote copy will be done */
3085
3086 prealloc_cf = ceph_alloc_cap_flush();
3087 if (!prealloc_cf)
3088 return -ENOMEM;
3089
3090 /* Start by sync'ing the source and destination files */
3091 ret = file_write_and_wait_range(src_file, src_off, (src_off + len));
3092 if (ret < 0) {
3093 doutc(cl, "failed to write src file (%zd)\n", ret);
3094 goto out;
3095 }
3096 ret = file_write_and_wait_range(dst_file, dst_off, (dst_off + len));
3097 if (ret < 0) {
3098 doutc(cl, "failed to write dst file (%zd)\n", ret);
3099 goto out;
3100 }
3101
3102 /*
3103 * We need FILE_WR caps for dst_ci and FILE_RD for src_ci as other
3104 * clients may have dirty data in their caches. And OSDs know nothing
3105 * about caps, so they can't safely do the remote object copies.
3106 */
3107 err = get_rd_wr_caps(src_file, &src_got,
3108 dst_file, (dst_off + len), &dst_got);
3109 if (err < 0) {
3110 doutc(cl, "get_rd_wr_caps returned %d\n", err);
3111 ret = -EOPNOTSUPP;
3112 goto out;
3113 }
3114
3115 ret = is_file_size_ok(src_inode, dst_inode, src_off, dst_off, len);
3116 if (ret < 0)
3117 goto out_caps;
3118
3119 /* Drop dst file cached pages */
3120 ceph_fscache_invalidate(dst_inode, false);
3121 ret = invalidate_inode_pages2_range(dst_inode->i_mapping,
3122 dst_off >> PAGE_SHIFT,
3123 (dst_off + len) >> PAGE_SHIFT);
3124 if (ret < 0) {
3125 doutc(cl, "Failed to invalidate inode pages (%zd)\n",
3126 ret);
3127 ret = 0; /* XXX */
3128 }
3129 ceph_calc_file_object_mapping(&src_ci->i_layout, src_off,
3130 src_ci->i_layout.object_size,
3131 &src_objnum, &src_objoff, &src_objlen);
3132 ceph_calc_file_object_mapping(&dst_ci->i_layout, dst_off,
3133 dst_ci->i_layout.object_size,
3134 &dst_objnum, &dst_objoff, &dst_objlen);
3135 /* object-level offsets need to the same */
3136 if (src_objoff != dst_objoff) {
3137 ret = -EOPNOTSUPP;
3138 goto out_caps;
3139 }
3140
3141 /*
3142 * Do a manual copy if the object offset isn't object aligned.
3143 * 'src_objlen' contains the bytes left until the end of the object,
3144 * starting at the src_off
3145 */
3146 if (src_objoff) {
3147 doutc(cl, "Initial partial copy of %u bytes\n", src_objlen);
3148
3149 /*
3150 * we need to temporarily drop all caps as we'll be calling
3151 * {read,write}_iter, which will get caps again.
3152 */
3153 put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
3154 ret = splice_file_range(src_file, &src_off, dst_file, &dst_off,
3155 src_objlen);
3156 /* Abort on short copies or on error */
3157 if (ret < (long)src_objlen) {
3158 doutc(cl, "Failed partial copy (%zd)\n", ret);
3159 goto out;
3160 }
3161 len -= ret;
3162 err = get_rd_wr_caps(src_file, &src_got,
3163 dst_file, (dst_off + len), &dst_got);
3164 if (err < 0)
3165 goto out;
3166 err = is_file_size_ok(src_inode, dst_inode,
3167 src_off, dst_off, len);
3168 if (err < 0)
3169 goto out_caps;
3170 }
3171
3172 size = i_size_read(dst_inode);
3173 bytes = ceph_do_objects_copy(src_ci, &src_off, dst_ci, &dst_off,
3174 src_fsc, len, flags);
3175 if (bytes <= 0) {
3176 if (!ret)
3177 ret = bytes;
3178 goto out_caps;
3179 }
3180 doutc(cl, "Copied %zu bytes out of %zu\n", bytes, len);
3181 len -= bytes;
3182 ret += bytes;
3183
3184 file_update_time(dst_file);
3185 inode_inc_iversion_raw(dst_inode);
3186
3187 if (dst_off > size) {
3188 /* Let the MDS know about dst file size change */
3189 if (ceph_inode_set_size(dst_inode, dst_off) ||
3190 ceph_quota_is_max_bytes_approaching(dst_inode, dst_off))
3191 ceph_check_caps(dst_ci, CHECK_CAPS_AUTHONLY | CHECK_CAPS_FLUSH);
3192 }
3193 /* Mark Fw dirty */
3194 spin_lock(&dst_ci->i_ceph_lock);
3195 dirty = __ceph_mark_dirty_caps(dst_ci, CEPH_CAP_FILE_WR, &prealloc_cf);
3196 spin_unlock(&dst_ci->i_ceph_lock);
3197 if (dirty)
3198 __mark_inode_dirty(dst_inode, dirty);
3199
3200 out_caps:
3201 put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
3202
3203 /*
3204 * Do the final manual copy if we still have some bytes left, unless
3205 * there were errors in remote object copies (len >= object_size).
3206 */
3207 if (len && (len < src_ci->i_layout.object_size)) {
3208 doutc(cl, "Final partial copy of %zu bytes\n", len);
3209 bytes = splice_file_range(src_file, &src_off, dst_file,
3210 &dst_off, len);
3211 if (bytes > 0)
3212 ret += bytes;
3213 else
3214 doutc(cl, "Failed partial copy (%zd)\n", bytes);
3215 }
3216
3217 out:
3218 ceph_free_cap_flush(prealloc_cf);
3219
3220 return ret;
3221 }
3222
ceph_copy_file_range(struct file * src_file,loff_t src_off,struct file * dst_file,loff_t dst_off,size_t len,unsigned int flags)3223 static ssize_t ceph_copy_file_range(struct file *src_file, loff_t src_off,
3224 struct file *dst_file, loff_t dst_off,
3225 size_t len, unsigned int flags)
3226 {
3227 ssize_t ret;
3228
3229 ret = __ceph_copy_file_range(src_file, src_off, dst_file, dst_off,
3230 len, flags);
3231
3232 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3233 ret = splice_copy_file_range(src_file, src_off, dst_file,
3234 dst_off, len);
3235 return ret;
3236 }
3237
3238 const struct file_operations ceph_file_fops = {
3239 .open = ceph_open,
3240 .release = ceph_release,
3241 .llseek = ceph_llseek,
3242 .read_iter = ceph_read_iter,
3243 .write_iter = ceph_write_iter,
3244 .mmap_prepare = ceph_mmap_prepare,
3245 .fsync = ceph_fsync,
3246 .lock = ceph_lock,
3247 .flock = ceph_flock,
3248 .splice_read = ceph_splice_read,
3249 .splice_write = iter_file_splice_write,
3250 .unlocked_ioctl = ceph_ioctl,
3251 .compat_ioctl = compat_ptr_ioctl,
3252 .fallocate = ceph_fallocate,
3253 .copy_file_range = ceph_copy_file_range,
3254 };
3255