1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* AFS filesystem file handling 3 * 4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/init.h> 11 #include <linux/fs.h> 12 #include <linux/pagemap.h> 13 #include <linux/writeback.h> 14 #include <linux/gfp.h> 15 #include <linux/task_io_accounting_ops.h> 16 #include <linux/mm.h> 17 #include "internal.h" 18 19 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma); 20 static int afs_readpage(struct file *file, struct page *page); 21 static void afs_invalidatepage(struct page *page, unsigned int offset, 22 unsigned int length); 23 static int afs_releasepage(struct page *page, gfp_t gfp_flags); 24 25 static int afs_readpages(struct file *filp, struct address_space *mapping, 26 struct list_head *pages, unsigned nr_pages); 27 28 const struct file_operations afs_file_operations = { 29 .open = afs_open, 30 .release = afs_release, 31 .llseek = generic_file_llseek, 32 .read_iter = generic_file_read_iter, 33 .write_iter = afs_file_write, 34 .mmap = afs_file_mmap, 35 .splice_read = generic_file_splice_read, 36 .fsync = afs_fsync, 37 .lock = afs_lock, 38 .flock = afs_flock, 39 }; 40 41 const struct inode_operations afs_file_inode_operations = { 42 .getattr = afs_getattr, 43 .setattr = afs_setattr, 44 .permission = afs_permission, 45 .listxattr = afs_listxattr, 46 }; 47 48 const struct address_space_operations afs_fs_aops = { 49 .readpage = afs_readpage, 50 .readpages = afs_readpages, 51 .set_page_dirty = afs_set_page_dirty, 52 .launder_page = afs_launder_page, 53 .releasepage = afs_releasepage, 54 .invalidatepage = afs_invalidatepage, 55 .write_begin = afs_write_begin, 56 .write_end = afs_write_end, 57 .writepage = afs_writepage, 58 .writepages = afs_writepages, 59 }; 60 61 static const struct vm_operations_struct afs_vm_ops = { 62 .fault = filemap_fault, 63 .map_pages = filemap_map_pages, 64 .page_mkwrite = afs_page_mkwrite, 65 }; 66 67 /* 68 * Discard a pin on a writeback key. 69 */ 70 void afs_put_wb_key(struct afs_wb_key *wbk) 71 { 72 if (refcount_dec_and_test(&wbk->usage)) { 73 key_put(wbk->key); 74 kfree(wbk); 75 } 76 } 77 78 /* 79 * Cache key for writeback. 80 */ 81 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) 82 { 83 struct afs_wb_key *wbk, *p; 84 85 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); 86 if (!wbk) 87 return -ENOMEM; 88 refcount_set(&wbk->usage, 2); 89 wbk->key = af->key; 90 91 spin_lock(&vnode->wb_lock); 92 list_for_each_entry(p, &vnode->wb_keys, vnode_link) { 93 if (p->key == wbk->key) 94 goto found; 95 } 96 97 key_get(wbk->key); 98 list_add_tail(&wbk->vnode_link, &vnode->wb_keys); 99 spin_unlock(&vnode->wb_lock); 100 af->wb = wbk; 101 return 0; 102 103 found: 104 refcount_inc(&p->usage); 105 spin_unlock(&vnode->wb_lock); 106 af->wb = p; 107 kfree(wbk); 108 return 0; 109 } 110 111 /* 112 * open an AFS file or directory and attach a key to it 113 */ 114 int afs_open(struct inode *inode, struct file *file) 115 { 116 struct afs_vnode *vnode = AFS_FS_I(inode); 117 struct afs_file *af; 118 struct key *key; 119 int ret; 120 121 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 122 123 key = afs_request_key(vnode->volume->cell); 124 if (IS_ERR(key)) { 125 ret = PTR_ERR(key); 126 goto error; 127 } 128 129 af = kzalloc(sizeof(*af), GFP_KERNEL); 130 if (!af) { 131 ret = -ENOMEM; 132 goto error_key; 133 } 134 af->key = key; 135 136 ret = afs_validate(vnode, key); 137 if (ret < 0) 138 goto error_af; 139 140 if (file->f_mode & FMODE_WRITE) { 141 ret = afs_cache_wb_key(vnode, af); 142 if (ret < 0) 143 goto error_af; 144 } 145 146 if (file->f_flags & O_TRUNC) 147 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 148 149 file->private_data = af; 150 _leave(" = 0"); 151 return 0; 152 153 error_af: 154 kfree(af); 155 error_key: 156 key_put(key); 157 error: 158 _leave(" = %d", ret); 159 return ret; 160 } 161 162 /* 163 * release an AFS file or directory and discard its key 164 */ 165 int afs_release(struct inode *inode, struct file *file) 166 { 167 struct afs_vnode *vnode = AFS_FS_I(inode); 168 struct afs_file *af = file->private_data; 169 int ret = 0; 170 171 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 172 173 if ((file->f_mode & FMODE_WRITE)) 174 ret = vfs_fsync(file, 0); 175 176 file->private_data = NULL; 177 if (af->wb) 178 afs_put_wb_key(af->wb); 179 key_put(af->key); 180 kfree(af); 181 afs_prune_wb_keys(vnode); 182 _leave(" = %d", ret); 183 return ret; 184 } 185 186 /* 187 * Dispose of a ref to a read record. 188 */ 189 void afs_put_read(struct afs_read *req) 190 { 191 int i; 192 193 if (refcount_dec_and_test(&req->usage)) { 194 for (i = 0; i < req->nr_pages; i++) 195 if (req->pages[i]) 196 put_page(req->pages[i]); 197 if (req->pages != req->array) 198 kfree(req->pages); 199 kfree(req); 200 } 201 } 202 203 #ifdef CONFIG_AFS_FSCACHE 204 /* 205 * deal with notification that a page was read from the cache 206 */ 207 static void afs_file_readpage_read_complete(struct page *page, 208 void *data, 209 int error) 210 { 211 _enter("%p,%p,%d", page, data, error); 212 213 /* if the read completes with an error, we just unlock the page and let 214 * the VM reissue the readpage */ 215 if (!error) 216 SetPageUptodate(page); 217 unlock_page(page); 218 } 219 #endif 220 221 /* 222 * Fetch file data from the volume. 223 */ 224 int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *desc) 225 { 226 struct afs_fs_cursor fc; 227 struct afs_status_cb *scb; 228 int ret; 229 230 _enter("%s{%llx:%llu.%u},%x,,,", 231 vnode->volume->name, 232 vnode->fid.vid, 233 vnode->fid.vnode, 234 vnode->fid.unique, 235 key_serial(key)); 236 237 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL); 238 if (!scb) 239 return -ENOMEM; 240 241 ret = -ERESTARTSYS; 242 if (afs_begin_vnode_operation(&fc, vnode, key, true)) { 243 afs_dataversion_t data_version = vnode->status.data_version; 244 245 while (afs_select_fileserver(&fc)) { 246 fc.cb_break = afs_calc_vnode_cb_break(vnode); 247 afs_fs_fetch_data(&fc, scb, desc); 248 } 249 250 afs_check_for_remote_deletion(&fc, vnode); 251 afs_vnode_commit_status(&fc, vnode, fc.cb_break, 252 &data_version, scb); 253 ret = afs_end_vnode_operation(&fc); 254 } 255 256 if (ret == 0) { 257 afs_stat_v(vnode, n_fetches); 258 atomic_long_add(desc->actual_len, 259 &afs_v2net(vnode)->n_fetch_bytes); 260 } 261 262 kfree(scb); 263 _leave(" = %d", ret); 264 return ret; 265 } 266 267 /* 268 * read page from file, directory or symlink, given a key to use 269 */ 270 int afs_page_filler(void *data, struct page *page) 271 { 272 struct inode *inode = page->mapping->host; 273 struct afs_vnode *vnode = AFS_FS_I(inode); 274 struct afs_read *req; 275 struct key *key = data; 276 int ret; 277 278 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index); 279 280 BUG_ON(!PageLocked(page)); 281 282 ret = -ESTALE; 283 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 284 goto error; 285 286 /* is it cached? */ 287 #ifdef CONFIG_AFS_FSCACHE 288 ret = fscache_read_or_alloc_page(vnode->cache, 289 page, 290 afs_file_readpage_read_complete, 291 NULL, 292 GFP_KERNEL); 293 #else 294 ret = -ENOBUFS; 295 #endif 296 switch (ret) { 297 /* read BIO submitted (page in cache) */ 298 case 0: 299 break; 300 301 /* page not yet cached */ 302 case -ENODATA: 303 _debug("cache said ENODATA"); 304 goto go_on; 305 306 /* page will not be cached */ 307 case -ENOBUFS: 308 _debug("cache said ENOBUFS"); 309 310 /* fall through */ 311 default: 312 go_on: 313 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL); 314 if (!req) 315 goto enomem; 316 317 /* We request a full page. If the page is a partial one at the 318 * end of the file, the server will return a short read and the 319 * unmarshalling code will clear the unfilled space. 320 */ 321 refcount_set(&req->usage, 1); 322 req->pos = (loff_t)page->index << PAGE_SHIFT; 323 req->len = PAGE_SIZE; 324 req->nr_pages = 1; 325 req->pages = req->array; 326 req->pages[0] = page; 327 get_page(page); 328 329 /* read the contents of the file from the server into the 330 * page */ 331 ret = afs_fetch_data(vnode, key, req); 332 afs_put_read(req); 333 334 if (ret < 0) { 335 if (ret == -ENOENT) { 336 _debug("got NOENT from server" 337 " - marking file deleted and stale"); 338 set_bit(AFS_VNODE_DELETED, &vnode->flags); 339 ret = -ESTALE; 340 } 341 342 #ifdef CONFIG_AFS_FSCACHE 343 fscache_uncache_page(vnode->cache, page); 344 #endif 345 BUG_ON(PageFsCache(page)); 346 347 if (ret == -EINTR || 348 ret == -ENOMEM || 349 ret == -ERESTARTSYS || 350 ret == -EAGAIN) 351 goto error; 352 goto io_error; 353 } 354 355 SetPageUptodate(page); 356 357 /* send the page to the cache */ 358 #ifdef CONFIG_AFS_FSCACHE 359 if (PageFsCache(page) && 360 fscache_write_page(vnode->cache, page, vnode->status.size, 361 GFP_KERNEL) != 0) { 362 fscache_uncache_page(vnode->cache, page); 363 BUG_ON(PageFsCache(page)); 364 } 365 #endif 366 unlock_page(page); 367 } 368 369 _leave(" = 0"); 370 return 0; 371 372 io_error: 373 SetPageError(page); 374 goto error; 375 enomem: 376 ret = -ENOMEM; 377 error: 378 unlock_page(page); 379 _leave(" = %d", ret); 380 return ret; 381 } 382 383 /* 384 * read page from file, directory or symlink, given a file to nominate the key 385 * to be used 386 */ 387 static int afs_readpage(struct file *file, struct page *page) 388 { 389 struct key *key; 390 int ret; 391 392 if (file) { 393 key = afs_file_key(file); 394 ASSERT(key != NULL); 395 ret = afs_page_filler(key, page); 396 } else { 397 struct inode *inode = page->mapping->host; 398 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell); 399 if (IS_ERR(key)) { 400 ret = PTR_ERR(key); 401 } else { 402 ret = afs_page_filler(key, page); 403 key_put(key); 404 } 405 } 406 return ret; 407 } 408 409 /* 410 * Make pages available as they're filled. 411 */ 412 static void afs_readpages_page_done(struct afs_read *req) 413 { 414 #ifdef CONFIG_AFS_FSCACHE 415 struct afs_vnode *vnode = req->vnode; 416 #endif 417 struct page *page = req->pages[req->index]; 418 419 req->pages[req->index] = NULL; 420 SetPageUptodate(page); 421 422 /* send the page to the cache */ 423 #ifdef CONFIG_AFS_FSCACHE 424 if (PageFsCache(page) && 425 fscache_write_page(vnode->cache, page, vnode->status.size, 426 GFP_KERNEL) != 0) { 427 fscache_uncache_page(vnode->cache, page); 428 BUG_ON(PageFsCache(page)); 429 } 430 #endif 431 unlock_page(page); 432 put_page(page); 433 } 434 435 /* 436 * Read a contiguous set of pages. 437 */ 438 static int afs_readpages_one(struct file *file, struct address_space *mapping, 439 struct list_head *pages) 440 { 441 struct afs_vnode *vnode = AFS_FS_I(mapping->host); 442 struct afs_read *req; 443 struct list_head *p; 444 struct page *first, *page; 445 struct key *key = afs_file_key(file); 446 pgoff_t index; 447 int ret, n, i; 448 449 /* Count the number of contiguous pages at the front of the list. Note 450 * that the list goes prev-wards rather than next-wards. 451 */ 452 first = lru_to_page(pages); 453 index = first->index + 1; 454 n = 1; 455 for (p = first->lru.prev; p != pages; p = p->prev) { 456 page = list_entry(p, struct page, lru); 457 if (page->index != index) 458 break; 459 index++; 460 n++; 461 } 462 463 req = kzalloc(struct_size(req, array, n), GFP_NOFS); 464 if (!req) 465 return -ENOMEM; 466 467 refcount_set(&req->usage, 1); 468 req->vnode = vnode; 469 req->page_done = afs_readpages_page_done; 470 req->pos = first->index; 471 req->pos <<= PAGE_SHIFT; 472 req->pages = req->array; 473 474 /* Transfer the pages to the request. We add them in until one fails 475 * to add to the LRU and then we stop (as that'll make a hole in the 476 * contiguous run. 477 * 478 * Note that it's possible for the file size to change whilst we're 479 * doing this, but we rely on the server returning less than we asked 480 * for if the file shrank. We also rely on this to deal with a partial 481 * page at the end of the file. 482 */ 483 do { 484 page = lru_to_page(pages); 485 list_del(&page->lru); 486 index = page->index; 487 if (add_to_page_cache_lru(page, mapping, index, 488 readahead_gfp_mask(mapping))) { 489 #ifdef CONFIG_AFS_FSCACHE 490 fscache_uncache_page(vnode->cache, page); 491 #endif 492 put_page(page); 493 break; 494 } 495 496 req->pages[req->nr_pages++] = page; 497 req->len += PAGE_SIZE; 498 } while (req->nr_pages < n); 499 500 if (req->nr_pages == 0) { 501 kfree(req); 502 return 0; 503 } 504 505 ret = afs_fetch_data(vnode, key, req); 506 if (ret < 0) 507 goto error; 508 509 task_io_account_read(PAGE_SIZE * req->nr_pages); 510 afs_put_read(req); 511 return 0; 512 513 error: 514 if (ret == -ENOENT) { 515 _debug("got NOENT from server" 516 " - marking file deleted and stale"); 517 set_bit(AFS_VNODE_DELETED, &vnode->flags); 518 ret = -ESTALE; 519 } 520 521 for (i = 0; i < req->nr_pages; i++) { 522 page = req->pages[i]; 523 if (page) { 524 #ifdef CONFIG_AFS_FSCACHE 525 fscache_uncache_page(vnode->cache, page); 526 #endif 527 SetPageError(page); 528 unlock_page(page); 529 } 530 } 531 532 afs_put_read(req); 533 return ret; 534 } 535 536 /* 537 * read a set of pages 538 */ 539 static int afs_readpages(struct file *file, struct address_space *mapping, 540 struct list_head *pages, unsigned nr_pages) 541 { 542 struct key *key = afs_file_key(file); 543 struct afs_vnode *vnode; 544 int ret = 0; 545 546 _enter("{%d},{%lu},,%d", 547 key_serial(key), mapping->host->i_ino, nr_pages); 548 549 ASSERT(key != NULL); 550 551 vnode = AFS_FS_I(mapping->host); 552 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 553 _leave(" = -ESTALE"); 554 return -ESTALE; 555 } 556 557 /* attempt to read as many of the pages as possible */ 558 #ifdef CONFIG_AFS_FSCACHE 559 ret = fscache_read_or_alloc_pages(vnode->cache, 560 mapping, 561 pages, 562 &nr_pages, 563 afs_file_readpage_read_complete, 564 NULL, 565 mapping_gfp_mask(mapping)); 566 #else 567 ret = -ENOBUFS; 568 #endif 569 570 switch (ret) { 571 /* all pages are being read from the cache */ 572 case 0: 573 BUG_ON(!list_empty(pages)); 574 BUG_ON(nr_pages != 0); 575 _leave(" = 0 [reading all]"); 576 return 0; 577 578 /* there were pages that couldn't be read from the cache */ 579 case -ENODATA: 580 case -ENOBUFS: 581 break; 582 583 /* other error */ 584 default: 585 _leave(" = %d", ret); 586 return ret; 587 } 588 589 while (!list_empty(pages)) { 590 ret = afs_readpages_one(file, mapping, pages); 591 if (ret < 0) 592 break; 593 } 594 595 _leave(" = %d [netting]", ret); 596 return ret; 597 } 598 599 /* 600 * invalidate part or all of a page 601 * - release a page and clean up its private data if offset is 0 (indicating 602 * the entire page) 603 */ 604 static void afs_invalidatepage(struct page *page, unsigned int offset, 605 unsigned int length) 606 { 607 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 608 unsigned long priv; 609 610 _enter("{%lu},%u,%u", page->index, offset, length); 611 612 BUG_ON(!PageLocked(page)); 613 614 /* we clean up only if the entire page is being invalidated */ 615 if (offset == 0 && length == PAGE_SIZE) { 616 #ifdef CONFIG_AFS_FSCACHE 617 if (PageFsCache(page)) { 618 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 619 fscache_wait_on_page_write(vnode->cache, page); 620 fscache_uncache_page(vnode->cache, page); 621 } 622 #endif 623 624 if (PagePrivate(page)) { 625 priv = page_private(page); 626 trace_afs_page_dirty(vnode, tracepoint_string("inval"), 627 page->index, priv); 628 set_page_private(page, 0); 629 ClearPagePrivate(page); 630 } 631 } 632 633 _leave(""); 634 } 635 636 /* 637 * release a page and clean up its private state if it's not busy 638 * - return true if the page can now be released, false if not 639 */ 640 static int afs_releasepage(struct page *page, gfp_t gfp_flags) 641 { 642 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 643 unsigned long priv; 644 645 _enter("{{%llx:%llu}[%lu],%lx},%x", 646 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags, 647 gfp_flags); 648 649 /* deny if page is being written to the cache and the caller hasn't 650 * elected to wait */ 651 #ifdef CONFIG_AFS_FSCACHE 652 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) { 653 _leave(" = F [cache busy]"); 654 return 0; 655 } 656 #endif 657 658 if (PagePrivate(page)) { 659 priv = page_private(page); 660 trace_afs_page_dirty(vnode, tracepoint_string("rel"), 661 page->index, priv); 662 set_page_private(page, 0); 663 ClearPagePrivate(page); 664 } 665 666 /* indicate that the page can be released */ 667 _leave(" = T"); 668 return 1; 669 } 670 671 /* 672 * Handle setting up a memory mapping on an AFS file. 673 */ 674 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma) 675 { 676 int ret; 677 678 ret = generic_file_mmap(file, vma); 679 if (ret == 0) 680 vma->vm_ops = &afs_vm_ops; 681 return ret; 682 } 683