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 <linux/netfs.h> 18 #include "internal.h" 19 20 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma); 21 static int afs_readpage(struct file *file, struct page *page); 22 static int afs_symlink_readpage(struct file *file, struct page *page); 23 static void afs_invalidatepage(struct page *page, unsigned int offset, 24 unsigned int length); 25 static int afs_releasepage(struct page *page, gfp_t gfp_flags); 26 27 static void afs_readahead(struct readahead_control *ractl); 28 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter); 29 static void afs_vm_open(struct vm_area_struct *area); 30 static void afs_vm_close(struct vm_area_struct *area); 31 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); 32 33 const struct file_operations afs_file_operations = { 34 .open = afs_open, 35 .release = afs_release, 36 .llseek = generic_file_llseek, 37 .read_iter = afs_file_read_iter, 38 .write_iter = afs_file_write, 39 .mmap = afs_file_mmap, 40 .splice_read = generic_file_splice_read, 41 .splice_write = iter_file_splice_write, 42 .fsync = afs_fsync, 43 .lock = afs_lock, 44 .flock = afs_flock, 45 }; 46 47 const struct inode_operations afs_file_inode_operations = { 48 .getattr = afs_getattr, 49 .setattr = afs_setattr, 50 .permission = afs_permission, 51 }; 52 53 const struct address_space_operations afs_file_aops = { 54 .readpage = afs_readpage, 55 .readahead = afs_readahead, 56 .set_page_dirty = afs_set_page_dirty, 57 .launder_page = afs_launder_page, 58 .releasepage = afs_releasepage, 59 .invalidatepage = afs_invalidatepage, 60 .write_begin = afs_write_begin, 61 .write_end = afs_write_end, 62 .writepage = afs_writepage, 63 .writepages = afs_writepages, 64 }; 65 66 const struct address_space_operations afs_symlink_aops = { 67 .readpage = afs_symlink_readpage, 68 .releasepage = afs_releasepage, 69 .invalidatepage = afs_invalidatepage, 70 }; 71 72 static const struct vm_operations_struct afs_vm_ops = { 73 .open = afs_vm_open, 74 .close = afs_vm_close, 75 .fault = filemap_fault, 76 .map_pages = afs_vm_map_pages, 77 .page_mkwrite = afs_page_mkwrite, 78 }; 79 80 /* 81 * Discard a pin on a writeback key. 82 */ 83 void afs_put_wb_key(struct afs_wb_key *wbk) 84 { 85 if (wbk && refcount_dec_and_test(&wbk->usage)) { 86 key_put(wbk->key); 87 kfree(wbk); 88 } 89 } 90 91 /* 92 * Cache key for writeback. 93 */ 94 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) 95 { 96 struct afs_wb_key *wbk, *p; 97 98 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); 99 if (!wbk) 100 return -ENOMEM; 101 refcount_set(&wbk->usage, 2); 102 wbk->key = af->key; 103 104 spin_lock(&vnode->wb_lock); 105 list_for_each_entry(p, &vnode->wb_keys, vnode_link) { 106 if (p->key == wbk->key) 107 goto found; 108 } 109 110 key_get(wbk->key); 111 list_add_tail(&wbk->vnode_link, &vnode->wb_keys); 112 spin_unlock(&vnode->wb_lock); 113 af->wb = wbk; 114 return 0; 115 116 found: 117 refcount_inc(&p->usage); 118 spin_unlock(&vnode->wb_lock); 119 af->wb = p; 120 kfree(wbk); 121 return 0; 122 } 123 124 /* 125 * open an AFS file or directory and attach a key to it 126 */ 127 int afs_open(struct inode *inode, struct file *file) 128 { 129 struct afs_vnode *vnode = AFS_FS_I(inode); 130 struct afs_file *af; 131 struct key *key; 132 int ret; 133 134 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 135 136 key = afs_request_key(vnode->volume->cell); 137 if (IS_ERR(key)) { 138 ret = PTR_ERR(key); 139 goto error; 140 } 141 142 af = kzalloc(sizeof(*af), GFP_KERNEL); 143 if (!af) { 144 ret = -ENOMEM; 145 goto error_key; 146 } 147 af->key = key; 148 149 ret = afs_validate(vnode, key); 150 if (ret < 0) 151 goto error_af; 152 153 if (file->f_mode & FMODE_WRITE) { 154 ret = afs_cache_wb_key(vnode, af); 155 if (ret < 0) 156 goto error_af; 157 } 158 159 if (file->f_flags & O_TRUNC) 160 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 161 162 file->private_data = af; 163 _leave(" = 0"); 164 return 0; 165 166 error_af: 167 kfree(af); 168 error_key: 169 key_put(key); 170 error: 171 _leave(" = %d", ret); 172 return ret; 173 } 174 175 /* 176 * release an AFS file or directory and discard its key 177 */ 178 int afs_release(struct inode *inode, struct file *file) 179 { 180 struct afs_vnode *vnode = AFS_FS_I(inode); 181 struct afs_file *af = file->private_data; 182 int ret = 0; 183 184 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 185 186 if ((file->f_mode & FMODE_WRITE)) 187 ret = vfs_fsync(file, 0); 188 189 file->private_data = NULL; 190 if (af->wb) 191 afs_put_wb_key(af->wb); 192 key_put(af->key); 193 kfree(af); 194 afs_prune_wb_keys(vnode); 195 _leave(" = %d", ret); 196 return ret; 197 } 198 199 /* 200 * Allocate a new read record. 201 */ 202 struct afs_read *afs_alloc_read(gfp_t gfp) 203 { 204 struct afs_read *req; 205 206 req = kzalloc(sizeof(struct afs_read), gfp); 207 if (req) 208 refcount_set(&req->usage, 1); 209 210 return req; 211 } 212 213 /* 214 * Dispose of a ref to a read record. 215 */ 216 void afs_put_read(struct afs_read *req) 217 { 218 if (refcount_dec_and_test(&req->usage)) { 219 if (req->cleanup) 220 req->cleanup(req); 221 key_put(req->key); 222 kfree(req); 223 } 224 } 225 226 static void afs_fetch_data_notify(struct afs_operation *op) 227 { 228 struct afs_read *req = op->fetch.req; 229 struct netfs_read_subrequest *subreq = req->subreq; 230 int error = op->error; 231 232 if (error == -ECONNABORTED) 233 error = afs_abort_to_error(op->ac.abort_code); 234 req->error = error; 235 236 if (subreq) { 237 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags); 238 netfs_subreq_terminated(subreq, error ?: req->actual_len, false); 239 req->subreq = NULL; 240 } else if (req->done) { 241 req->done(req); 242 } 243 } 244 245 static void afs_fetch_data_success(struct afs_operation *op) 246 { 247 struct afs_vnode *vnode = op->file[0].vnode; 248 249 _enter("op=%08x", op->debug_id); 250 afs_vnode_commit_status(op, &op->file[0]); 251 afs_stat_v(vnode, n_fetches); 252 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes); 253 afs_fetch_data_notify(op); 254 } 255 256 static void afs_fetch_data_put(struct afs_operation *op) 257 { 258 op->fetch.req->error = op->error; 259 afs_put_read(op->fetch.req); 260 } 261 262 static const struct afs_operation_ops afs_fetch_data_operation = { 263 .issue_afs_rpc = afs_fs_fetch_data, 264 .issue_yfs_rpc = yfs_fs_fetch_data, 265 .success = afs_fetch_data_success, 266 .aborted = afs_check_for_remote_deletion, 267 .failed = afs_fetch_data_notify, 268 .put = afs_fetch_data_put, 269 }; 270 271 /* 272 * Fetch file data from the volume. 273 */ 274 int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req) 275 { 276 struct afs_operation *op; 277 278 _enter("%s{%llx:%llu.%u},%x,,,", 279 vnode->volume->name, 280 vnode->fid.vid, 281 vnode->fid.vnode, 282 vnode->fid.unique, 283 key_serial(req->key)); 284 285 op = afs_alloc_operation(req->key, vnode->volume); 286 if (IS_ERR(op)) { 287 if (req->subreq) 288 netfs_subreq_terminated(req->subreq, PTR_ERR(op), false); 289 return PTR_ERR(op); 290 } 291 292 afs_op_set_vnode(op, 0, vnode); 293 294 op->fetch.req = afs_get_read(req); 295 op->ops = &afs_fetch_data_operation; 296 return afs_do_sync_operation(op); 297 } 298 299 static void afs_req_issue_op(struct netfs_read_subrequest *subreq) 300 { 301 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode); 302 struct afs_read *fsreq; 303 304 fsreq = afs_alloc_read(GFP_NOFS); 305 if (!fsreq) 306 return netfs_subreq_terminated(subreq, -ENOMEM, false); 307 308 fsreq->subreq = subreq; 309 fsreq->pos = subreq->start + subreq->transferred; 310 fsreq->len = subreq->len - subreq->transferred; 311 fsreq->key = key_get(subreq->rreq->netfs_priv); 312 fsreq->vnode = vnode; 313 fsreq->iter = &fsreq->def_iter; 314 315 iov_iter_xarray(&fsreq->def_iter, READ, 316 &fsreq->vnode->vfs_inode.i_mapping->i_pages, 317 fsreq->pos, fsreq->len); 318 319 afs_fetch_data(fsreq->vnode, fsreq); 320 afs_put_read(fsreq); 321 } 322 323 static int afs_symlink_readpage(struct file *file, struct page *page) 324 { 325 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 326 struct afs_read *fsreq; 327 int ret; 328 329 fsreq = afs_alloc_read(GFP_NOFS); 330 if (!fsreq) 331 return -ENOMEM; 332 333 fsreq->pos = page->index * PAGE_SIZE; 334 fsreq->len = PAGE_SIZE; 335 fsreq->vnode = vnode; 336 fsreq->iter = &fsreq->def_iter; 337 iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages, 338 fsreq->pos, fsreq->len); 339 340 ret = afs_fetch_data(fsreq->vnode, fsreq); 341 page_endio(page, false, ret); 342 return ret; 343 } 344 345 static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file) 346 { 347 rreq->netfs_priv = key_get(afs_file_key(file)); 348 } 349 350 static bool afs_is_cache_enabled(struct inode *inode) 351 { 352 struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode)); 353 354 return fscache_cookie_enabled(cookie) && !hlist_empty(&cookie->backing_objects); 355 } 356 357 static int afs_begin_cache_operation(struct netfs_read_request *rreq) 358 { 359 struct afs_vnode *vnode = AFS_FS_I(rreq->inode); 360 361 return fscache_begin_read_operation(rreq, afs_vnode_cache(vnode)); 362 } 363 364 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len, 365 struct page *page, void **_fsdata) 366 { 367 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 368 369 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0; 370 } 371 372 static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv) 373 { 374 key_put(netfs_priv); 375 } 376 377 const struct netfs_read_request_ops afs_req_ops = { 378 .init_rreq = afs_init_rreq, 379 .is_cache_enabled = afs_is_cache_enabled, 380 .begin_cache_operation = afs_begin_cache_operation, 381 .check_write_begin = afs_check_write_begin, 382 .issue_op = afs_req_issue_op, 383 .cleanup = afs_priv_cleanup, 384 }; 385 386 static int afs_readpage(struct file *file, struct page *page) 387 { 388 return netfs_readpage(file, page, &afs_req_ops, NULL); 389 } 390 391 static void afs_readahead(struct readahead_control *ractl) 392 { 393 netfs_readahead(ractl, &afs_req_ops, NULL); 394 } 395 396 /* 397 * Adjust the dirty region of the page on truncation or full invalidation, 398 * getting rid of the markers altogether if the region is entirely invalidated. 399 */ 400 static void afs_invalidate_dirty(struct page *page, unsigned int offset, 401 unsigned int length) 402 { 403 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 404 unsigned long priv; 405 unsigned int f, t, end = offset + length; 406 407 priv = page_private(page); 408 409 /* we clean up only if the entire page is being invalidated */ 410 if (offset == 0 && length == thp_size(page)) 411 goto full_invalidate; 412 413 /* If the page was dirtied by page_mkwrite(), the PTE stays writable 414 * and we don't get another notification to tell us to expand it 415 * again. 416 */ 417 if (afs_is_page_dirty_mmapped(priv)) 418 return; 419 420 /* We may need to shorten the dirty region */ 421 f = afs_page_dirty_from(page, priv); 422 t = afs_page_dirty_to(page, priv); 423 424 if (t <= offset || f >= end) 425 return; /* Doesn't overlap */ 426 427 if (f < offset && t > end) 428 return; /* Splits the dirty region - just absorb it */ 429 430 if (f >= offset && t <= end) 431 goto undirty; 432 433 if (f < offset) 434 t = offset; 435 else 436 f = end; 437 if (f == t) 438 goto undirty; 439 440 priv = afs_page_dirty(page, f, t); 441 set_page_private(page, priv); 442 trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page); 443 return; 444 445 undirty: 446 trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page); 447 clear_page_dirty_for_io(page); 448 full_invalidate: 449 trace_afs_page_dirty(vnode, tracepoint_string("inval"), page); 450 detach_page_private(page); 451 } 452 453 /* 454 * invalidate part or all of a page 455 * - release a page and clean up its private data if offset is 0 (indicating 456 * the entire page) 457 */ 458 static void afs_invalidatepage(struct page *page, unsigned int offset, 459 unsigned int length) 460 { 461 _enter("{%lu},%u,%u", page->index, offset, length); 462 463 BUG_ON(!PageLocked(page)); 464 465 if (PagePrivate(page)) 466 afs_invalidate_dirty(page, offset, length); 467 468 wait_on_page_fscache(page); 469 _leave(""); 470 } 471 472 /* 473 * release a page and clean up its private state if it's not busy 474 * - return true if the page can now be released, false if not 475 */ 476 static int afs_releasepage(struct page *page, gfp_t gfp_flags) 477 { 478 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 479 480 _enter("{{%llx:%llu}[%lu],%lx},%x", 481 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags, 482 gfp_flags); 483 484 /* deny if page is being written to the cache and the caller hasn't 485 * elected to wait */ 486 #ifdef CONFIG_AFS_FSCACHE 487 if (PageFsCache(page)) { 488 if (!(gfp_flags & __GFP_DIRECT_RECLAIM) || !(gfp_flags & __GFP_FS)) 489 return false; 490 wait_on_page_fscache(page); 491 } 492 #endif 493 494 if (PagePrivate(page)) { 495 trace_afs_page_dirty(vnode, tracepoint_string("rel"), page); 496 detach_page_private(page); 497 } 498 499 /* indicate that the page can be released */ 500 _leave(" = T"); 501 return 1; 502 } 503 504 static void afs_add_open_mmap(struct afs_vnode *vnode) 505 { 506 if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) { 507 down_write(&vnode->volume->cell->fs_open_mmaps_lock); 508 509 list_add_tail(&vnode->cb_mmap_link, 510 &vnode->volume->cell->fs_open_mmaps); 511 512 up_write(&vnode->volume->cell->fs_open_mmaps_lock); 513 } 514 } 515 516 static void afs_drop_open_mmap(struct afs_vnode *vnode) 517 { 518 if (!atomic_dec_and_test(&vnode->cb_nr_mmap)) 519 return; 520 521 down_write(&vnode->volume->cell->fs_open_mmaps_lock); 522 523 if (atomic_read(&vnode->cb_nr_mmap) == 0) 524 list_del_init(&vnode->cb_mmap_link); 525 526 up_write(&vnode->volume->cell->fs_open_mmaps_lock); 527 flush_work(&vnode->cb_work); 528 } 529 530 /* 531 * Handle setting up a memory mapping on an AFS file. 532 */ 533 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma) 534 { 535 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 536 int ret; 537 538 afs_add_open_mmap(vnode); 539 540 ret = generic_file_mmap(file, vma); 541 if (ret == 0) 542 vma->vm_ops = &afs_vm_ops; 543 else 544 afs_drop_open_mmap(vnode); 545 return ret; 546 } 547 548 static void afs_vm_open(struct vm_area_struct *vma) 549 { 550 afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); 551 } 552 553 static void afs_vm_close(struct vm_area_struct *vma) 554 { 555 afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); 556 } 557 558 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff) 559 { 560 struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file)); 561 struct afs_file *af = vmf->vma->vm_file->private_data; 562 563 switch (afs_validate(vnode, af->key)) { 564 case 0: 565 return filemap_map_pages(vmf, start_pgoff, end_pgoff); 566 case -ENOMEM: 567 return VM_FAULT_OOM; 568 case -EINTR: 569 case -ERESTARTSYS: 570 return VM_FAULT_RETRY; 571 case -ESTALE: 572 default: 573 return VM_FAULT_SIGBUS; 574 } 575 } 576 577 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) 578 { 579 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 580 struct afs_file *af = iocb->ki_filp->private_data; 581 int ret; 582 583 ret = afs_validate(vnode, af->key); 584 if (ret < 0) 585 return ret; 586 587 return generic_file_read_iter(iocb, iter); 588 } 589