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 */ 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 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 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 */ 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 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 * 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 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 */ 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 */ 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 */ 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 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 */ 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 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 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 /* Serialized by i_ceph_lock; the two ops touch different bits. */ 602 clear_and_wake_up_bit(CEPH_I_ASYNC_CREATE_BIT, &ci->i_ceph_flags); 603 604 if (test_and_clear_bit(CEPH_I_ASYNC_CHECK_CAPS_BIT, 605 &ci->i_ceph_flags)) 606 check_cap = true; 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 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 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 set_bit(CEPH_I_ASYNC_CREATE_BIT, 770 &ceph_inode(inode)->i_ceph_flags); 771 unlock_new_inode(inode); 772 } 773 if (d_in_lookup(dentry) || d_really_is_negative(dentry)) { 774 if (!d_unhashed(dentry)) 775 d_drop(dentry); 776 dn = d_splice_alias(inode, dentry); 777 WARN_ON_ONCE(dn && dn != dentry); 778 } 779 file->f_mode |= FMODE_CREATED; 780 ret = finish_open(file, dentry, ceph_open); 781 } 782 783 spin_lock(&dentry->d_lock); 784 clear_and_wake_up_bit(CEPH_DENTRY_ASYNC_CREATE_BIT, &di->flags); 785 spin_unlock(&dentry->d_lock); 786 787 return ret; 788 } 789 790 /* 791 * Do a lookup + open with a single request. If we get a non-existent 792 * file or symlink, return 1 so the VFS can retry. 793 */ 794 int ceph_atomic_open(struct inode *dir, struct dentry *dentry, 795 struct file *file, unsigned flags, umode_t mode) 796 { 797 struct mnt_idmap *idmap = file_mnt_idmap(file); 798 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb); 799 struct ceph_client *cl = fsc->client; 800 struct ceph_mds_client *mdsc = fsc->mdsc; 801 struct ceph_mds_request *req; 802 struct inode *new_inode = NULL; 803 struct dentry *dn; 804 struct ceph_acl_sec_ctx as_ctx = {}; 805 bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS); 806 int mask; 807 int err; 808 char *path; 809 810 doutc(cl, "%p %llx.%llx dentry %p '%pd' %s flags %d mode 0%o\n", 811 dir, ceph_vinop(dir), dentry, dentry, 812 d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode); 813 814 if (dentry->d_name.len > NAME_MAX) 815 return -ENAMETOOLONG; 816 817 err = ceph_wait_on_conflict_unlink(dentry); 818 if (err) 819 return err; 820 /* 821 * Do not truncate the file, since atomic_open is called before the 822 * permission check. The caller will do the truncation afterward. 823 */ 824 flags &= ~O_TRUNC; 825 826 dn = d_find_alias(dir); 827 if (!dn) { 828 try_async = false; 829 } else { 830 struct ceph_path_info path_info = {0}; 831 path = ceph_mdsc_build_path(mdsc, dn, &path_info, 0); 832 if (IS_ERR(path)) { 833 try_async = false; 834 err = 0; 835 } else { 836 int fmode = ceph_flags_to_mode(flags); 837 838 mask = MAY_READ; 839 if (fmode & CEPH_FILE_MODE_WR) 840 mask |= MAY_WRITE; 841 err = ceph_mds_check_access(mdsc, path, mask); 842 } 843 ceph_mdsc_free_path_info(&path_info); 844 dput(dn); 845 846 /* For none EACCES cases will let the MDS do the mds auth check */ 847 if (err == -EACCES) { 848 return err; 849 } else if (err < 0) { 850 try_async = false; 851 err = 0; 852 } 853 } 854 855 retry: 856 if (flags & O_CREAT) { 857 if (ceph_quota_is_max_files_exceeded(dir)) 858 return -EDQUOT; 859 860 new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx); 861 if (IS_ERR(new_inode)) { 862 err = PTR_ERR(new_inode); 863 goto out_ctx; 864 } 865 /* Async create can't handle more than a page of xattrs */ 866 if (as_ctx.pagelist && 867 !list_is_singular(&as_ctx.pagelist->head)) 868 try_async = false; 869 } else if (!d_in_lookup(dentry)) { 870 /* If it's not being looked up, it's negative */ 871 return -ENOENT; 872 } 873 874 /* do the open */ 875 req = prepare_open_request(dir->i_sb, flags, mode); 876 if (IS_ERR(req)) { 877 err = PTR_ERR(req); 878 goto out_ctx; 879 } 880 req->r_dentry = dget(dentry); 881 req->r_num_caps = 2; 882 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED; 883 if (ceph_security_xattr_wanted(dir)) 884 mask |= CEPH_CAP_XATTR_SHARED; 885 req->r_args.open.mask = cpu_to_le32(mask); 886 req->r_parent = dir; 887 if (req->r_op == CEPH_MDS_OP_CREATE) 888 req->r_mnt_idmap = mnt_idmap_get(idmap); 889 ihold(dir); 890 if (IS_ENCRYPTED(dir)) { 891 set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags); 892 err = fscrypt_prepare_lookup_partial(dir, dentry); 893 if (err < 0) 894 goto out_req; 895 } 896 897 if (flags & O_CREAT) { 898 struct ceph_file_layout lo; 899 900 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL | 901 CEPH_CAP_XATTR_EXCL; 902 req->r_dentry_unless = CEPH_CAP_FILE_EXCL; 903 904 ceph_as_ctx_to_req(req, &as_ctx); 905 906 if (try_async && (req->r_dir_caps = 907 try_prep_async_create(dir, dentry, &lo, 908 &req->r_deleg_ino))) { 909 struct ceph_vino vino = { .ino = req->r_deleg_ino, 910 .snap = CEPH_NOSNAP }; 911 struct ceph_dentry_info *di = ceph_dentry(dentry); 912 913 set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags); 914 req->r_args.open.flags |= cpu_to_le32(CEPH_O_EXCL); 915 req->r_callback = ceph_async_create_cb; 916 917 /* Hash inode before RPC */ 918 new_inode = ceph_get_inode(dir->i_sb, vino, new_inode); 919 if (IS_ERR(new_inode)) { 920 err = PTR_ERR(new_inode); 921 new_inode = NULL; 922 goto out_req; 923 } 924 WARN_ON_ONCE(!(inode_state_read_once(new_inode) & I_NEW)); 925 926 spin_lock(&dentry->d_lock); 927 di->flags |= CEPH_DENTRY_ASYNC_CREATE; 928 spin_unlock(&dentry->d_lock); 929 930 err = ceph_mdsc_submit_request(mdsc, dir, req); 931 if (!err) { 932 err = ceph_finish_async_create(dir, new_inode, 933 dentry, file, 934 mode, req, 935 &as_ctx, &lo); 936 new_inode = NULL; 937 } else if (err == -EJUKEBOX) { 938 restore_deleg_ino(dir, req->r_deleg_ino); 939 ceph_mdsc_put_request(req); 940 discard_new_inode(new_inode); 941 ceph_release_acl_sec_ctx(&as_ctx); 942 memset(&as_ctx, 0, sizeof(as_ctx)); 943 new_inode = NULL; 944 try_async = false; 945 ceph_put_string(rcu_dereference_raw(lo.pool_ns)); 946 goto retry; 947 } 948 ceph_put_string(rcu_dereference_raw(lo.pool_ns)); 949 goto out_req; 950 } 951 } 952 953 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); 954 req->r_new_inode = new_inode; 955 new_inode = NULL; 956 err = ceph_mdsc_do_request(mdsc, (flags & O_CREAT) ? dir : NULL, req); 957 if (err == -ENOENT) { 958 dentry = ceph_handle_snapdir(req, dentry); 959 if (IS_ERR(dentry)) { 960 err = PTR_ERR(dentry); 961 goto out_req; 962 } 963 err = 0; 964 } 965 966 if (!err && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry) 967 err = ceph_handle_notrace_create(dir, dentry); 968 969 if (d_in_lookup(dentry)) { 970 dn = ceph_finish_lookup(req, dentry, err); 971 if (IS_ERR(dn)) 972 err = PTR_ERR(dn); 973 } else { 974 /* we were given a hashed negative dentry */ 975 dn = NULL; 976 } 977 if (err) 978 goto out_req; 979 if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) { 980 /* make vfs retry on splice, ENOENT, or symlink */ 981 doutc(cl, "finish_no_open on dn %p\n", dn); 982 err = finish_no_open(file, dn); 983 } else { 984 if (IS_ENCRYPTED(dir) && 985 !fscrypt_has_permitted_context(dir, d_inode(dentry))) { 986 pr_warn_client(cl, 987 "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n", 988 ceph_vinop(dir), ceph_vinop(d_inode(dentry))); 989 goto out_req; 990 } 991 992 doutc(cl, "finish_open on dn %p\n", dn); 993 if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) { 994 struct inode *newino = d_inode(dentry); 995 996 cache_file_layout(dir, newino); 997 ceph_init_inode_acls(newino, &as_ctx); 998 file->f_mode |= FMODE_CREATED; 999 } 1000 if ((flags & __O_REGULAR) && !d_is_reg(dentry)) { 1001 err = -EFTYPE; 1002 goto out_req; 1003 } 1004 err = finish_open(file, dentry, ceph_open); 1005 } 1006 out_req: 1007 ceph_mdsc_put_request(req); 1008 iput(new_inode); 1009 out_ctx: 1010 ceph_release_acl_sec_ctx(&as_ctx); 1011 doutc(cl, "result=%d\n", err); 1012 return err; 1013 } 1014 1015 int ceph_release(struct inode *inode, struct file *file) 1016 { 1017 struct ceph_client *cl = ceph_inode_to_client(inode); 1018 struct ceph_inode_info *ci = ceph_inode(inode); 1019 1020 if (S_ISDIR(inode->i_mode)) { 1021 struct ceph_dir_file_info *dfi = file->private_data; 1022 doutc(cl, "%p %llx.%llx dir file %p\n", inode, 1023 ceph_vinop(inode), file); 1024 WARN_ON(!list_empty(&dfi->file_info.rw_contexts)); 1025 1026 ceph_put_fmode(ci, dfi->file_info.fmode, 1); 1027 1028 if (dfi->last_readdir) 1029 ceph_mdsc_put_request(dfi->last_readdir); 1030 kfree(dfi->last_name); 1031 kfree(dfi->dir_info); 1032 kmem_cache_free(ceph_dir_file_cachep, dfi); 1033 } else { 1034 struct ceph_file_info *fi = file->private_data; 1035 doutc(cl, "%p %llx.%llx regular file %p\n", inode, 1036 ceph_vinop(inode), file); 1037 WARN_ON(!list_empty(&fi->rw_contexts)); 1038 1039 ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE); 1040 ceph_put_fmode(ci, fi->fmode, 1); 1041 1042 kmem_cache_free(ceph_file_cachep, fi); 1043 } 1044 1045 /* wake up anyone waiting for caps on this inode */ 1046 wake_up_all(&ci->i_cap_wq); 1047 return 0; 1048 } 1049 1050 enum { 1051 HAVE_RETRIED = 1, 1052 CHECK_EOF = 2, 1053 READ_INLINE = 3, 1054 }; 1055 1056 /* 1057 * Completely synchronous read and write methods. Direct from __user 1058 * buffer to osd, or directly to user pages (if O_DIRECT). 1059 * 1060 * If the read spans object boundary, just do multiple reads. (That's not 1061 * atomic, but good enough for now.) 1062 * 1063 * If we get a short result from the OSD, check against i_size; we need to 1064 * only return a short read to the caller if we hit EOF. 1065 */ 1066 ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, 1067 struct iov_iter *to, int *retry_op, 1068 u64 *last_objver) 1069 { 1070 struct ceph_inode_info *ci = ceph_inode(inode); 1071 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); 1072 struct ceph_client *cl = fsc->client; 1073 struct ceph_osd_client *osdc = &fsc->client->osdc; 1074 ssize_t ret; 1075 u64 off = *ki_pos; 1076 u64 len = iov_iter_count(to); 1077 u64 i_size = i_size_read(inode); 1078 bool sparse = IS_ENCRYPTED(inode) || ceph_test_mount_opt(fsc, SPARSEREAD); 1079 u64 objver = 0; 1080 1081 doutc(cl, "on inode %p %llx.%llx %llx~%llx\n", inode, 1082 ceph_vinop(inode), *ki_pos, len); 1083 1084 if (ceph_inode_is_shutdown(inode)) 1085 return -EIO; 1086 1087 if (!len || !i_size) 1088 return 0; 1089 /* 1090 * flush any page cache pages in this range. this 1091 * will make concurrent normal and sync io slow, 1092 * but it will at least behave sensibly when they are 1093 * in sequence. 1094 */ 1095 ret = filemap_write_and_wait_range(inode->i_mapping, 1096 off, off + len - 1); 1097 if (ret < 0) 1098 return ret; 1099 1100 ret = 0; 1101 while ((len = iov_iter_count(to)) > 0) { 1102 struct ceph_osd_request *req; 1103 struct page **pages; 1104 int num_pages; 1105 size_t page_off; 1106 bool more; 1107 int idx = 0; 1108 size_t left; 1109 struct ceph_osd_req_op *op; 1110 u64 read_off = off; 1111 u64 read_len = len; 1112 int extent_cnt; 1113 1114 /* determine new offset/length if encrypted */ 1115 ceph_fscrypt_adjust_off_and_len(inode, &read_off, &read_len); 1116 1117 doutc(cl, "orig %llu~%llu reading %llu~%llu", off, len, 1118 read_off, read_len); 1119 1120 req = ceph_osdc_new_request(osdc, &ci->i_layout, 1121 ci->i_vino, read_off, &read_len, 0, 1, 1122 sparse ? CEPH_OSD_OP_SPARSE_READ : 1123 CEPH_OSD_OP_READ, 1124 CEPH_OSD_FLAG_READ, 1125 NULL, ci->i_truncate_seq, 1126 ci->i_truncate_size, false); 1127 if (IS_ERR(req)) { 1128 ret = PTR_ERR(req); 1129 break; 1130 } 1131 1132 /* adjust len downward if the request truncated the len */ 1133 if (off + len > read_off + read_len) 1134 len = read_off + read_len - off; 1135 more = len < iov_iter_count(to); 1136 1137 op = &req->r_ops[0]; 1138 if (sparse) { 1139 extent_cnt = __ceph_sparse_read_ext_count(inode, read_len); 1140 ret = ceph_alloc_sparse_ext_map(op, extent_cnt); 1141 if (ret) { 1142 ceph_osdc_put_request(req); 1143 break; 1144 } 1145 } 1146 1147 num_pages = calc_pages_for(read_off, read_len); 1148 page_off = offset_in_page(off); 1149 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); 1150 if (IS_ERR(pages)) { 1151 ceph_osdc_put_request(req); 1152 ret = PTR_ERR(pages); 1153 break; 1154 } 1155 1156 osd_req_op_extent_osd_data_pages(req, 0, pages, read_len, 1157 offset_in_page(read_off), 1158 false, true); 1159 1160 ceph_osdc_start_request(osdc, req); 1161 ret = ceph_osdc_wait_request(osdc, req); 1162 1163 ceph_update_read_metrics(&fsc->mdsc->metric, 1164 req->r_start_latency, 1165 req->r_end_latency, 1166 read_len, ret); 1167 /* 1168 * Only record subvolume metrics for actual bytes read. 1169 * ret == 0 means EOF (no data), not an I/O operation. 1170 */ 1171 if (ret > 0) 1172 ceph_record_subvolume_io(inode, false, 1173 req->r_start_latency, 1174 req->r_end_latency, 1175 ret); 1176 1177 if (ret > 0) 1178 objver = req->r_version; 1179 1180 i_size = i_size_read(inode); 1181 doutc(cl, "%llu~%llu got %zd i_size %llu%s\n", off, len, 1182 ret, i_size, (more ? " MORE" : "")); 1183 1184 /* Fix it to go to end of extent map */ 1185 if (sparse && ret >= 0) 1186 ret = ceph_sparse_ext_map_end(op); 1187 else if (ret == -ENOENT) 1188 ret = 0; 1189 1190 if (ret < 0) { 1191 ceph_osdc_put_request(req); 1192 if (ret == -EBLOCKLISTED) 1193 fsc->blocklisted = true; 1194 break; 1195 } 1196 1197 if (IS_ENCRYPTED(inode)) { 1198 int fret; 1199 1200 fret = ceph_fscrypt_decrypt_extents(inode, pages, 1201 read_off, op->extent.sparse_ext, 1202 op->extent.sparse_ext_cnt); 1203 if (fret < 0) { 1204 ret = fret; 1205 ceph_osdc_put_request(req); 1206 break; 1207 } 1208 1209 /* account for any partial block at the beginning */ 1210 fret -= (off - read_off); 1211 1212 /* 1213 * Short read after big offset adjustment? 1214 * Nothing is usable, just call it a zero 1215 * len read. 1216 */ 1217 fret = max(fret, 0); 1218 1219 /* account for partial block at the end */ 1220 ret = min_t(ssize_t, fret, len); 1221 } 1222 1223 /* Short read but not EOF? Zero out the remainder. */ 1224 if (ret < len && (off + ret < i_size)) { 1225 int zlen = min(len - ret, i_size - off - ret); 1226 int zoff = page_off + ret; 1227 1228 doutc(cl, "zero gap %llu~%llu\n", off + ret, 1229 off + ret + zlen); 1230 ceph_zero_page_vector_range(zoff, zlen, pages); 1231 ret += zlen; 1232 } 1233 1234 if (off + ret > i_size) 1235 left = (i_size > off) ? i_size - off : 0; 1236 else 1237 left = ret; 1238 1239 while (left > 0) { 1240 size_t plen, copied; 1241 1242 plen = min_t(size_t, left, PAGE_SIZE - page_off); 1243 SetPageUptodate(pages[idx]); 1244 copied = copy_page_to_iter(pages[idx++], 1245 page_off, plen, to); 1246 off += copied; 1247 left -= copied; 1248 page_off = 0; 1249 if (copied < plen) { 1250 ret = -EFAULT; 1251 break; 1252 } 1253 } 1254 1255 ceph_osdc_put_request(req); 1256 1257 if (off >= i_size || !more) 1258 break; 1259 } 1260 1261 if (ret > 0) { 1262 if (off >= i_size) { 1263 *retry_op = CHECK_EOF; 1264 ret = i_size - *ki_pos; 1265 *ki_pos = i_size; 1266 } else { 1267 ret = off - *ki_pos; 1268 *ki_pos = off; 1269 } 1270 1271 if (last_objver) 1272 *last_objver = objver; 1273 } 1274 doutc(cl, "result %zd retry_op %d\n", ret, *retry_op); 1275 return ret; 1276 } 1277 1278 static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to, 1279 int *retry_op) 1280 { 1281 struct file *file = iocb->ki_filp; 1282 struct inode *inode = file_inode(file); 1283 struct ceph_client *cl = ceph_inode_to_client(inode); 1284 1285 doutc(cl, "on file %p %llx~%zx %s\n", file, iocb->ki_pos, 1286 iov_iter_count(to), 1287 (file->f_flags & O_DIRECT) ? "O_DIRECT" : ""); 1288 1289 return __ceph_sync_read(inode, &iocb->ki_pos, to, retry_op, NULL); 1290 } 1291 1292 struct ceph_aio_request { 1293 struct kiocb *iocb; 1294 size_t total_len; 1295 bool write; 1296 bool should_dirty; 1297 int error; 1298 struct list_head osd_reqs; 1299 unsigned num_reqs; 1300 atomic_t pending_reqs; 1301 struct timespec64 mtime; 1302 struct ceph_cap_flush *prealloc_cf; 1303 }; 1304 1305 struct ceph_aio_work { 1306 struct work_struct work; 1307 struct ceph_osd_request *req; 1308 }; 1309 1310 static void ceph_aio_retry_work(struct work_struct *work); 1311 1312 static void ceph_aio_complete(struct inode *inode, 1313 struct ceph_aio_request *aio_req) 1314 { 1315 struct ceph_client *cl = ceph_inode_to_client(inode); 1316 struct ceph_inode_info *ci = ceph_inode(inode); 1317 int ret; 1318 1319 if (!atomic_dec_and_test(&aio_req->pending_reqs)) 1320 return; 1321 1322 if (aio_req->iocb->ki_flags & IOCB_DIRECT) 1323 inode_dio_end(inode); 1324 1325 ret = aio_req->error; 1326 if (!ret) 1327 ret = aio_req->total_len; 1328 1329 doutc(cl, "%p %llx.%llx rc %d\n", inode, ceph_vinop(inode), ret); 1330 1331 if (ret >= 0 && aio_req->write) { 1332 int dirty; 1333 1334 loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len; 1335 if (endoff > i_size_read(inode)) { 1336 if (ceph_inode_set_size(inode, endoff)) 1337 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY); 1338 } 1339 1340 spin_lock(&ci->i_ceph_lock); 1341 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, 1342 &aio_req->prealloc_cf); 1343 spin_unlock(&ci->i_ceph_lock); 1344 if (dirty) 1345 __mark_inode_dirty(inode, dirty); 1346 1347 } 1348 1349 ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR : 1350 CEPH_CAP_FILE_RD)); 1351 1352 aio_req->iocb->ki_complete(aio_req->iocb, ret); 1353 1354 ceph_free_cap_flush(aio_req->prealloc_cf); 1355 kfree(aio_req); 1356 } 1357 1358 static void ceph_aio_complete_req(struct ceph_osd_request *req) 1359 { 1360 int rc = req->r_result; 1361 struct inode *inode = req->r_inode; 1362 struct ceph_aio_request *aio_req = req->r_priv; 1363 struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0); 1364 struct ceph_osd_req_op *op = &req->r_ops[0]; 1365 struct ceph_client_metric *metric = &ceph_sb_to_mdsc(inode->i_sb)->metric; 1366 unsigned int len = osd_data->bvec_pos.iter.bi_size; 1367 bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ); 1368 struct ceph_client *cl = ceph_inode_to_client(inode); 1369 1370 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_BVECS); 1371 BUG_ON(!osd_data->num_bvecs); 1372 1373 doutc(cl, "req %p inode %p %llx.%llx, rc %d bytes %u\n", req, 1374 inode, ceph_vinop(inode), rc, len); 1375 1376 if (rc == -EOLDSNAPC) { 1377 struct ceph_aio_work *aio_work; 1378 BUG_ON(!aio_req->write); 1379 1380 aio_work = kmalloc_obj(*aio_work, GFP_NOFS); 1381 if (aio_work) { 1382 INIT_WORK(&aio_work->work, ceph_aio_retry_work); 1383 aio_work->req = req; 1384 queue_work(ceph_inode_to_fs_client(inode)->inode_wq, 1385 &aio_work->work); 1386 return; 1387 } 1388 rc = -ENOMEM; 1389 } else if (!aio_req->write) { 1390 if (sparse && rc >= 0) 1391 rc = ceph_sparse_ext_map_end(op); 1392 if (rc == -ENOENT) 1393 rc = 0; 1394 if (rc >= 0 && len > rc) { 1395 struct iov_iter i; 1396 int zlen = len - rc; 1397 1398 /* 1399 * If read is satisfied by single OSD request, 1400 * it can pass EOF. Otherwise read is within 1401 * i_size. 1402 */ 1403 if (aio_req->num_reqs == 1) { 1404 loff_t i_size = i_size_read(inode); 1405 loff_t endoff = aio_req->iocb->ki_pos + rc; 1406 if (endoff < i_size) 1407 zlen = min_t(size_t, zlen, 1408 i_size - endoff); 1409 aio_req->total_len = rc + zlen; 1410 } 1411 1412 iov_iter_bvec(&i, ITER_DEST, osd_data->bvec_pos.bvecs, 1413 osd_data->num_bvecs, len); 1414 iov_iter_advance(&i, rc); 1415 iov_iter_zero(zlen, &i); 1416 } 1417 } 1418 1419 /* r_start_latency == 0 means the request was not submitted */ 1420 if (req->r_start_latency) { 1421 if (aio_req->write) { 1422 ceph_update_write_metrics(metric, req->r_start_latency, 1423 req->r_end_latency, len, rc); 1424 if (rc >= 0 && len) 1425 ceph_record_subvolume_io(inode, true, 1426 req->r_start_latency, 1427 req->r_end_latency, 1428 len); 1429 } else { 1430 ceph_update_read_metrics(metric, req->r_start_latency, 1431 req->r_end_latency, len, rc); 1432 if (rc > 0) 1433 ceph_record_subvolume_io(inode, false, 1434 req->r_start_latency, 1435 req->r_end_latency, 1436 rc); 1437 } 1438 } 1439 1440 put_bvecs(osd_data->bvec_pos.bvecs, osd_data->num_bvecs, 1441 aio_req->should_dirty); 1442 ceph_osdc_put_request(req); 1443 1444 if (rc < 0) 1445 cmpxchg(&aio_req->error, 0, rc); 1446 1447 ceph_aio_complete(inode, aio_req); 1448 return; 1449 } 1450 1451 static void ceph_aio_retry_work(struct work_struct *work) 1452 { 1453 struct ceph_aio_work *aio_work = 1454 container_of(work, struct ceph_aio_work, work); 1455 struct ceph_osd_request *orig_req = aio_work->req; 1456 struct ceph_aio_request *aio_req = orig_req->r_priv; 1457 struct inode *inode = orig_req->r_inode; 1458 struct ceph_inode_info *ci = ceph_inode(inode); 1459 struct ceph_snap_context *snapc; 1460 struct ceph_osd_request *req; 1461 int ret; 1462 1463 spin_lock(&ci->i_ceph_lock); 1464 if (__ceph_have_pending_cap_snap(ci)) { 1465 struct ceph_cap_snap *capsnap = 1466 list_last_entry(&ci->i_cap_snaps, 1467 struct ceph_cap_snap, 1468 ci_item); 1469 snapc = ceph_get_snap_context(capsnap->context); 1470 } else { 1471 BUG_ON(!ci->i_head_snapc); 1472 snapc = ceph_get_snap_context(ci->i_head_snapc); 1473 } 1474 spin_unlock(&ci->i_ceph_lock); 1475 1476 req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 1, 1477 false, GFP_NOFS); 1478 if (!req) { 1479 ret = -ENOMEM; 1480 req = orig_req; 1481 goto out; 1482 } 1483 1484 req->r_flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE; 1485 ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc); 1486 ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid); 1487 1488 req->r_ops[0] = orig_req->r_ops[0]; 1489 1490 req->r_mtime = aio_req->mtime; 1491 req->r_data_offset = req->r_ops[0].extent.offset; 1492 1493 ret = ceph_osdc_alloc_messages(req, GFP_NOFS); 1494 if (ret) { 1495 ceph_osdc_put_request(req); 1496 req = orig_req; 1497 goto out; 1498 } 1499 1500 ceph_osdc_put_request(orig_req); 1501 1502 req->r_callback = ceph_aio_complete_req; 1503 req->r_inode = inode; 1504 req->r_priv = aio_req; 1505 1506 ceph_osdc_start_request(req->r_osdc, req); 1507 out: 1508 if (ret < 0) { 1509 req->r_result = ret; 1510 ceph_aio_complete_req(req); 1511 } 1512 1513 ceph_put_snap_context(snapc); 1514 kfree(aio_work); 1515 } 1516 1517 static ssize_t 1518 ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, 1519 struct ceph_snap_context *snapc, 1520 struct ceph_cap_flush **pcf) 1521 { 1522 struct file *file = iocb->ki_filp; 1523 struct inode *inode = file_inode(file); 1524 struct ceph_inode_info *ci = ceph_inode(inode); 1525 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); 1526 struct ceph_client *cl = fsc->client; 1527 struct ceph_client_metric *metric = &fsc->mdsc->metric; 1528 struct ceph_vino vino; 1529 struct ceph_osd_request *req; 1530 struct bio_vec *bvecs; 1531 struct ceph_aio_request *aio_req = NULL; 1532 int num_pages = 0; 1533 int flags; 1534 int ret = 0; 1535 struct timespec64 mtime = current_time(inode); 1536 size_t count = iov_iter_count(iter); 1537 loff_t pos = iocb->ki_pos; 1538 bool write = iov_iter_rw(iter) == WRITE; 1539 bool should_dirty = !write && user_backed_iter(iter); 1540 bool sparse = ceph_test_mount_opt(fsc, SPARSEREAD); 1541 1542 if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP) 1543 return -EROFS; 1544 1545 doutc(cl, "sync_direct_%s on file %p %lld~%u snapc %p seq %lld\n", 1546 (write ? "write" : "read"), file, pos, (unsigned)count, 1547 snapc, snapc ? snapc->seq : 0); 1548 1549 if (write) { 1550 int ret2; 1551 1552 ceph_fscache_invalidate(inode, true); 1553 1554 ret2 = invalidate_inode_pages2_range(inode->i_mapping, 1555 pos >> PAGE_SHIFT, 1556 (pos + count - 1) >> PAGE_SHIFT); 1557 if (ret2 < 0) 1558 doutc(cl, "invalidate_inode_pages2_range returned %d\n", 1559 ret2); 1560 1561 flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE; 1562 } else { 1563 flags = CEPH_OSD_FLAG_READ; 1564 } 1565 1566 while (iov_iter_count(iter) > 0) { 1567 u64 size = iov_iter_count(iter); 1568 ssize_t len; 1569 struct ceph_osd_req_op *op; 1570 int readop = sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ; 1571 int extent_cnt; 1572 1573 if (write) 1574 size = min_t(u64, size, fsc->mount_options->wsize); 1575 else 1576 size = min_t(u64, size, fsc->mount_options->rsize); 1577 1578 vino = ceph_vino(inode); 1579 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 1580 vino, pos, &size, 0, 1581 1, 1582 write ? CEPH_OSD_OP_WRITE : readop, 1583 flags, snapc, 1584 ci->i_truncate_seq, 1585 ci->i_truncate_size, 1586 false); 1587 if (IS_ERR(req)) { 1588 ret = PTR_ERR(req); 1589 break; 1590 } 1591 1592 op = &req->r_ops[0]; 1593 if (!write && sparse) { 1594 extent_cnt = __ceph_sparse_read_ext_count(inode, size); 1595 ret = ceph_alloc_sparse_ext_map(op, extent_cnt); 1596 if (ret) { 1597 ceph_osdc_put_request(req); 1598 break; 1599 } 1600 } 1601 1602 len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages); 1603 if (len < 0) { 1604 ceph_osdc_put_request(req); 1605 ret = len; 1606 break; 1607 } 1608 if (len != size) 1609 osd_req_op_extent_update(req, 0, len); 1610 1611 osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len); 1612 1613 /* 1614 * To simplify error handling, allow AIO when IO within i_size 1615 * or IO can be satisfied by single OSD request. 1616 */ 1617 if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) && 1618 (len == count || pos + count <= i_size_read(inode))) { 1619 aio_req = kzalloc_obj(*aio_req); 1620 if (aio_req) { 1621 aio_req->iocb = iocb; 1622 aio_req->write = write; 1623 aio_req->should_dirty = should_dirty; 1624 INIT_LIST_HEAD(&aio_req->osd_reqs); 1625 if (write) { 1626 aio_req->mtime = mtime; 1627 swap(aio_req->prealloc_cf, *pcf); 1628 } 1629 } 1630 /* ignore error */ 1631 } 1632 1633 if (write) { 1634 /* 1635 * throw out any page cache pages in this range. this 1636 * may block. 1637 */ 1638 truncate_inode_pages_range(inode->i_mapping, pos, 1639 PAGE_ALIGN(pos + len) - 1); 1640 1641 req->r_mtime = mtime; 1642 } 1643 1644 if (aio_req) { 1645 aio_req->total_len += len; 1646 aio_req->num_reqs++; 1647 atomic_inc(&aio_req->pending_reqs); 1648 1649 req->r_callback = ceph_aio_complete_req; 1650 req->r_inode = inode; 1651 req->r_priv = aio_req; 1652 list_add_tail(&req->r_private_item, &aio_req->osd_reqs); 1653 1654 pos += len; 1655 continue; 1656 } 1657 1658 ceph_osdc_start_request(req->r_osdc, req); 1659 ret = ceph_osdc_wait_request(&fsc->client->osdc, req); 1660 1661 if (write) { 1662 ceph_update_write_metrics(metric, req->r_start_latency, 1663 req->r_end_latency, len, ret); 1664 if (ret >= 0 && len) 1665 ceph_record_subvolume_io(inode, true, 1666 req->r_start_latency, 1667 req->r_end_latency, 1668 len); 1669 } else { 1670 ceph_update_read_metrics(metric, req->r_start_latency, 1671 req->r_end_latency, len, ret); 1672 if (ret > 0) 1673 ceph_record_subvolume_io(inode, false, 1674 req->r_start_latency, 1675 req->r_end_latency, 1676 ret); 1677 } 1678 1679 size = i_size_read(inode); 1680 if (!write) { 1681 if (sparse && ret >= 0) 1682 ret = ceph_sparse_ext_map_end(op); 1683 else if (ret == -ENOENT) 1684 ret = 0; 1685 1686 if (ret >= 0 && ret < len && pos + ret < size) { 1687 struct iov_iter i; 1688 int zlen = min_t(size_t, len - ret, 1689 size - pos - ret); 1690 1691 iov_iter_bvec(&i, ITER_DEST, bvecs, num_pages, len); 1692 iov_iter_advance(&i, ret); 1693 iov_iter_zero(zlen, &i); 1694 ret += zlen; 1695 } 1696 if (ret >= 0) 1697 len = ret; 1698 } 1699 1700 put_bvecs(bvecs, num_pages, should_dirty); 1701 ceph_osdc_put_request(req); 1702 if (ret < 0) 1703 break; 1704 1705 pos += len; 1706 if (!write && pos >= size) 1707 break; 1708 1709 if (write && pos > size) { 1710 if (ceph_inode_set_size(inode, pos)) 1711 ceph_check_caps(ceph_inode(inode), 1712 CHECK_CAPS_AUTHONLY); 1713 } 1714 } 1715 1716 if (aio_req) { 1717 LIST_HEAD(osd_reqs); 1718 1719 if (aio_req->num_reqs == 0) { 1720 kfree(aio_req); 1721 return ret; 1722 } 1723 1724 ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR : 1725 CEPH_CAP_FILE_RD); 1726 1727 list_splice(&aio_req->osd_reqs, &osd_reqs); 1728 inode_dio_begin(inode); 1729 while (!list_empty(&osd_reqs)) { 1730 req = list_first_entry(&osd_reqs, 1731 struct ceph_osd_request, 1732 r_private_item); 1733 list_del_init(&req->r_private_item); 1734 if (ret >= 0) 1735 ceph_osdc_start_request(req->r_osdc, req); 1736 if (ret < 0) { 1737 req->r_result = ret; 1738 ceph_aio_complete_req(req); 1739 } 1740 } 1741 return -EIOCBQUEUED; 1742 } 1743 1744 if (ret != -EOLDSNAPC && pos > iocb->ki_pos) { 1745 ret = pos - iocb->ki_pos; 1746 iocb->ki_pos = pos; 1747 } 1748 return ret; 1749 } 1750 1751 /* 1752 * Synchronous write, straight from __user pointer or user pages. 1753 * 1754 * If write spans object boundary, just do multiple writes. (For a 1755 * correct atomic write, we should e.g. take write locks on all 1756 * objects, rollback on failure, etc.) 1757 */ 1758 static ssize_t 1759 ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos, 1760 struct ceph_snap_context *snapc) 1761 { 1762 struct file *file = iocb->ki_filp; 1763 struct inode *inode = file_inode(file); 1764 struct ceph_inode_info *ci = ceph_inode(inode); 1765 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); 1766 struct ceph_client *cl = fsc->client; 1767 struct ceph_osd_client *osdc = &fsc->client->osdc; 1768 struct ceph_osd_request *req; 1769 struct page **pages; 1770 u64 len; 1771 int num_pages; 1772 int written = 0; 1773 int ret; 1774 bool check_caps = false; 1775 struct timespec64 mtime = current_time(inode); 1776 size_t count = iov_iter_count(from); 1777 1778 if (ceph_snap(file_inode(file)) != CEPH_NOSNAP) 1779 return -EROFS; 1780 1781 doutc(cl, "on file %p %lld~%u snapc %p seq %lld\n", file, pos, 1782 (unsigned)count, snapc, snapc->seq); 1783 1784 ret = filemap_write_and_wait_range(inode->i_mapping, 1785 pos, pos + count - 1); 1786 if (ret < 0) 1787 return ret; 1788 1789 ceph_fscache_invalidate(inode, false); 1790 1791 while ((len = iov_iter_count(from)) > 0) { 1792 size_t left; 1793 int n; 1794 u64 write_pos = pos; 1795 u64 write_len = len; 1796 u64 objnum, objoff; 1797 u32 xlen; 1798 u64 assert_ver = 0; 1799 bool rmw; 1800 bool first, last; 1801 struct iov_iter saved_iter = *from; 1802 size_t off; 1803 1804 ceph_fscrypt_adjust_off_and_len(inode, &write_pos, &write_len); 1805 1806 /* clamp the length to the end of first object */ 1807 ceph_calc_file_object_mapping(&ci->i_layout, write_pos, 1808 write_len, &objnum, &objoff, 1809 &xlen); 1810 write_len = xlen; 1811 1812 /* adjust len downward if it goes beyond current object */ 1813 if (pos + len > write_pos + write_len) 1814 len = write_pos + write_len - pos; 1815 1816 /* 1817 * If we had to adjust the length or position to align with a 1818 * crypto block, then we must do a read/modify/write cycle. We 1819 * use a version assertion to redrive the thing if something 1820 * changes in between. 1821 */ 1822 first = pos != write_pos; 1823 last = (pos + len) != (write_pos + write_len); 1824 rmw = first || last; 1825 1826 doutc(cl, "ino %llx %lld~%llu adjusted %lld~%llu -- %srmw\n", 1827 ci->i_vino.ino, pos, len, write_pos, write_len, 1828 rmw ? "" : "no "); 1829 1830 /* 1831 * The data is emplaced into the page as it would be if it were 1832 * in an array of pagecache pages. 1833 */ 1834 num_pages = calc_pages_for(write_pos, write_len); 1835 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); 1836 if (IS_ERR(pages)) { 1837 ret = PTR_ERR(pages); 1838 break; 1839 } 1840 1841 /* Do we need to preload the pages? */ 1842 if (rmw) { 1843 u64 first_pos = write_pos; 1844 u64 last_pos = (write_pos + write_len) - CEPH_FSCRYPT_BLOCK_SIZE; 1845 u64 read_len = CEPH_FSCRYPT_BLOCK_SIZE; 1846 struct ceph_osd_req_op *op; 1847 1848 /* We should only need to do this for encrypted inodes */ 1849 WARN_ON_ONCE(!IS_ENCRYPTED(inode)); 1850 1851 /* No need to do two reads if first and last blocks are same */ 1852 if (first && last_pos == first_pos) 1853 last = false; 1854 1855 /* 1856 * Allocate a read request for one or two extents, 1857 * depending on how the request was aligned. 1858 */ 1859 req = ceph_osdc_new_request(osdc, &ci->i_layout, 1860 ci->i_vino, first ? first_pos : last_pos, 1861 &read_len, 0, (first && last) ? 2 : 1, 1862 CEPH_OSD_OP_SPARSE_READ, CEPH_OSD_FLAG_READ, 1863 NULL, ci->i_truncate_seq, 1864 ci->i_truncate_size, false); 1865 if (IS_ERR(req)) { 1866 ceph_release_page_vector(pages, num_pages); 1867 ret = PTR_ERR(req); 1868 break; 1869 } 1870 1871 /* Something is misaligned! */ 1872 if (read_len != CEPH_FSCRYPT_BLOCK_SIZE) { 1873 ceph_osdc_put_request(req); 1874 ceph_release_page_vector(pages, num_pages); 1875 ret = -EIO; 1876 break; 1877 } 1878 1879 /* Add extent for first block? */ 1880 op = &req->r_ops[0]; 1881 1882 if (first) { 1883 osd_req_op_extent_osd_data_pages(req, 0, pages, 1884 CEPH_FSCRYPT_BLOCK_SIZE, 1885 offset_in_page(first_pos), 1886 false, false); 1887 /* We only expect a single extent here */ 1888 ret = __ceph_alloc_sparse_ext_map(op, 1); 1889 if (ret) { 1890 ceph_osdc_put_request(req); 1891 ceph_release_page_vector(pages, num_pages); 1892 break; 1893 } 1894 } 1895 1896 /* Add extent for last block */ 1897 if (last) { 1898 /* Init the other extent if first extent has been used */ 1899 if (first) { 1900 op = &req->r_ops[1]; 1901 osd_req_op_extent_init(req, 1, 1902 CEPH_OSD_OP_SPARSE_READ, 1903 last_pos, CEPH_FSCRYPT_BLOCK_SIZE, 1904 ci->i_truncate_size, 1905 ci->i_truncate_seq); 1906 } 1907 1908 ret = __ceph_alloc_sparse_ext_map(op, 1); 1909 if (ret) { 1910 ceph_osdc_put_request(req); 1911 ceph_release_page_vector(pages, num_pages); 1912 break; 1913 } 1914 1915 osd_req_op_extent_osd_data_pages(req, first ? 1 : 0, 1916 &pages[num_pages - 1], 1917 CEPH_FSCRYPT_BLOCK_SIZE, 1918 offset_in_page(last_pos), 1919 false, false); 1920 } 1921 1922 ceph_osdc_start_request(osdc, req); 1923 ret = ceph_osdc_wait_request(osdc, req); 1924 1925 /* FIXME: length field is wrong if there are 2 extents */ 1926 ceph_update_read_metrics(&fsc->mdsc->metric, 1927 req->r_start_latency, 1928 req->r_end_latency, 1929 read_len, ret); 1930 if (ret > 0) 1931 ceph_record_subvolume_io(inode, false, 1932 req->r_start_latency, 1933 req->r_end_latency, 1934 ret); 1935 1936 /* Ok if object is not already present */ 1937 if (ret == -ENOENT) { 1938 /* 1939 * If there is no object, then we can't assert 1940 * on its version. Set it to 0, and we'll use an 1941 * exclusive create instead. 1942 */ 1943 ceph_osdc_put_request(req); 1944 ret = 0; 1945 1946 /* 1947 * zero out the soon-to-be uncopied parts of the 1948 * first and last pages. 1949 */ 1950 if (first) 1951 zero_user_segment(pages[0], 0, 1952 offset_in_page(first_pos)); 1953 if (last) 1954 zero_user_segment(pages[num_pages - 1], 1955 offset_in_page(last_pos), 1956 PAGE_SIZE); 1957 } else { 1958 if (ret < 0) { 1959 ceph_osdc_put_request(req); 1960 ceph_release_page_vector(pages, num_pages); 1961 break; 1962 } 1963 1964 op = &req->r_ops[0]; 1965 if (op->extent.sparse_ext_cnt == 0) { 1966 if (first) 1967 zero_user_segment(pages[0], 0, 1968 offset_in_page(first_pos)); 1969 else 1970 zero_user_segment(pages[num_pages - 1], 1971 offset_in_page(last_pos), 1972 PAGE_SIZE); 1973 } else if (op->extent.sparse_ext_cnt != 1 || 1974 ceph_sparse_ext_map_end(op) != 1975 CEPH_FSCRYPT_BLOCK_SIZE) { 1976 ret = -EIO; 1977 ceph_osdc_put_request(req); 1978 ceph_release_page_vector(pages, num_pages); 1979 break; 1980 } 1981 1982 if (first && last) { 1983 op = &req->r_ops[1]; 1984 if (op->extent.sparse_ext_cnt == 0) { 1985 zero_user_segment(pages[num_pages - 1], 1986 offset_in_page(last_pos), 1987 PAGE_SIZE); 1988 } else if (op->extent.sparse_ext_cnt != 1 || 1989 ceph_sparse_ext_map_end(op) != 1990 CEPH_FSCRYPT_BLOCK_SIZE) { 1991 ret = -EIO; 1992 ceph_osdc_put_request(req); 1993 ceph_release_page_vector(pages, num_pages); 1994 break; 1995 } 1996 } 1997 1998 /* Grab assert version. It must be non-zero. */ 1999 assert_ver = req->r_version; 2000 WARN_ON_ONCE(ret > 0 && assert_ver == 0); 2001 2002 ceph_osdc_put_request(req); 2003 if (first) { 2004 ret = ceph_fscrypt_decrypt_block_inplace(inode, 2005 pages[0], CEPH_FSCRYPT_BLOCK_SIZE, 2006 offset_in_page(first_pos), 2007 first_pos >> CEPH_FSCRYPT_BLOCK_SHIFT); 2008 if (ret < 0) { 2009 ceph_release_page_vector(pages, num_pages); 2010 break; 2011 } 2012 } 2013 if (last) { 2014 ret = ceph_fscrypt_decrypt_block_inplace(inode, 2015 pages[num_pages - 1], 2016 CEPH_FSCRYPT_BLOCK_SIZE, 2017 offset_in_page(last_pos), 2018 last_pos >> CEPH_FSCRYPT_BLOCK_SHIFT); 2019 if (ret < 0) { 2020 ceph_release_page_vector(pages, num_pages); 2021 break; 2022 } 2023 } 2024 } 2025 } 2026 2027 left = len; 2028 off = offset_in_page(pos); 2029 for (n = 0; n < num_pages; n++) { 2030 size_t plen = min_t(size_t, left, PAGE_SIZE - off); 2031 2032 /* copy the data */ 2033 ret = copy_page_from_iter(pages[n], off, plen, from); 2034 if (ret != plen) { 2035 ret = -EFAULT; 2036 break; 2037 } 2038 off = 0; 2039 left -= ret; 2040 } 2041 if (ret < 0) { 2042 doutc(cl, "write failed with %d\n", ret); 2043 ceph_release_page_vector(pages, num_pages); 2044 break; 2045 } 2046 2047 if (IS_ENCRYPTED(inode)) { 2048 ret = ceph_fscrypt_encrypt_pages(inode, pages, 2049 write_pos, write_len); 2050 if (ret < 0) { 2051 doutc(cl, "encryption failed with %d\n", ret); 2052 ceph_release_page_vector(pages, num_pages); 2053 break; 2054 } 2055 } 2056 2057 req = ceph_osdc_new_request(osdc, &ci->i_layout, 2058 ci->i_vino, write_pos, &write_len, 2059 rmw ? 1 : 0, rmw ? 2 : 1, 2060 CEPH_OSD_OP_WRITE, 2061 CEPH_OSD_FLAG_WRITE, 2062 snapc, ci->i_truncate_seq, 2063 ci->i_truncate_size, false); 2064 if (IS_ERR(req)) { 2065 ret = PTR_ERR(req); 2066 ceph_release_page_vector(pages, num_pages); 2067 break; 2068 } 2069 2070 doutc(cl, "write op %lld~%llu\n", write_pos, write_len); 2071 osd_req_op_extent_osd_data_pages(req, rmw ? 1 : 0, pages, write_len, 2072 offset_in_page(write_pos), false, 2073 true); 2074 req->r_inode = inode; 2075 req->r_mtime = mtime; 2076 2077 /* Set up the assertion */ 2078 if (rmw) { 2079 /* 2080 * Set up the assertion. If we don't have a version 2081 * number, then the object doesn't exist yet. Use an 2082 * exclusive create instead of a version assertion in 2083 * that case. 2084 */ 2085 if (assert_ver) { 2086 osd_req_op_init(req, 0, CEPH_OSD_OP_ASSERT_VER, 0); 2087 req->r_ops[0].assert_ver.ver = assert_ver; 2088 } else { 2089 osd_req_op_init(req, 0, CEPH_OSD_OP_CREATE, 2090 CEPH_OSD_OP_FLAG_EXCL); 2091 } 2092 } 2093 2094 ceph_osdc_start_request(osdc, req); 2095 ret = ceph_osdc_wait_request(osdc, req); 2096 2097 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency, 2098 req->r_end_latency, len, ret); 2099 if (ret >= 0 && write_len) 2100 ceph_record_subvolume_io(inode, true, 2101 req->r_start_latency, 2102 req->r_end_latency, 2103 write_len); 2104 ceph_osdc_put_request(req); 2105 if (ret != 0) { 2106 doutc(cl, "osd write returned %d\n", ret); 2107 /* Version changed! Must re-do the rmw cycle */ 2108 if ((assert_ver && (ret == -ERANGE || ret == -EOVERFLOW)) || 2109 (!assert_ver && ret == -EEXIST)) { 2110 /* We should only ever see this on a rmw */ 2111 WARN_ON_ONCE(!rmw); 2112 2113 /* The version should never go backward */ 2114 WARN_ON_ONCE(ret == -EOVERFLOW); 2115 2116 *from = saved_iter; 2117 2118 /* FIXME: limit number of times we loop? */ 2119 continue; 2120 } 2121 ceph_set_error_write(ci); 2122 break; 2123 } 2124 2125 ceph_clear_error_write(ci); 2126 2127 /* 2128 * We successfully wrote to a range of the file. Declare 2129 * that region of the pagecache invalid. 2130 */ 2131 ret = invalidate_inode_pages2_range( 2132 inode->i_mapping, 2133 pos >> PAGE_SHIFT, 2134 (pos + len - 1) >> PAGE_SHIFT); 2135 if (ret < 0) { 2136 doutc(cl, "invalidate_inode_pages2_range returned %d\n", 2137 ret); 2138 ret = 0; 2139 } 2140 pos += len; 2141 written += len; 2142 doutc(cl, "written %d\n", written); 2143 if (pos > i_size_read(inode)) { 2144 check_caps = ceph_inode_set_size(inode, pos); 2145 if (check_caps) 2146 ceph_check_caps(ceph_inode(inode), 2147 CHECK_CAPS_AUTHONLY); 2148 } 2149 2150 } 2151 2152 if (ret != -EOLDSNAPC && written > 0) { 2153 ret = written; 2154 iocb->ki_pos = pos; 2155 } 2156 doutc(cl, "returning %d\n", ret); 2157 return ret; 2158 } 2159 2160 /* 2161 * Wrap generic_file_aio_read with checks for cap bits on the inode. 2162 * Atomically grab references, so that those bits are not released 2163 * back to the MDS mid-read. 2164 * 2165 * Hmm, the sync read case isn't actually async... should it be? 2166 */ 2167 static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to) 2168 { 2169 struct file *filp = iocb->ki_filp; 2170 struct ceph_file_info *fi = filp->private_data; 2171 size_t len = iov_iter_count(to); 2172 struct inode *inode = file_inode(filp); 2173 struct ceph_inode_info *ci = ceph_inode(inode); 2174 bool direct_lock = iocb->ki_flags & IOCB_DIRECT; 2175 struct ceph_client *cl = ceph_inode_to_client(inode); 2176 ssize_t ret; 2177 int want = 0, got = 0; 2178 int retry_op = 0, read = 0; 2179 2180 again: 2181 doutc(cl, "%llu~%u trying to get caps on %p %llx.%llx\n", 2182 iocb->ki_pos, (unsigned)len, inode, ceph_vinop(inode)); 2183 2184 if (ceph_inode_is_shutdown(inode)) 2185 return -ESTALE; 2186 2187 ret = direct_lock ? ceph_start_io_direct(inode) : 2188 ceph_start_io_read(inode); 2189 if (ret) 2190 return ret; 2191 2192 if (!(fi->flags & CEPH_F_SYNC) && !direct_lock) 2193 want |= CEPH_CAP_FILE_CACHE; 2194 if (fi->fmode & CEPH_FILE_MODE_LAZY) 2195 want |= CEPH_CAP_FILE_LAZYIO; 2196 2197 ret = ceph_get_caps(filp, CEPH_CAP_FILE_RD, want, -1, &got); 2198 if (ret < 0) { 2199 if (direct_lock) 2200 ceph_end_io_direct(inode); 2201 else 2202 ceph_end_io_read(inode); 2203 return ret; 2204 } 2205 2206 if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 || 2207 (iocb->ki_flags & IOCB_DIRECT) || 2208 (fi->flags & CEPH_F_SYNC)) { 2209 2210 doutc(cl, "sync %p %llx.%llx %llu~%u got cap refs on %s\n", 2211 inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, 2212 ceph_cap_string(got)); 2213 2214 if (!ceph_has_inline_data(ci)) { 2215 if (!retry_op && 2216 (iocb->ki_flags & IOCB_DIRECT) && 2217 !IS_ENCRYPTED(inode)) { 2218 ret = ceph_direct_read_write(iocb, to, 2219 NULL, NULL); 2220 if (ret >= 0 && ret < len) 2221 retry_op = CHECK_EOF; 2222 } else { 2223 ret = ceph_sync_read(iocb, to, &retry_op); 2224 } 2225 } else { 2226 retry_op = READ_INLINE; 2227 } 2228 } else { 2229 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got); 2230 doutc(cl, "async %p %llx.%llx %llu~%u got cap refs on %s\n", 2231 inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, 2232 ceph_cap_string(got)); 2233 ceph_add_rw_context(fi, &rw_ctx); 2234 ret = generic_file_read_iter(iocb, to); 2235 ceph_del_rw_context(fi, &rw_ctx); 2236 } 2237 2238 doutc(cl, "%p %llx.%llx dropping cap refs on %s = %d\n", 2239 inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret); 2240 ceph_put_cap_refs(ci, got); 2241 2242 if (direct_lock) 2243 ceph_end_io_direct(inode); 2244 else 2245 ceph_end_io_read(inode); 2246 2247 if (retry_op > HAVE_RETRIED && ret >= 0) { 2248 int statret; 2249 struct page *page = NULL; 2250 loff_t i_size; 2251 int mask = CEPH_STAT_CAP_SIZE; 2252 if (retry_op == READ_INLINE) { 2253 page = __page_cache_alloc(GFP_KERNEL); 2254 if (!page) 2255 return -ENOMEM; 2256 2257 mask = CEPH_STAT_CAP_INLINE_DATA; 2258 } 2259 2260 statret = __ceph_do_getattr(inode, page, mask, !!page); 2261 if (statret < 0) { 2262 if (page) 2263 __free_page(page); 2264 if (statret == -ENODATA) { 2265 BUG_ON(retry_op != READ_INLINE); 2266 goto again; 2267 } 2268 return statret; 2269 } 2270 2271 i_size = i_size_read(inode); 2272 if (retry_op == READ_INLINE) { 2273 BUG_ON(ret > 0 || read > 0); 2274 if (iocb->ki_pos < i_size && 2275 iocb->ki_pos < PAGE_SIZE) { 2276 loff_t end = min_t(loff_t, i_size, 2277 iocb->ki_pos + len); 2278 end = min_t(loff_t, end, PAGE_SIZE); 2279 if (statret < end) 2280 zero_user_segment(page, statret, end); 2281 ret = copy_page_to_iter(page, 2282 iocb->ki_pos & ~PAGE_MASK, 2283 end - iocb->ki_pos, to); 2284 iocb->ki_pos += ret; 2285 read += ret; 2286 } 2287 if (iocb->ki_pos < i_size && read < len) { 2288 size_t zlen = min_t(size_t, len - read, 2289 i_size - iocb->ki_pos); 2290 ret = iov_iter_zero(zlen, to); 2291 iocb->ki_pos += ret; 2292 read += ret; 2293 } 2294 __free_pages(page, 0); 2295 return read; 2296 } 2297 2298 /* hit EOF or hole? */ 2299 if (retry_op == CHECK_EOF && iocb->ki_pos < i_size && 2300 ret < len) { 2301 doutc(cl, "may hit hole, ppos %lld < size %lld, reading more\n", 2302 iocb->ki_pos, i_size); 2303 2304 read += ret; 2305 len -= ret; 2306 retry_op = HAVE_RETRIED; 2307 goto again; 2308 } 2309 } 2310 2311 if (ret >= 0) 2312 ret += read; 2313 2314 return ret; 2315 } 2316 2317 /* 2318 * Wrap filemap_splice_read with checks for cap bits on the inode. 2319 * Atomically grab references, so that those bits are not released 2320 * back to the MDS mid-read. 2321 */ 2322 static ssize_t ceph_splice_read(struct file *in, loff_t *ppos, 2323 struct pipe_inode_info *pipe, 2324 size_t len, unsigned int flags) 2325 { 2326 struct ceph_file_info *fi = in->private_data; 2327 struct inode *inode = file_inode(in); 2328 struct ceph_inode_info *ci = ceph_inode(inode); 2329 ssize_t ret; 2330 int want = 0, got = 0; 2331 CEPH_DEFINE_RW_CONTEXT(rw_ctx, 0); 2332 2333 dout("splice_read %p %llx.%llx %llu~%zu trying to get caps on %p\n", 2334 inode, ceph_vinop(inode), *ppos, len, inode); 2335 2336 if (ceph_inode_is_shutdown(inode)) 2337 return -ESTALE; 2338 2339 if (ceph_has_inline_data(ci) || 2340 (fi->flags & CEPH_F_SYNC)) 2341 return copy_splice_read(in, ppos, pipe, len, flags); 2342 2343 ret = ceph_start_io_read(inode); 2344 if (ret) 2345 return ret; 2346 2347 want = CEPH_CAP_FILE_CACHE; 2348 if (fi->fmode & CEPH_FILE_MODE_LAZY) 2349 want |= CEPH_CAP_FILE_LAZYIO; 2350 2351 ret = ceph_get_caps(in, CEPH_CAP_FILE_RD, want, -1, &got); 2352 if (ret < 0) 2353 goto out_end; 2354 2355 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) == 0) { 2356 dout("splice_read/sync %p %llx.%llx %llu~%zu got cap refs on %s\n", 2357 inode, ceph_vinop(inode), *ppos, len, 2358 ceph_cap_string(got)); 2359 2360 ceph_put_cap_refs(ci, got); 2361 ceph_end_io_read(inode); 2362 return copy_splice_read(in, ppos, pipe, len, flags); 2363 } 2364 2365 dout("splice_read %p %llx.%llx %llu~%zu got cap refs on %s\n", 2366 inode, ceph_vinop(inode), *ppos, len, ceph_cap_string(got)); 2367 2368 rw_ctx.caps = got; 2369 ceph_add_rw_context(fi, &rw_ctx); 2370 ret = filemap_splice_read(in, ppos, pipe, len, flags); 2371 ceph_del_rw_context(fi, &rw_ctx); 2372 2373 dout("splice_read %p %llx.%llx dropping cap refs on %s = %zd\n", 2374 inode, ceph_vinop(inode), ceph_cap_string(got), ret); 2375 2376 ceph_put_cap_refs(ci, got); 2377 out_end: 2378 ceph_end_io_read(inode); 2379 return ret; 2380 } 2381 2382 /* 2383 * Take cap references to avoid releasing caps to MDS mid-write. 2384 * 2385 * If we are synchronous, and write with an old snap context, the OSD 2386 * may return EOLDSNAPC. In that case, retry the write.. _after_ 2387 * dropping our cap refs and allowing the pending snap to logically 2388 * complete _before_ this write occurs. 2389 * 2390 * If we are near ENOSPC, write synchronously. 2391 */ 2392 static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from) 2393 { 2394 struct file *file = iocb->ki_filp; 2395 struct ceph_file_info *fi = file->private_data; 2396 struct inode *inode = file_inode(file); 2397 struct ceph_inode_info *ci = ceph_inode(inode); 2398 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); 2399 struct ceph_client *cl = fsc->client; 2400 struct ceph_osd_client *osdc = &fsc->client->osdc; 2401 struct ceph_cap_flush *prealloc_cf; 2402 ssize_t count, written = 0; 2403 int err, want = 0, got; 2404 bool direct_lock = false; 2405 u32 map_flags; 2406 u64 pool_flags; 2407 loff_t pos; 2408 loff_t limit = max(i_size_read(inode), fsc->max_file_size); 2409 2410 if (ceph_inode_is_shutdown(inode)) 2411 return -ESTALE; 2412 2413 if (ceph_snap(inode) != CEPH_NOSNAP) 2414 return -EROFS; 2415 2416 prealloc_cf = ceph_alloc_cap_flush(); 2417 if (!prealloc_cf) 2418 return -ENOMEM; 2419 2420 if ((iocb->ki_flags & (IOCB_DIRECT | IOCB_APPEND)) == IOCB_DIRECT) 2421 direct_lock = true; 2422 2423 retry_snap: 2424 err = direct_lock ? ceph_start_io_direct(inode) : 2425 ceph_start_io_write(inode); 2426 if (err) 2427 goto out_unlocked; 2428 2429 if (iocb->ki_flags & IOCB_APPEND) { 2430 err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false); 2431 if (err < 0) 2432 goto out; 2433 } 2434 2435 err = generic_write_checks(iocb, from); 2436 if (err <= 0) 2437 goto out; 2438 2439 pos = iocb->ki_pos; 2440 if (unlikely(pos >= limit)) { 2441 err = -EFBIG; 2442 goto out; 2443 } else { 2444 iov_iter_truncate(from, limit - pos); 2445 } 2446 2447 count = iov_iter_count(from); 2448 if (ceph_quota_is_max_bytes_exceeded(inode, pos + count)) { 2449 err = -EDQUOT; 2450 goto out; 2451 } 2452 2453 down_read(&osdc->lock); 2454 map_flags = osdc->osdmap->flags; 2455 pool_flags = ceph_pg_pool_flags(osdc->osdmap, ci->i_layout.pool_id); 2456 up_read(&osdc->lock); 2457 if ((map_flags & CEPH_OSDMAP_FULL) || 2458 (pool_flags & CEPH_POOL_FLAG_FULL)) { 2459 err = -ENOSPC; 2460 goto out; 2461 } 2462 2463 err = file_remove_privs(file); 2464 if (err) 2465 goto out; 2466 2467 doutc(cl, "%p %llx.%llx %llu~%zd getting caps. i_size %llu\n", 2468 inode, ceph_vinop(inode), pos, count, 2469 i_size_read(inode)); 2470 if (!(fi->flags & CEPH_F_SYNC) && !direct_lock) 2471 want |= CEPH_CAP_FILE_BUFFER; 2472 if (fi->fmode & CEPH_FILE_MODE_LAZY) 2473 want |= CEPH_CAP_FILE_LAZYIO; 2474 got = 0; 2475 err = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, pos + count, &got); 2476 if (err < 0) 2477 goto out; 2478 2479 err = file_update_time(file); 2480 if (err) 2481 goto out_caps; 2482 2483 inode_inc_iversion_raw(inode); 2484 2485 doutc(cl, "%p %llx.%llx %llu~%zd got cap refs on %s\n", 2486 inode, ceph_vinop(inode), pos, count, ceph_cap_string(got)); 2487 2488 if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 || 2489 (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC) || 2490 test_bit(CEPH_I_ERROR_WRITE_BIT, &ci->i_ceph_flags)) { 2491 struct ceph_snap_context *snapc; 2492 struct iov_iter data; 2493 2494 spin_lock(&ci->i_ceph_lock); 2495 if (__ceph_have_pending_cap_snap(ci)) { 2496 struct ceph_cap_snap *capsnap = 2497 list_last_entry(&ci->i_cap_snaps, 2498 struct ceph_cap_snap, 2499 ci_item); 2500 snapc = ceph_get_snap_context(capsnap->context); 2501 } else { 2502 BUG_ON(!ci->i_head_snapc); 2503 snapc = ceph_get_snap_context(ci->i_head_snapc); 2504 } 2505 spin_unlock(&ci->i_ceph_lock); 2506 2507 /* we might need to revert back to that point */ 2508 data = *from; 2509 if ((iocb->ki_flags & IOCB_DIRECT) && !IS_ENCRYPTED(inode)) 2510 written = ceph_direct_read_write(iocb, &data, snapc, 2511 &prealloc_cf); 2512 else 2513 written = ceph_sync_write(iocb, &data, pos, snapc); 2514 if (direct_lock) 2515 ceph_end_io_direct(inode); 2516 else 2517 ceph_end_io_write(inode); 2518 if (written > 0) 2519 iov_iter_advance(from, written); 2520 ceph_put_snap_context(snapc); 2521 } else { 2522 /* 2523 * No need to acquire the i_truncate_mutex. Because 2524 * the MDS revokes Fwb caps before sending truncate 2525 * message to us. We can't get Fwb cap while there 2526 * are pending vmtruncate. So write and vmtruncate 2527 * can not run at the same time 2528 */ 2529 written = generic_perform_write(iocb, from); 2530 ceph_end_io_write(inode); 2531 } 2532 2533 if (written >= 0) { 2534 int dirty; 2535 2536 spin_lock(&ci->i_ceph_lock); 2537 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, 2538 &prealloc_cf); 2539 spin_unlock(&ci->i_ceph_lock); 2540 if (dirty) 2541 __mark_inode_dirty(inode, dirty); 2542 if (ceph_quota_is_max_bytes_approaching(inode, iocb->ki_pos)) 2543 ceph_check_caps(ci, CHECK_CAPS_FLUSH); 2544 } 2545 2546 doutc(cl, "%p %llx.%llx %llu~%u dropping cap refs on %s\n", 2547 inode, ceph_vinop(inode), pos, (unsigned)count, 2548 ceph_cap_string(got)); 2549 ceph_put_cap_refs(ci, got); 2550 2551 if (written == -EOLDSNAPC) { 2552 doutc(cl, "%p %llx.%llx %llu~%u" "got EOLDSNAPC, retrying\n", 2553 inode, ceph_vinop(inode), pos, (unsigned)count); 2554 goto retry_snap; 2555 } 2556 2557 if (written >= 0) { 2558 if ((map_flags & CEPH_OSDMAP_NEARFULL) || 2559 (pool_flags & CEPH_POOL_FLAG_NEARFULL)) 2560 iocb->ki_flags |= IOCB_DSYNC; 2561 written = generic_write_sync(iocb, written); 2562 } 2563 2564 goto out_unlocked; 2565 out_caps: 2566 ceph_put_cap_refs(ci, got); 2567 out: 2568 if (direct_lock) 2569 ceph_end_io_direct(inode); 2570 else 2571 ceph_end_io_write(inode); 2572 out_unlocked: 2573 ceph_free_cap_flush(prealloc_cf); 2574 return written ? written : err; 2575 } 2576 2577 /* 2578 * llseek. be sure to verify file size on SEEK_END. 2579 */ 2580 static loff_t ceph_llseek(struct file *file, loff_t offset, int whence) 2581 { 2582 if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) { 2583 struct inode *inode = file_inode(file); 2584 int ret; 2585 2586 ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false); 2587 if (ret < 0) 2588 return ret; 2589 } 2590 return generic_file_llseek(file, offset, whence); 2591 } 2592 2593 static inline void ceph_zero_partial_page(struct inode *inode, 2594 loff_t offset, size_t size) 2595 { 2596 struct folio *folio; 2597 2598 folio = filemap_lock_folio(inode->i_mapping, offset >> PAGE_SHIFT); 2599 if (IS_ERR(folio)) 2600 return; 2601 2602 folio_wait_writeback(folio); 2603 folio_zero_range(folio, offset_in_folio(folio, offset), size); 2604 folio_unlock(folio); 2605 folio_put(folio); 2606 } 2607 2608 static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset, 2609 loff_t length) 2610 { 2611 loff_t nearly = round_up(offset, PAGE_SIZE); 2612 if (offset < nearly) { 2613 loff_t size = nearly - offset; 2614 if (length < size) 2615 size = length; 2616 ceph_zero_partial_page(inode, offset, size); 2617 offset += size; 2618 length -= size; 2619 } 2620 if (length >= PAGE_SIZE) { 2621 loff_t size = round_down(length, PAGE_SIZE); 2622 truncate_pagecache_range(inode, offset, offset + size - 1); 2623 offset += size; 2624 length -= size; 2625 } 2626 if (length) 2627 ceph_zero_partial_page(inode, offset, length); 2628 } 2629 2630 static int ceph_zero_partial_object(struct inode *inode, 2631 loff_t offset, loff_t *length) 2632 { 2633 struct ceph_inode_info *ci = ceph_inode(inode); 2634 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); 2635 struct ceph_osd_request *req; 2636 struct ceph_snap_context *snapc; 2637 int ret = 0; 2638 loff_t zero = 0; 2639 int op; 2640 2641 if (ceph_inode_is_shutdown(inode)) 2642 return -EIO; 2643 2644 if (!length) { 2645 op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE; 2646 length = &zero; 2647 } else { 2648 op = CEPH_OSD_OP_ZERO; 2649 } 2650 2651 spin_lock(&ci->i_ceph_lock); 2652 if (__ceph_have_pending_cap_snap(ci)) { 2653 struct ceph_cap_snap *capsnap = 2654 list_last_entry(&ci->i_cap_snaps, 2655 struct ceph_cap_snap, 2656 ci_item); 2657 snapc = ceph_get_snap_context(capsnap->context); 2658 } else { 2659 BUG_ON(!ci->i_head_snapc); 2660 snapc = ceph_get_snap_context(ci->i_head_snapc); 2661 } 2662 spin_unlock(&ci->i_ceph_lock); 2663 2664 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, 2665 ceph_vino(inode), 2666 offset, length, 2667 0, 1, op, 2668 CEPH_OSD_FLAG_WRITE, 2669 snapc, 0, 0, false); 2670 if (IS_ERR(req)) { 2671 ret = PTR_ERR(req); 2672 goto out; 2673 } 2674 2675 req->r_mtime = inode_get_mtime(inode); 2676 ceph_osdc_start_request(&fsc->client->osdc, req); 2677 ret = ceph_osdc_wait_request(&fsc->client->osdc, req); 2678 if (ret == -ENOENT) 2679 ret = 0; 2680 ceph_osdc_put_request(req); 2681 2682 out: 2683 ceph_put_snap_context(snapc); 2684 return ret; 2685 } 2686 2687 static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length) 2688 { 2689 int ret = 0; 2690 struct ceph_inode_info *ci = ceph_inode(inode); 2691 s32 stripe_unit = ci->i_layout.stripe_unit; 2692 s32 stripe_count = ci->i_layout.stripe_count; 2693 s32 object_size = ci->i_layout.object_size; 2694 u64 object_set_size = (u64) object_size * stripe_count; 2695 u64 nearly, t; 2696 2697 /* round offset up to next period boundary */ 2698 nearly = offset + object_set_size - 1; 2699 t = nearly; 2700 nearly -= do_div(t, object_set_size); 2701 2702 while (length && offset < nearly) { 2703 loff_t size = length; 2704 ret = ceph_zero_partial_object(inode, offset, &size); 2705 if (ret < 0) 2706 return ret; 2707 offset += size; 2708 length -= size; 2709 } 2710 while (length >= object_set_size) { 2711 int i; 2712 loff_t pos = offset; 2713 for (i = 0; i < stripe_count; ++i) { 2714 ret = ceph_zero_partial_object(inode, pos, NULL); 2715 if (ret < 0) 2716 return ret; 2717 pos += stripe_unit; 2718 } 2719 offset += object_set_size; 2720 length -= object_set_size; 2721 } 2722 while (length) { 2723 loff_t size = length; 2724 ret = ceph_zero_partial_object(inode, offset, &size); 2725 if (ret < 0) 2726 return ret; 2727 offset += size; 2728 length -= size; 2729 } 2730 return ret; 2731 } 2732 2733 static long ceph_fallocate(struct file *file, int mode, 2734 loff_t offset, loff_t length) 2735 { 2736 struct ceph_file_info *fi = file->private_data; 2737 struct inode *inode = file_inode(file); 2738 struct ceph_inode_info *ci = ceph_inode(inode); 2739 struct ceph_cap_flush *prealloc_cf; 2740 struct ceph_client *cl = ceph_inode_to_client(inode); 2741 int want, got = 0; 2742 int dirty; 2743 int ret = 0; 2744 loff_t endoff = 0; 2745 loff_t size; 2746 2747 doutc(cl, "%p %llx.%llx mode %x, offset %llu length %llu\n", 2748 inode, ceph_vinop(inode), mode, offset, length); 2749 2750 if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 2751 return -EOPNOTSUPP; 2752 2753 if (!S_ISREG(inode->i_mode)) 2754 return -EOPNOTSUPP; 2755 2756 if (IS_ENCRYPTED(inode)) 2757 return -EOPNOTSUPP; 2758 2759 prealloc_cf = ceph_alloc_cap_flush(); 2760 if (!prealloc_cf) 2761 return -ENOMEM; 2762 2763 inode_lock(inode); 2764 2765 if (ceph_snap(inode) != CEPH_NOSNAP) { 2766 ret = -EROFS; 2767 goto unlock; 2768 } 2769 2770 size = i_size_read(inode); 2771 2772 /* Are we punching a hole beyond EOF? */ 2773 if (offset >= size) 2774 goto unlock; 2775 if ((offset + length) > size) 2776 length = size - offset; 2777 2778 if (fi->fmode & CEPH_FILE_MODE_LAZY) 2779 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; 2780 else 2781 want = CEPH_CAP_FILE_BUFFER; 2782 2783 ret = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, endoff, &got); 2784 if (ret < 0) 2785 goto unlock; 2786 2787 ret = file_modified(file); 2788 if (ret) 2789 goto put_caps; 2790 2791 filemap_invalidate_lock(inode->i_mapping); 2792 ceph_fscache_invalidate(inode, false); 2793 ceph_zero_pagecache_range(inode, offset, length); 2794 ret = ceph_zero_objects(inode, offset, length); 2795 2796 if (!ret) { 2797 spin_lock(&ci->i_ceph_lock); 2798 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, 2799 &prealloc_cf); 2800 spin_unlock(&ci->i_ceph_lock); 2801 if (dirty) 2802 __mark_inode_dirty(inode, dirty); 2803 } 2804 filemap_invalidate_unlock(inode->i_mapping); 2805 2806 put_caps: 2807 ceph_put_cap_refs(ci, got); 2808 unlock: 2809 inode_unlock(inode); 2810 ceph_free_cap_flush(prealloc_cf); 2811 return ret; 2812 } 2813 2814 /* 2815 * This function tries to get FILE_WR capabilities for dst_ci and FILE_RD for 2816 * src_ci. Two attempts are made to obtain both caps, and an error is return if 2817 * this fails; zero is returned on success. 2818 */ 2819 static int get_rd_wr_caps(struct file *src_filp, int *src_got, 2820 struct file *dst_filp, 2821 loff_t dst_endoff, int *dst_got) 2822 { 2823 int ret = 0; 2824 bool retrying = false; 2825 2826 retry_caps: 2827 ret = ceph_get_caps(dst_filp, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER, 2828 dst_endoff, dst_got); 2829 if (ret < 0) 2830 return ret; 2831 2832 /* 2833 * Since we're already holding the FILE_WR capability for the dst file, 2834 * we would risk a deadlock by using ceph_get_caps. Thus, we'll do some 2835 * retry dance instead to try to get both capabilities. 2836 */ 2837 ret = ceph_try_get_caps(file_inode(src_filp), 2838 CEPH_CAP_FILE_RD, CEPH_CAP_FILE_SHARED, 2839 false, src_got); 2840 if (ret <= 0) { 2841 /* Start by dropping dst_ci caps and getting src_ci caps */ 2842 ceph_put_cap_refs(ceph_inode(file_inode(dst_filp)), *dst_got); 2843 if (retrying) { 2844 if (!ret) 2845 /* ceph_try_get_caps masks EAGAIN */ 2846 ret = -EAGAIN; 2847 return ret; 2848 } 2849 ret = ceph_get_caps(src_filp, CEPH_CAP_FILE_RD, 2850 CEPH_CAP_FILE_SHARED, -1, src_got); 2851 if (ret < 0) 2852 return ret; 2853 /*... drop src_ci caps too, and retry */ 2854 ceph_put_cap_refs(ceph_inode(file_inode(src_filp)), *src_got); 2855 retrying = true; 2856 goto retry_caps; 2857 } 2858 return ret; 2859 } 2860 2861 static void put_rd_wr_caps(struct ceph_inode_info *src_ci, int src_got, 2862 struct ceph_inode_info *dst_ci, int dst_got) 2863 { 2864 ceph_put_cap_refs(src_ci, src_got); 2865 ceph_put_cap_refs(dst_ci, dst_got); 2866 } 2867 2868 /* 2869 * This function does several size-related checks, returning an error if: 2870 * - source file is smaller than off+len 2871 * - destination file size is not OK (inode_newsize_ok()) 2872 * - max bytes quotas is exceeded 2873 */ 2874 static int is_file_size_ok(struct inode *src_inode, struct inode *dst_inode, 2875 loff_t src_off, loff_t dst_off, size_t len) 2876 { 2877 struct ceph_client *cl = ceph_inode_to_client(src_inode); 2878 loff_t size, endoff; 2879 2880 size = i_size_read(src_inode); 2881 /* 2882 * Don't copy beyond source file EOF. Instead of simply setting length 2883 * to (size - src_off), just drop to VFS default implementation, as the 2884 * local i_size may be stale due to other clients writing to the source 2885 * inode. 2886 */ 2887 if (src_off + len > size) { 2888 doutc(cl, "Copy beyond EOF (%llu + %zu > %llu)\n", src_off, 2889 len, size); 2890 return -EOPNOTSUPP; 2891 } 2892 size = i_size_read(dst_inode); 2893 2894 endoff = dst_off + len; 2895 if (inode_newsize_ok(dst_inode, endoff)) 2896 return -EOPNOTSUPP; 2897 2898 if (ceph_quota_is_max_bytes_exceeded(dst_inode, endoff)) 2899 return -EDQUOT; 2900 2901 return 0; 2902 } 2903 2904 static struct ceph_osd_request * 2905 ceph_alloc_copyfrom_request(struct ceph_osd_client *osdc, 2906 u64 src_snapid, 2907 struct ceph_object_id *src_oid, 2908 struct ceph_object_locator *src_oloc, 2909 struct ceph_object_id *dst_oid, 2910 struct ceph_object_locator *dst_oloc, 2911 u32 truncate_seq, u64 truncate_size) 2912 { 2913 struct ceph_osd_request *req; 2914 int ret; 2915 u32 src_fadvise_flags = 2916 CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL | 2917 CEPH_OSD_OP_FLAG_FADVISE_NOCACHE; 2918 u32 dst_fadvise_flags = 2919 CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL | 2920 CEPH_OSD_OP_FLAG_FADVISE_DONTNEED; 2921 2922 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); 2923 if (!req) 2924 return ERR_PTR(-ENOMEM); 2925 2926 req->r_flags = CEPH_OSD_FLAG_WRITE; 2927 2928 ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc); 2929 ceph_oid_copy(&req->r_t.base_oid, dst_oid); 2930 2931 ret = osd_req_op_copy_from_init(req, src_snapid, 0, 2932 src_oid, src_oloc, 2933 src_fadvise_flags, 2934 dst_fadvise_flags, 2935 truncate_seq, 2936 truncate_size, 2937 CEPH_OSD_COPY_FROM_FLAG_TRUNCATE_SEQ); 2938 if (ret) 2939 goto out; 2940 2941 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); 2942 if (ret) 2943 goto out; 2944 2945 return req; 2946 2947 out: 2948 ceph_osdc_put_request(req); 2949 return ERR_PTR(ret); 2950 } 2951 2952 static ssize_t ceph_do_objects_copy(struct ceph_inode_info *src_ci, u64 *src_off, 2953 struct ceph_inode_info *dst_ci, u64 *dst_off, 2954 struct ceph_fs_client *fsc, 2955 size_t len, unsigned int flags) 2956 { 2957 struct ceph_object_locator src_oloc, dst_oloc; 2958 struct ceph_object_id src_oid, dst_oid; 2959 struct ceph_osd_client *osdc; 2960 struct ceph_osd_request *req; 2961 ssize_t bytes = 0; 2962 u64 src_objnum, src_objoff, dst_objnum, dst_objoff; 2963 u32 src_objlen, dst_objlen; 2964 u32 object_size = src_ci->i_layout.object_size; 2965 struct ceph_client *cl = fsc->client; 2966 int ret; 2967 2968 src_oloc.pool = src_ci->i_layout.pool_id; 2969 src_oloc.pool_ns = ceph_try_get_string(src_ci->i_layout.pool_ns); 2970 dst_oloc.pool = dst_ci->i_layout.pool_id; 2971 dst_oloc.pool_ns = ceph_try_get_string(dst_ci->i_layout.pool_ns); 2972 osdc = &fsc->client->osdc; 2973 2974 while (len >= object_size) { 2975 ceph_calc_file_object_mapping(&src_ci->i_layout, *src_off, 2976 object_size, &src_objnum, 2977 &src_objoff, &src_objlen); 2978 ceph_calc_file_object_mapping(&dst_ci->i_layout, *dst_off, 2979 object_size, &dst_objnum, 2980 &dst_objoff, &dst_objlen); 2981 ceph_oid_init(&src_oid); 2982 ceph_oid_printf(&src_oid, "%llx.%08llx", 2983 src_ci->i_vino.ino, src_objnum); 2984 ceph_oid_init(&dst_oid); 2985 ceph_oid_printf(&dst_oid, "%llx.%08llx", 2986 dst_ci->i_vino.ino, dst_objnum); 2987 /* Do an object remote copy */ 2988 req = ceph_alloc_copyfrom_request(osdc, src_ci->i_vino.snap, 2989 &src_oid, &src_oloc, 2990 &dst_oid, &dst_oloc, 2991 dst_ci->i_truncate_seq, 2992 dst_ci->i_truncate_size); 2993 if (IS_ERR(req)) 2994 ret = PTR_ERR(req); 2995 else { 2996 ceph_osdc_start_request(osdc, req); 2997 ret = ceph_osdc_wait_request(osdc, req); 2998 ceph_update_copyfrom_metrics(&fsc->mdsc->metric, 2999 req->r_start_latency, 3000 req->r_end_latency, 3001 object_size, ret); 3002 ceph_osdc_put_request(req); 3003 } 3004 if (ret) { 3005 if (ret == -EOPNOTSUPP) { 3006 fsc->have_copy_from2 = false; 3007 pr_notice_client(cl, 3008 "OSDs don't support copy-from2; disabling copy offload\n"); 3009 } 3010 doutc(cl, "returned %d\n", ret); 3011 if (bytes <= 0) 3012 bytes = ret; 3013 goto out; 3014 } 3015 len -= object_size; 3016 bytes += object_size; 3017 *src_off += object_size; 3018 *dst_off += object_size; 3019 } 3020 3021 out: 3022 ceph_oloc_destroy(&src_oloc); 3023 ceph_oloc_destroy(&dst_oloc); 3024 return bytes; 3025 } 3026 3027 static ssize_t __ceph_copy_file_range(struct file *src_file, loff_t src_off, 3028 struct file *dst_file, loff_t dst_off, 3029 size_t len, unsigned int flags) 3030 { 3031 struct inode *src_inode = file_inode(src_file); 3032 struct inode *dst_inode = file_inode(dst_file); 3033 struct ceph_inode_info *src_ci = ceph_inode(src_inode); 3034 struct ceph_inode_info *dst_ci = ceph_inode(dst_inode); 3035 struct ceph_cap_flush *prealloc_cf; 3036 struct ceph_fs_client *src_fsc = ceph_inode_to_fs_client(src_inode); 3037 struct ceph_client *cl = src_fsc->client; 3038 loff_t size; 3039 ssize_t ret = -EIO, bytes; 3040 u64 src_objnum, dst_objnum, src_objoff, dst_objoff; 3041 u32 src_objlen, dst_objlen; 3042 int src_got = 0, dst_got = 0, err, dirty; 3043 3044 if (src_inode->i_sb != dst_inode->i_sb) { 3045 struct ceph_fs_client *dst_fsc = ceph_inode_to_fs_client(dst_inode); 3046 3047 if (ceph_fsid_compare(&src_fsc->client->fsid, 3048 &dst_fsc->client->fsid)) { 3049 dout("Copying files across clusters: src: %pU dst: %pU\n", 3050 &src_fsc->client->fsid, &dst_fsc->client->fsid); 3051 return -EXDEV; 3052 } 3053 } 3054 if (ceph_snap(dst_inode) != CEPH_NOSNAP) 3055 return -EROFS; 3056 3057 /* 3058 * Some of the checks below will return -EOPNOTSUPP, which will force a 3059 * fallback to the default VFS copy_file_range implementation. This is 3060 * desirable in several cases (for ex, the 'len' is smaller than the 3061 * size of the objects, or in cases where that would be more 3062 * efficient). 3063 */ 3064 3065 if (ceph_test_mount_opt(src_fsc, NOCOPYFROM)) 3066 return -EOPNOTSUPP; 3067 3068 if (!src_fsc->have_copy_from2) 3069 return -EOPNOTSUPP; 3070 3071 /* 3072 * Striped file layouts require that we copy partial objects, but the 3073 * OSD copy-from operation only supports full-object copies. Limit 3074 * this to non-striped file layouts for now. 3075 */ 3076 if ((src_ci->i_layout.stripe_unit != dst_ci->i_layout.stripe_unit) || 3077 (src_ci->i_layout.stripe_count != 1) || 3078 (dst_ci->i_layout.stripe_count != 1) || 3079 (src_ci->i_layout.object_size != dst_ci->i_layout.object_size)) { 3080 doutc(cl, "Invalid src/dst files layout\n"); 3081 return -EOPNOTSUPP; 3082 } 3083 3084 /* Every encrypted inode gets its own key, so we can't offload them */ 3085 if (IS_ENCRYPTED(src_inode) || IS_ENCRYPTED(dst_inode)) 3086 return -EOPNOTSUPP; 3087 3088 if (len < src_ci->i_layout.object_size) 3089 return -EOPNOTSUPP; /* no remote copy will be done */ 3090 3091 prealloc_cf = ceph_alloc_cap_flush(); 3092 if (!prealloc_cf) 3093 return -ENOMEM; 3094 3095 /* Start by sync'ing the source and destination files */ 3096 ret = file_write_and_wait_range(src_file, src_off, (src_off + len)); 3097 if (ret < 0) { 3098 doutc(cl, "failed to write src file (%zd)\n", ret); 3099 goto out; 3100 } 3101 ret = file_write_and_wait_range(dst_file, dst_off, (dst_off + len)); 3102 if (ret < 0) { 3103 doutc(cl, "failed to write dst file (%zd)\n", ret); 3104 goto out; 3105 } 3106 3107 /* 3108 * We need FILE_WR caps for dst_ci and FILE_RD for src_ci as other 3109 * clients may have dirty data in their caches. And OSDs know nothing 3110 * about caps, so they can't safely do the remote object copies. 3111 */ 3112 err = get_rd_wr_caps(src_file, &src_got, 3113 dst_file, (dst_off + len), &dst_got); 3114 if (err < 0) { 3115 doutc(cl, "get_rd_wr_caps returned %d\n", err); 3116 ret = -EOPNOTSUPP; 3117 goto out; 3118 } 3119 3120 ret = is_file_size_ok(src_inode, dst_inode, src_off, dst_off, len); 3121 if (ret < 0) 3122 goto out_caps; 3123 3124 /* Drop dst file cached pages */ 3125 ceph_fscache_invalidate(dst_inode, false); 3126 ret = invalidate_inode_pages2_range(dst_inode->i_mapping, 3127 dst_off >> PAGE_SHIFT, 3128 (dst_off + len) >> PAGE_SHIFT); 3129 if (ret < 0) { 3130 doutc(cl, "Failed to invalidate inode pages (%zd)\n", 3131 ret); 3132 ret = 0; /* XXX */ 3133 } 3134 ceph_calc_file_object_mapping(&src_ci->i_layout, src_off, 3135 src_ci->i_layout.object_size, 3136 &src_objnum, &src_objoff, &src_objlen); 3137 ceph_calc_file_object_mapping(&dst_ci->i_layout, dst_off, 3138 dst_ci->i_layout.object_size, 3139 &dst_objnum, &dst_objoff, &dst_objlen); 3140 /* object-level offsets need to the same */ 3141 if (src_objoff != dst_objoff) { 3142 ret = -EOPNOTSUPP; 3143 goto out_caps; 3144 } 3145 3146 /* 3147 * Do a manual copy if the object offset isn't object aligned. 3148 * 'src_objlen' contains the bytes left until the end of the object, 3149 * starting at the src_off 3150 */ 3151 if (src_objoff) { 3152 doutc(cl, "Initial partial copy of %u bytes\n", src_objlen); 3153 3154 /* 3155 * we need to temporarily drop all caps as we'll be calling 3156 * {read,write}_iter, which will get caps again. 3157 */ 3158 put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got); 3159 ret = splice_file_range(src_file, &src_off, dst_file, &dst_off, 3160 src_objlen); 3161 /* Abort on short copies or on error */ 3162 if (ret < (long)src_objlen) { 3163 doutc(cl, "Failed partial copy (%zd)\n", ret); 3164 goto out; 3165 } 3166 len -= ret; 3167 err = get_rd_wr_caps(src_file, &src_got, 3168 dst_file, (dst_off + len), &dst_got); 3169 if (err < 0) 3170 goto out; 3171 err = is_file_size_ok(src_inode, dst_inode, 3172 src_off, dst_off, len); 3173 if (err < 0) 3174 goto out_caps; 3175 } 3176 3177 size = i_size_read(dst_inode); 3178 bytes = ceph_do_objects_copy(src_ci, &src_off, dst_ci, &dst_off, 3179 src_fsc, len, flags); 3180 if (bytes <= 0) { 3181 if (!ret) 3182 ret = bytes; 3183 goto out_caps; 3184 } 3185 doutc(cl, "Copied %zu bytes out of %zu\n", bytes, len); 3186 len -= bytes; 3187 ret += bytes; 3188 3189 file_update_time(dst_file); 3190 inode_inc_iversion_raw(dst_inode); 3191 3192 if (dst_off > size) { 3193 /* Let the MDS know about dst file size change */ 3194 if (ceph_inode_set_size(dst_inode, dst_off) || 3195 ceph_quota_is_max_bytes_approaching(dst_inode, dst_off)) 3196 ceph_check_caps(dst_ci, CHECK_CAPS_AUTHONLY | CHECK_CAPS_FLUSH); 3197 } 3198 /* Mark Fw dirty */ 3199 spin_lock(&dst_ci->i_ceph_lock); 3200 dirty = __ceph_mark_dirty_caps(dst_ci, CEPH_CAP_FILE_WR, &prealloc_cf); 3201 spin_unlock(&dst_ci->i_ceph_lock); 3202 if (dirty) 3203 __mark_inode_dirty(dst_inode, dirty); 3204 3205 out_caps: 3206 put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got); 3207 3208 /* 3209 * Do the final manual copy if we still have some bytes left, unless 3210 * there were errors in remote object copies (len >= object_size). 3211 */ 3212 if (len && (len < src_ci->i_layout.object_size)) { 3213 doutc(cl, "Final partial copy of %zu bytes\n", len); 3214 bytes = splice_file_range(src_file, &src_off, dst_file, 3215 &dst_off, len); 3216 if (bytes > 0) 3217 ret += bytes; 3218 else 3219 doutc(cl, "Failed partial copy (%zd)\n", bytes); 3220 } 3221 3222 out: 3223 ceph_free_cap_flush(prealloc_cf); 3224 3225 return ret; 3226 } 3227 3228 static ssize_t ceph_copy_file_range(struct file *src_file, loff_t src_off, 3229 struct file *dst_file, loff_t dst_off, 3230 size_t len, unsigned int flags) 3231 { 3232 ssize_t ret; 3233 3234 ret = __ceph_copy_file_range(src_file, src_off, dst_file, dst_off, 3235 len, flags); 3236 3237 if (ret == -EOPNOTSUPP || ret == -EXDEV) 3238 ret = splice_copy_file_range(src_file, src_off, dst_file, 3239 dst_off, len); 3240 return ret; 3241 } 3242 3243 const struct file_operations ceph_file_fops = { 3244 .open = ceph_open, 3245 .release = ceph_release, 3246 .llseek = ceph_llseek, 3247 .read_iter = ceph_read_iter, 3248 .write_iter = ceph_write_iter, 3249 .mmap_prepare = ceph_mmap_prepare, 3250 .fsync = ceph_fsync, 3251 .lock = ceph_lock, 3252 .flock = ceph_flock, 3253 .splice_read = ceph_splice_read, 3254 .splice_write = iter_file_splice_write, 3255 .unlocked_ioctl = ceph_ioctl, 3256 .compat_ioctl = compat_ptr_ioctl, 3257 .fallocate = ceph_fallocate, 3258 .copy_file_range = ceph_copy_file_range, 3259 }; 3260