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