1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * (C) 2001 Clemson University and The University of Chicago 4 * Copyright 2018 Omnibond Systems, L.L.C. 5 * 6 * See COPYING in top-level directory. 7 */ 8 9 /* 10 * Linux VFS inode operations. 11 */ 12 13 #include <linux/blkdev.h> 14 #include <linux/fileattr.h> 15 #include "protocol.h" 16 #include "orangefs-kernel.h" 17 #include "orangefs-bufmap.h" 18 19 static int orangefs_writepage_locked(struct page *page, 20 struct writeback_control *wbc) 21 { 22 struct inode *inode = page->mapping->host; 23 struct orangefs_write_range *wr = NULL; 24 struct iov_iter iter; 25 struct bio_vec bv; 26 size_t len, wlen; 27 ssize_t ret; 28 loff_t off; 29 30 set_page_writeback(page); 31 32 len = i_size_read(inode); 33 if (PagePrivate(page)) { 34 wr = (struct orangefs_write_range *)page_private(page); 35 WARN_ON(wr->pos >= len); 36 off = wr->pos; 37 if (off + wr->len > len) 38 wlen = len - off; 39 else 40 wlen = wr->len; 41 } else { 42 WARN_ON(1); 43 off = page_offset(page); 44 if (off + PAGE_SIZE > len) 45 wlen = len - off; 46 else 47 wlen = PAGE_SIZE; 48 } 49 /* Should've been handled in orangefs_invalidate_folio. */ 50 WARN_ON(off == len || off + wlen > len); 51 52 WARN_ON(wlen == 0); 53 bvec_set_page(&bv, page, wlen, off % PAGE_SIZE); 54 iov_iter_bvec(&iter, ITER_SOURCE, &bv, 1, wlen); 55 56 ret = wait_for_direct_io(ORANGEFS_IO_WRITE, inode, &off, &iter, wlen, 57 len, wr, NULL, NULL); 58 if (ret < 0) { 59 SetPageError(page); 60 mapping_set_error(page->mapping, ret); 61 } else { 62 ret = 0; 63 } 64 kfree(detach_page_private(page)); 65 return ret; 66 } 67 68 static int orangefs_writepage(struct page *page, struct writeback_control *wbc) 69 { 70 int ret; 71 ret = orangefs_writepage_locked(page, wbc); 72 unlock_page(page); 73 end_page_writeback(page); 74 return ret; 75 } 76 77 struct orangefs_writepages { 78 loff_t off; 79 size_t len; 80 kuid_t uid; 81 kgid_t gid; 82 int maxpages; 83 int npages; 84 struct page **pages; 85 struct bio_vec *bv; 86 }; 87 88 static int orangefs_writepages_work(struct orangefs_writepages *ow, 89 struct writeback_control *wbc) 90 { 91 struct inode *inode = ow->pages[0]->mapping->host; 92 struct orangefs_write_range *wrp, wr; 93 struct iov_iter iter; 94 ssize_t ret; 95 size_t len; 96 loff_t off; 97 int i; 98 99 len = i_size_read(inode); 100 101 for (i = 0; i < ow->npages; i++) { 102 set_page_writeback(ow->pages[i]); 103 bvec_set_page(&ow->bv[i], ow->pages[i], 104 min(page_offset(ow->pages[i]) + PAGE_SIZE, 105 ow->off + ow->len) - 106 max(ow->off, page_offset(ow->pages[i])), 107 i == 0 ? ow->off - page_offset(ow->pages[i]) : 0); 108 } 109 iov_iter_bvec(&iter, ITER_SOURCE, ow->bv, ow->npages, ow->len); 110 111 WARN_ON(ow->off >= len); 112 if (ow->off + ow->len > len) 113 ow->len = len - ow->off; 114 115 off = ow->off; 116 wr.uid = ow->uid; 117 wr.gid = ow->gid; 118 ret = wait_for_direct_io(ORANGEFS_IO_WRITE, inode, &off, &iter, ow->len, 119 0, &wr, NULL, NULL); 120 if (ret < 0) { 121 for (i = 0; i < ow->npages; i++) { 122 SetPageError(ow->pages[i]); 123 mapping_set_error(ow->pages[i]->mapping, ret); 124 if (PagePrivate(ow->pages[i])) { 125 wrp = (struct orangefs_write_range *) 126 page_private(ow->pages[i]); 127 ClearPagePrivate(ow->pages[i]); 128 put_page(ow->pages[i]); 129 kfree(wrp); 130 } 131 end_page_writeback(ow->pages[i]); 132 unlock_page(ow->pages[i]); 133 } 134 } else { 135 ret = 0; 136 for (i = 0; i < ow->npages; i++) { 137 if (PagePrivate(ow->pages[i])) { 138 wrp = (struct orangefs_write_range *) 139 page_private(ow->pages[i]); 140 ClearPagePrivate(ow->pages[i]); 141 put_page(ow->pages[i]); 142 kfree(wrp); 143 } 144 end_page_writeback(ow->pages[i]); 145 unlock_page(ow->pages[i]); 146 } 147 } 148 return ret; 149 } 150 151 static int orangefs_writepages_callback(struct folio *folio, 152 struct writeback_control *wbc, void *data) 153 { 154 struct orangefs_writepages *ow = data; 155 struct orangefs_write_range *wr = folio->private; 156 int ret; 157 158 if (!wr) { 159 folio_unlock(folio); 160 /* It's not private so there's nothing to write, right? */ 161 printk("writepages_callback not private!\n"); 162 BUG(); 163 return 0; 164 } 165 166 ret = -1; 167 if (ow->npages == 0) { 168 ow->off = wr->pos; 169 ow->len = wr->len; 170 ow->uid = wr->uid; 171 ow->gid = wr->gid; 172 ow->pages[ow->npages++] = &folio->page; 173 ret = 0; 174 goto done; 175 } 176 if (!uid_eq(ow->uid, wr->uid) || !gid_eq(ow->gid, wr->gid)) { 177 orangefs_writepages_work(ow, wbc); 178 ow->npages = 0; 179 ret = -1; 180 goto done; 181 } 182 if (ow->off + ow->len == wr->pos) { 183 ow->len += wr->len; 184 ow->pages[ow->npages++] = &folio->page; 185 ret = 0; 186 goto done; 187 } 188 done: 189 if (ret == -1) { 190 if (ow->npages) { 191 orangefs_writepages_work(ow, wbc); 192 ow->npages = 0; 193 } 194 ret = orangefs_writepage_locked(&folio->page, wbc); 195 mapping_set_error(folio->mapping, ret); 196 folio_unlock(folio); 197 folio_end_writeback(folio); 198 } else { 199 if (ow->npages == ow->maxpages) { 200 orangefs_writepages_work(ow, wbc); 201 ow->npages = 0; 202 } 203 } 204 return ret; 205 } 206 207 static int orangefs_writepages(struct address_space *mapping, 208 struct writeback_control *wbc) 209 { 210 struct orangefs_writepages *ow; 211 struct blk_plug plug; 212 int ret; 213 ow = kzalloc(sizeof(struct orangefs_writepages), GFP_KERNEL); 214 if (!ow) 215 return -ENOMEM; 216 ow->maxpages = orangefs_bufmap_size_query()/PAGE_SIZE; 217 ow->pages = kcalloc(ow->maxpages, sizeof(struct page *), GFP_KERNEL); 218 if (!ow->pages) { 219 kfree(ow); 220 return -ENOMEM; 221 } 222 ow->bv = kcalloc(ow->maxpages, sizeof(struct bio_vec), GFP_KERNEL); 223 if (!ow->bv) { 224 kfree(ow->pages); 225 kfree(ow); 226 return -ENOMEM; 227 } 228 blk_start_plug(&plug); 229 ret = write_cache_pages(mapping, wbc, orangefs_writepages_callback, ow); 230 if (ow->npages) 231 ret = orangefs_writepages_work(ow, wbc); 232 blk_finish_plug(&plug); 233 kfree(ow->pages); 234 kfree(ow->bv); 235 kfree(ow); 236 return ret; 237 } 238 239 static int orangefs_launder_folio(struct folio *); 240 241 static void orangefs_readahead(struct readahead_control *rac) 242 { 243 loff_t offset; 244 struct iov_iter iter; 245 struct inode *inode = rac->mapping->host; 246 struct xarray *i_pages; 247 struct page *page; 248 loff_t new_start = readahead_pos(rac); 249 int ret; 250 size_t new_len = 0; 251 252 loff_t bytes_remaining = inode->i_size - readahead_pos(rac); 253 loff_t pages_remaining = bytes_remaining / PAGE_SIZE; 254 255 if (pages_remaining >= 1024) 256 new_len = 4194304; 257 else if (pages_remaining > readahead_count(rac)) 258 new_len = bytes_remaining; 259 260 if (new_len) 261 readahead_expand(rac, new_start, new_len); 262 263 offset = readahead_pos(rac); 264 i_pages = &rac->mapping->i_pages; 265 266 iov_iter_xarray(&iter, ITER_DEST, i_pages, offset, readahead_length(rac)); 267 268 /* read in the pages. */ 269 if ((ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, 270 &offset, &iter, readahead_length(rac), 271 inode->i_size, NULL, NULL, rac->file)) < 0) 272 gossip_debug(GOSSIP_FILE_DEBUG, 273 "%s: wait_for_direct_io failed. \n", __func__); 274 else 275 ret = 0; 276 277 /* clean up. */ 278 while ((page = readahead_page(rac))) { 279 page_endio(page, false, ret); 280 put_page(page); 281 } 282 } 283 284 static int orangefs_read_folio(struct file *file, struct folio *folio) 285 { 286 struct inode *inode = folio->mapping->host; 287 struct iov_iter iter; 288 struct bio_vec bv; 289 ssize_t ret; 290 loff_t off; /* offset of this folio in the file */ 291 292 if (folio_test_dirty(folio)) 293 orangefs_launder_folio(folio); 294 295 off = folio_pos(folio); 296 bvec_set_folio(&bv, folio, folio_size(folio), 0); 297 iov_iter_bvec(&iter, ITER_DEST, &bv, 1, folio_size(folio)); 298 299 ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, &off, &iter, 300 folio_size(folio), inode->i_size, NULL, NULL, file); 301 /* this will only zero remaining unread portions of the folio data */ 302 iov_iter_zero(~0U, &iter); 303 /* takes care of potential aliasing */ 304 flush_dcache_folio(folio); 305 if (ret < 0) { 306 folio_set_error(folio); 307 } else { 308 folio_mark_uptodate(folio); 309 ret = 0; 310 } 311 /* unlock the folio after the ->read_folio() routine completes */ 312 folio_unlock(folio); 313 return ret; 314 } 315 316 static int orangefs_write_begin(struct file *file, 317 struct address_space *mapping, loff_t pos, unsigned len, 318 struct page **pagep, void **fsdata) 319 { 320 struct orangefs_write_range *wr; 321 struct folio *folio; 322 struct page *page; 323 pgoff_t index; 324 int ret; 325 326 index = pos >> PAGE_SHIFT; 327 328 page = grab_cache_page_write_begin(mapping, index); 329 if (!page) 330 return -ENOMEM; 331 332 *pagep = page; 333 folio = page_folio(page); 334 335 if (folio_test_dirty(folio) && !folio_test_private(folio)) { 336 /* 337 * Should be impossible. If it happens, launder the page 338 * since we don't know what's dirty. This will WARN in 339 * orangefs_writepage_locked. 340 */ 341 ret = orangefs_launder_folio(folio); 342 if (ret) 343 return ret; 344 } 345 if (folio_test_private(folio)) { 346 struct orangefs_write_range *wr; 347 wr = folio_get_private(folio); 348 if (wr->pos + wr->len == pos && 349 uid_eq(wr->uid, current_fsuid()) && 350 gid_eq(wr->gid, current_fsgid())) { 351 wr->len += len; 352 goto okay; 353 } else { 354 ret = orangefs_launder_folio(folio); 355 if (ret) 356 return ret; 357 } 358 } 359 360 wr = kmalloc(sizeof *wr, GFP_KERNEL); 361 if (!wr) 362 return -ENOMEM; 363 364 wr->pos = pos; 365 wr->len = len; 366 wr->uid = current_fsuid(); 367 wr->gid = current_fsgid(); 368 folio_attach_private(folio, wr); 369 okay: 370 return 0; 371 } 372 373 static int orangefs_write_end(struct file *file, struct address_space *mapping, 374 loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata) 375 { 376 struct inode *inode = page->mapping->host; 377 loff_t last_pos = pos + copied; 378 379 /* 380 * No need to use i_size_read() here, the i_size 381 * cannot change under us because we hold the i_mutex. 382 */ 383 if (last_pos > inode->i_size) 384 i_size_write(inode, last_pos); 385 386 /* zero the stale part of the page if we did a short copy */ 387 if (!PageUptodate(page)) { 388 unsigned from = pos & (PAGE_SIZE - 1); 389 if (copied < len) { 390 zero_user(page, from + copied, len - copied); 391 } 392 /* Set fully written pages uptodate. */ 393 if (pos == page_offset(page) && 394 (len == PAGE_SIZE || pos + len == inode->i_size)) { 395 zero_user_segment(page, from + copied, PAGE_SIZE); 396 SetPageUptodate(page); 397 } 398 } 399 400 set_page_dirty(page); 401 unlock_page(page); 402 put_page(page); 403 404 mark_inode_dirty_sync(file_inode(file)); 405 return copied; 406 } 407 408 static void orangefs_invalidate_folio(struct folio *folio, 409 size_t offset, size_t length) 410 { 411 struct orangefs_write_range *wr = folio_get_private(folio); 412 413 if (offset == 0 && length == PAGE_SIZE) { 414 kfree(folio_detach_private(folio)); 415 return; 416 /* write range entirely within invalidate range (or equal) */ 417 } else if (folio_pos(folio) + offset <= wr->pos && 418 wr->pos + wr->len <= folio_pos(folio) + offset + length) { 419 kfree(folio_detach_private(folio)); 420 /* XXX is this right? only caller in fs */ 421 folio_cancel_dirty(folio); 422 return; 423 /* invalidate range chops off end of write range */ 424 } else if (wr->pos < folio_pos(folio) + offset && 425 wr->pos + wr->len <= folio_pos(folio) + offset + length && 426 folio_pos(folio) + offset < wr->pos + wr->len) { 427 size_t x; 428 x = wr->pos + wr->len - (folio_pos(folio) + offset); 429 WARN_ON(x > wr->len); 430 wr->len -= x; 431 wr->uid = current_fsuid(); 432 wr->gid = current_fsgid(); 433 /* invalidate range chops off beginning of write range */ 434 } else if (folio_pos(folio) + offset <= wr->pos && 435 folio_pos(folio) + offset + length < wr->pos + wr->len && 436 wr->pos < folio_pos(folio) + offset + length) { 437 size_t x; 438 x = folio_pos(folio) + offset + length - wr->pos; 439 WARN_ON(x > wr->len); 440 wr->pos += x; 441 wr->len -= x; 442 wr->uid = current_fsuid(); 443 wr->gid = current_fsgid(); 444 /* invalidate range entirely within write range (punch hole) */ 445 } else if (wr->pos < folio_pos(folio) + offset && 446 folio_pos(folio) + offset + length < wr->pos + wr->len) { 447 /* XXX what do we do here... should not WARN_ON */ 448 WARN_ON(1); 449 /* punch hole */ 450 /* 451 * should we just ignore this and write it out anyway? 452 * it hardly makes sense 453 */ 454 return; 455 /* non-overlapping ranges */ 456 } else { 457 /* WARN if they do overlap */ 458 if (!((folio_pos(folio) + offset + length <= wr->pos) ^ 459 (wr->pos + wr->len <= folio_pos(folio) + offset))) { 460 WARN_ON(1); 461 printk("invalidate range offset %llu length %zu\n", 462 folio_pos(folio) + offset, length); 463 printk("write range offset %llu length %zu\n", 464 wr->pos, wr->len); 465 } 466 return; 467 } 468 469 /* 470 * Above there are returns where wr is freed or where we WARN. 471 * Thus the following runs if wr was modified above. 472 */ 473 474 orangefs_launder_folio(folio); 475 } 476 477 static bool orangefs_release_folio(struct folio *folio, gfp_t foo) 478 { 479 return !folio_test_private(folio); 480 } 481 482 static void orangefs_free_folio(struct folio *folio) 483 { 484 kfree(folio_detach_private(folio)); 485 } 486 487 static int orangefs_launder_folio(struct folio *folio) 488 { 489 int r = 0; 490 struct writeback_control wbc = { 491 .sync_mode = WB_SYNC_ALL, 492 .nr_to_write = 0, 493 }; 494 folio_wait_writeback(folio); 495 if (folio_clear_dirty_for_io(folio)) { 496 r = orangefs_writepage_locked(&folio->page, &wbc); 497 folio_end_writeback(folio); 498 } 499 return r; 500 } 501 502 static ssize_t orangefs_direct_IO(struct kiocb *iocb, 503 struct iov_iter *iter) 504 { 505 /* 506 * Comment from original do_readv_writev: 507 * Common entry point for read/write/readv/writev 508 * This function will dispatch it to either the direct I/O 509 * or buffered I/O path depending on the mount options and/or 510 * augmented/extended metadata attached to the file. 511 * Note: File extended attributes override any mount options. 512 */ 513 struct file *file = iocb->ki_filp; 514 loff_t pos = iocb->ki_pos; 515 enum ORANGEFS_io_type type = iov_iter_rw(iter) == WRITE ? 516 ORANGEFS_IO_WRITE : ORANGEFS_IO_READ; 517 loff_t *offset = &pos; 518 struct inode *inode = file->f_mapping->host; 519 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); 520 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle; 521 size_t count = iov_iter_count(iter); 522 ssize_t total_count = 0; 523 ssize_t ret = -EINVAL; 524 525 gossip_debug(GOSSIP_FILE_DEBUG, 526 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n", 527 __func__, 528 handle, 529 (int)count); 530 531 if (type == ORANGEFS_IO_WRITE) { 532 gossip_debug(GOSSIP_FILE_DEBUG, 533 "%s(%pU): proceeding with offset : %llu, " 534 "size %d\n", 535 __func__, 536 handle, 537 llu(*offset), 538 (int)count); 539 } 540 541 if (count == 0) { 542 ret = 0; 543 goto out; 544 } 545 546 while (iov_iter_count(iter)) { 547 size_t each_count = iov_iter_count(iter); 548 size_t amt_complete; 549 550 /* how much to transfer in this loop iteration */ 551 if (each_count > orangefs_bufmap_size_query()) 552 each_count = orangefs_bufmap_size_query(); 553 554 gossip_debug(GOSSIP_FILE_DEBUG, 555 "%s(%pU): size of each_count(%d)\n", 556 __func__, 557 handle, 558 (int)each_count); 559 gossip_debug(GOSSIP_FILE_DEBUG, 560 "%s(%pU): BEFORE wait_for_io: offset is %d\n", 561 __func__, 562 handle, 563 (int)*offset); 564 565 ret = wait_for_direct_io(type, inode, offset, iter, 566 each_count, 0, NULL, NULL, file); 567 gossip_debug(GOSSIP_FILE_DEBUG, 568 "%s(%pU): return from wait_for_io:%d\n", 569 __func__, 570 handle, 571 (int)ret); 572 573 if (ret < 0) 574 goto out; 575 576 *offset += ret; 577 total_count += ret; 578 amt_complete = ret; 579 580 gossip_debug(GOSSIP_FILE_DEBUG, 581 "%s(%pU): AFTER wait_for_io: offset is %d\n", 582 __func__, 583 handle, 584 (int)*offset); 585 586 /* 587 * if we got a short I/O operations, 588 * fall out and return what we got so far 589 */ 590 if (amt_complete < each_count) 591 break; 592 } /*end while */ 593 594 out: 595 if (total_count > 0) 596 ret = total_count; 597 if (ret > 0) { 598 if (type == ORANGEFS_IO_READ) { 599 file_accessed(file); 600 } else { 601 file_update_time(file); 602 if (*offset > i_size_read(inode)) 603 i_size_write(inode, *offset); 604 } 605 } 606 607 gossip_debug(GOSSIP_FILE_DEBUG, 608 "%s(%pU): Value(%d) returned.\n", 609 __func__, 610 handle, 611 (int)ret); 612 613 return ret; 614 } 615 616 /** ORANGEFS2 implementation of address space operations */ 617 static const struct address_space_operations orangefs_address_operations = { 618 .writepage = orangefs_writepage, 619 .readahead = orangefs_readahead, 620 .read_folio = orangefs_read_folio, 621 .writepages = orangefs_writepages, 622 .dirty_folio = filemap_dirty_folio, 623 .write_begin = orangefs_write_begin, 624 .write_end = orangefs_write_end, 625 .invalidate_folio = orangefs_invalidate_folio, 626 .release_folio = orangefs_release_folio, 627 .free_folio = orangefs_free_folio, 628 .launder_folio = orangefs_launder_folio, 629 .direct_IO = orangefs_direct_IO, 630 }; 631 632 vm_fault_t orangefs_page_mkwrite(struct vm_fault *vmf) 633 { 634 struct folio *folio = page_folio(vmf->page); 635 struct inode *inode = file_inode(vmf->vma->vm_file); 636 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); 637 unsigned long *bitlock = &orangefs_inode->bitlock; 638 vm_fault_t ret; 639 struct orangefs_write_range *wr; 640 641 sb_start_pagefault(inode->i_sb); 642 643 if (wait_on_bit(bitlock, 1, TASK_KILLABLE)) { 644 ret = VM_FAULT_RETRY; 645 goto out; 646 } 647 648 folio_lock(folio); 649 if (folio_test_dirty(folio) && !folio_test_private(folio)) { 650 /* 651 * Should be impossible. If it happens, launder the folio 652 * since we don't know what's dirty. This will WARN in 653 * orangefs_writepage_locked. 654 */ 655 if (orangefs_launder_folio(folio)) { 656 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY; 657 goto out; 658 } 659 } 660 if (folio_test_private(folio)) { 661 wr = folio_get_private(folio); 662 if (uid_eq(wr->uid, current_fsuid()) && 663 gid_eq(wr->gid, current_fsgid())) { 664 wr->pos = page_offset(vmf->page); 665 wr->len = PAGE_SIZE; 666 goto okay; 667 } else { 668 if (orangefs_launder_folio(folio)) { 669 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY; 670 goto out; 671 } 672 } 673 } 674 wr = kmalloc(sizeof *wr, GFP_KERNEL); 675 if (!wr) { 676 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY; 677 goto out; 678 } 679 wr->pos = page_offset(vmf->page); 680 wr->len = PAGE_SIZE; 681 wr->uid = current_fsuid(); 682 wr->gid = current_fsgid(); 683 folio_attach_private(folio, wr); 684 okay: 685 686 file_update_time(vmf->vma->vm_file); 687 if (folio->mapping != inode->i_mapping) { 688 folio_unlock(folio); 689 ret = VM_FAULT_LOCKED|VM_FAULT_NOPAGE; 690 goto out; 691 } 692 693 /* 694 * We mark the folio dirty already here so that when freeze is in 695 * progress, we are guaranteed that writeback during freezing will 696 * see the dirty folio and writeprotect it again. 697 */ 698 folio_mark_dirty(folio); 699 folio_wait_stable(folio); 700 ret = VM_FAULT_LOCKED; 701 out: 702 sb_end_pagefault(inode->i_sb); 703 return ret; 704 } 705 706 static int orangefs_setattr_size(struct inode *inode, struct iattr *iattr) 707 { 708 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); 709 struct orangefs_kernel_op_s *new_op; 710 loff_t orig_size; 711 int ret = -EINVAL; 712 713 gossip_debug(GOSSIP_INODE_DEBUG, 714 "%s: %pU: Handle is %pU | fs_id %d | size is %llu\n", 715 __func__, 716 get_khandle_from_ino(inode), 717 &orangefs_inode->refn.khandle, 718 orangefs_inode->refn.fs_id, 719 iattr->ia_size); 720 721 /* Ensure that we have a up to date size, so we know if it changed. */ 722 ret = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_SIZE); 723 if (ret == -ESTALE) 724 ret = -EIO; 725 if (ret) { 726 gossip_err("%s: orangefs_inode_getattr failed, ret:%d:.\n", 727 __func__, ret); 728 return ret; 729 } 730 orig_size = i_size_read(inode); 731 732 /* This is truncate_setsize in a different order. */ 733 truncate_pagecache(inode, iattr->ia_size); 734 i_size_write(inode, iattr->ia_size); 735 if (iattr->ia_size > orig_size) 736 pagecache_isize_extended(inode, orig_size, iattr->ia_size); 737 738 new_op = op_alloc(ORANGEFS_VFS_OP_TRUNCATE); 739 if (!new_op) 740 return -ENOMEM; 741 742 new_op->upcall.req.truncate.refn = orangefs_inode->refn; 743 new_op->upcall.req.truncate.size = (__s64) iattr->ia_size; 744 745 ret = service_operation(new_op, 746 __func__, 747 get_interruptible_flag(inode)); 748 749 /* 750 * the truncate has no downcall members to retrieve, but 751 * the status value tells us if it went through ok or not 752 */ 753 gossip_debug(GOSSIP_INODE_DEBUG, "%s: ret:%d:\n", __func__, ret); 754 755 op_release(new_op); 756 757 if (ret != 0) 758 return ret; 759 760 if (orig_size != i_size_read(inode)) 761 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME; 762 763 return ret; 764 } 765 766 int __orangefs_setattr(struct inode *inode, struct iattr *iattr) 767 { 768 int ret; 769 770 if (iattr->ia_valid & ATTR_MODE) { 771 if (iattr->ia_mode & (S_ISVTX)) { 772 if (is_root_handle(inode)) { 773 /* 774 * allow sticky bit to be set on root (since 775 * it shows up that way by default anyhow), 776 * but don't show it to the server 777 */ 778 iattr->ia_mode -= S_ISVTX; 779 } else { 780 gossip_debug(GOSSIP_UTILS_DEBUG, 781 "User attempted to set sticky bit on non-root directory; returning EINVAL.\n"); 782 ret = -EINVAL; 783 goto out; 784 } 785 } 786 if (iattr->ia_mode & (S_ISUID)) { 787 gossip_debug(GOSSIP_UTILS_DEBUG, 788 "Attempting to set setuid bit (not supported); returning EINVAL.\n"); 789 ret = -EINVAL; 790 goto out; 791 } 792 } 793 794 if (iattr->ia_valid & ATTR_SIZE) { 795 ret = orangefs_setattr_size(inode, iattr); 796 if (ret) 797 goto out; 798 } 799 800 again: 801 spin_lock(&inode->i_lock); 802 if (ORANGEFS_I(inode)->attr_valid) { 803 if (uid_eq(ORANGEFS_I(inode)->attr_uid, current_fsuid()) && 804 gid_eq(ORANGEFS_I(inode)->attr_gid, current_fsgid())) { 805 ORANGEFS_I(inode)->attr_valid = iattr->ia_valid; 806 } else { 807 spin_unlock(&inode->i_lock); 808 write_inode_now(inode, 1); 809 goto again; 810 } 811 } else { 812 ORANGEFS_I(inode)->attr_valid = iattr->ia_valid; 813 ORANGEFS_I(inode)->attr_uid = current_fsuid(); 814 ORANGEFS_I(inode)->attr_gid = current_fsgid(); 815 } 816 setattr_copy(&nop_mnt_idmap, inode, iattr); 817 spin_unlock(&inode->i_lock); 818 mark_inode_dirty(inode); 819 820 ret = 0; 821 out: 822 return ret; 823 } 824 825 int __orangefs_setattr_mode(struct dentry *dentry, struct iattr *iattr) 826 { 827 int ret; 828 struct inode *inode = d_inode(dentry); 829 830 ret = __orangefs_setattr(inode, iattr); 831 /* change mode on a file that has ACLs */ 832 if (!ret && (iattr->ia_valid & ATTR_MODE)) 833 ret = posix_acl_chmod(&nop_mnt_idmap, dentry, inode->i_mode); 834 return ret; 835 } 836 837 /* 838 * Change attributes of an object referenced by dentry. 839 */ 840 int orangefs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 841 struct iattr *iattr) 842 { 843 int ret; 844 gossip_debug(GOSSIP_INODE_DEBUG, "__orangefs_setattr: called on %pd\n", 845 dentry); 846 ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr); 847 if (ret) 848 goto out; 849 ret = __orangefs_setattr_mode(dentry, iattr); 850 sync_inode_metadata(d_inode(dentry), 1); 851 out: 852 gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_setattr: returning %d\n", 853 ret); 854 return ret; 855 } 856 857 /* 858 * Obtain attributes of an object given a dentry 859 */ 860 int orangefs_getattr(struct mnt_idmap *idmap, const struct path *path, 861 struct kstat *stat, u32 request_mask, unsigned int flags) 862 { 863 int ret; 864 struct inode *inode = path->dentry->d_inode; 865 866 gossip_debug(GOSSIP_INODE_DEBUG, 867 "orangefs_getattr: called on %pd mask %u\n", 868 path->dentry, request_mask); 869 870 ret = orangefs_inode_getattr(inode, 871 request_mask & STATX_SIZE ? ORANGEFS_GETATTR_SIZE : 0); 872 if (ret == 0) { 873 generic_fillattr(&nop_mnt_idmap, inode, stat); 874 875 /* override block size reported to stat */ 876 if (!(request_mask & STATX_SIZE)) 877 stat->result_mask &= ~STATX_SIZE; 878 879 generic_fill_statx_attr(inode, stat); 880 } 881 return ret; 882 } 883 884 int orangefs_permission(struct mnt_idmap *idmap, 885 struct inode *inode, int mask) 886 { 887 int ret; 888 889 if (mask & MAY_NOT_BLOCK) 890 return -ECHILD; 891 892 gossip_debug(GOSSIP_INODE_DEBUG, "%s: refreshing\n", __func__); 893 894 /* Make sure the permission (and other common attrs) are up to date. */ 895 ret = orangefs_inode_getattr(inode, 0); 896 if (ret < 0) 897 return ret; 898 899 return generic_permission(&nop_mnt_idmap, inode, mask); 900 } 901 902 int orangefs_update_time(struct inode *inode, struct timespec64 *time, int flags) 903 { 904 struct iattr iattr; 905 gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_update_time: %pU\n", 906 get_khandle_from_ino(inode)); 907 generic_update_time(inode, time, flags); 908 memset(&iattr, 0, sizeof iattr); 909 if (flags & S_ATIME) 910 iattr.ia_valid |= ATTR_ATIME; 911 if (flags & S_CTIME) 912 iattr.ia_valid |= ATTR_CTIME; 913 if (flags & S_MTIME) 914 iattr.ia_valid |= ATTR_MTIME; 915 return __orangefs_setattr(inode, &iattr); 916 } 917 918 static int orangefs_fileattr_get(struct dentry *dentry, struct fileattr *fa) 919 { 920 u64 val = 0; 921 int ret; 922 923 gossip_debug(GOSSIP_FILE_DEBUG, "%s: called on %pd\n", __func__, 924 dentry); 925 926 ret = orangefs_inode_getxattr(d_inode(dentry), 927 "user.pvfs2.meta_hint", 928 &val, sizeof(val)); 929 if (ret < 0 && ret != -ENODATA) 930 return ret; 931 932 gossip_debug(GOSSIP_FILE_DEBUG, "%s: flags=%u\n", __func__, (u32) val); 933 934 fileattr_fill_flags(fa, val); 935 return 0; 936 } 937 938 static int orangefs_fileattr_set(struct mnt_idmap *idmap, 939 struct dentry *dentry, struct fileattr *fa) 940 { 941 u64 val = 0; 942 943 gossip_debug(GOSSIP_FILE_DEBUG, "%s: called on %pd\n", __func__, 944 dentry); 945 /* 946 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode is 947 * turned on for a file. The user is not allowed to turn on this bit, 948 * but the bit is present if the user first gets the flags and then 949 * updates the flags with some new settings. So, we ignore it in the 950 * following edit. bligon. 951 */ 952 if (fileattr_has_fsx(fa) || 953 (fa->flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL | ORANGEFS_MIRROR_FL))) { 954 gossip_err("%s: only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n", 955 __func__); 956 return -EOPNOTSUPP; 957 } 958 val = fa->flags; 959 gossip_debug(GOSSIP_FILE_DEBUG, "%s: flags=%u\n", __func__, (u32) val); 960 return orangefs_inode_setxattr(d_inode(dentry), 961 "user.pvfs2.meta_hint", 962 &val, sizeof(val), 0); 963 } 964 965 /* ORANGEFS2 implementation of VFS inode operations for files */ 966 static const struct inode_operations orangefs_file_inode_operations = { 967 .get_inode_acl = orangefs_get_acl, 968 .set_acl = orangefs_set_acl, 969 .setattr = orangefs_setattr, 970 .getattr = orangefs_getattr, 971 .listxattr = orangefs_listxattr, 972 .permission = orangefs_permission, 973 .update_time = orangefs_update_time, 974 .fileattr_get = orangefs_fileattr_get, 975 .fileattr_set = orangefs_fileattr_set, 976 }; 977 978 static int orangefs_init_iops(struct inode *inode) 979 { 980 inode->i_mapping->a_ops = &orangefs_address_operations; 981 982 switch (inode->i_mode & S_IFMT) { 983 case S_IFREG: 984 inode->i_op = &orangefs_file_inode_operations; 985 inode->i_fop = &orangefs_file_operations; 986 break; 987 case S_IFLNK: 988 inode->i_op = &orangefs_symlink_inode_operations; 989 break; 990 case S_IFDIR: 991 inode->i_op = &orangefs_dir_inode_operations; 992 inode->i_fop = &orangefs_dir_operations; 993 break; 994 default: 995 gossip_debug(GOSSIP_INODE_DEBUG, 996 "%s: unsupported mode\n", 997 __func__); 998 return -EINVAL; 999 } 1000 1001 return 0; 1002 } 1003 1004 /* 1005 * Given an ORANGEFS object identifier (fsid, handle), convert it into 1006 * a ino_t type that will be used as a hash-index from where the handle will 1007 * be searched for in the VFS hash table of inodes. 1008 */ 1009 static inline ino_t orangefs_handle_hash(struct orangefs_object_kref *ref) 1010 { 1011 if (!ref) 1012 return 0; 1013 return orangefs_khandle_to_ino(&(ref->khandle)); 1014 } 1015 1016 /* 1017 * Called to set up an inode from iget5_locked. 1018 */ 1019 static int orangefs_set_inode(struct inode *inode, void *data) 1020 { 1021 struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data; 1022 ORANGEFS_I(inode)->refn.fs_id = ref->fs_id; 1023 ORANGEFS_I(inode)->refn.khandle = ref->khandle; 1024 ORANGEFS_I(inode)->attr_valid = 0; 1025 hash_init(ORANGEFS_I(inode)->xattr_cache); 1026 ORANGEFS_I(inode)->mapping_time = jiffies - 1; 1027 ORANGEFS_I(inode)->bitlock = 0; 1028 return 0; 1029 } 1030 1031 /* 1032 * Called to determine if handles match. 1033 */ 1034 static int orangefs_test_inode(struct inode *inode, void *data) 1035 { 1036 struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data; 1037 struct orangefs_inode_s *orangefs_inode = NULL; 1038 1039 orangefs_inode = ORANGEFS_I(inode); 1040 /* test handles and fs_ids... */ 1041 return (!ORANGEFS_khandle_cmp(&(orangefs_inode->refn.khandle), 1042 &(ref->khandle)) && 1043 orangefs_inode->refn.fs_id == ref->fs_id); 1044 } 1045 1046 /* 1047 * Front-end to lookup the inode-cache maintained by the VFS using the ORANGEFS 1048 * file handle. 1049 * 1050 * @sb: the file system super block instance. 1051 * @ref: The ORANGEFS object for which we are trying to locate an inode. 1052 */ 1053 struct inode *orangefs_iget(struct super_block *sb, 1054 struct orangefs_object_kref *ref) 1055 { 1056 struct inode *inode = NULL; 1057 unsigned long hash; 1058 int error; 1059 1060 hash = orangefs_handle_hash(ref); 1061 inode = iget5_locked(sb, 1062 hash, 1063 orangefs_test_inode, 1064 orangefs_set_inode, 1065 ref); 1066 1067 if (!inode) 1068 return ERR_PTR(-ENOMEM); 1069 1070 if (!(inode->i_state & I_NEW)) 1071 return inode; 1072 1073 error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW); 1074 if (error) { 1075 iget_failed(inode); 1076 return ERR_PTR(error); 1077 } 1078 1079 inode->i_ino = hash; /* needed for stat etc */ 1080 orangefs_init_iops(inode); 1081 unlock_new_inode(inode); 1082 1083 gossip_debug(GOSSIP_INODE_DEBUG, 1084 "iget handle %pU, fsid %d hash %ld i_ino %lu\n", 1085 &ref->khandle, 1086 ref->fs_id, 1087 hash, 1088 inode->i_ino); 1089 1090 return inode; 1091 } 1092 1093 /* 1094 * Allocate an inode for a newly created file and insert it into the inode hash. 1095 */ 1096 struct inode *orangefs_new_inode(struct super_block *sb, struct inode *dir, 1097 umode_t mode, dev_t dev, struct orangefs_object_kref *ref) 1098 { 1099 struct posix_acl *acl = NULL, *default_acl = NULL; 1100 unsigned long hash = orangefs_handle_hash(ref); 1101 struct inode *inode; 1102 int error; 1103 1104 gossip_debug(GOSSIP_INODE_DEBUG, 1105 "%s:(sb is %p | MAJOR(dev)=%u | MINOR(dev)=%u mode=%o)\n", 1106 __func__, 1107 sb, 1108 MAJOR(dev), 1109 MINOR(dev), 1110 mode); 1111 1112 inode = new_inode(sb); 1113 if (!inode) 1114 return ERR_PTR(-ENOMEM); 1115 1116 error = posix_acl_create(dir, &mode, &default_acl, &acl); 1117 if (error) 1118 goto out_iput; 1119 1120 orangefs_set_inode(inode, ref); 1121 inode->i_ino = hash; /* needed for stat etc */ 1122 1123 error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW); 1124 if (error) 1125 goto out_iput; 1126 1127 orangefs_init_iops(inode); 1128 inode->i_rdev = dev; 1129 1130 if (default_acl) { 1131 error = __orangefs_set_acl(inode, default_acl, 1132 ACL_TYPE_DEFAULT); 1133 if (error) 1134 goto out_iput; 1135 } 1136 1137 if (acl) { 1138 error = __orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS); 1139 if (error) 1140 goto out_iput; 1141 } 1142 1143 error = insert_inode_locked4(inode, hash, orangefs_test_inode, ref); 1144 if (error < 0) 1145 goto out_iput; 1146 1147 gossip_debug(GOSSIP_INODE_DEBUG, 1148 "Initializing ACL's for inode %pU\n", 1149 get_khandle_from_ino(inode)); 1150 if (mode != inode->i_mode) { 1151 struct iattr iattr = { 1152 .ia_mode = mode, 1153 .ia_valid = ATTR_MODE, 1154 }; 1155 inode->i_mode = mode; 1156 __orangefs_setattr(inode, &iattr); 1157 __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); 1158 } 1159 posix_acl_release(acl); 1160 posix_acl_release(default_acl); 1161 return inode; 1162 1163 out_iput: 1164 iput(inode); 1165 posix_acl_release(acl); 1166 posix_acl_release(default_acl); 1167 return ERR_PTR(error); 1168 } 1169